Added support for asset upload

Removed lxml dependency
This commit is contained in:
Rick Rongen
2019-03-12 11:14:00 +01:00
parent f71419820f
commit 4989913304
2 changed files with 28 additions and 2 deletions

View File

@@ -25,6 +25,8 @@ CRX_SERVER_ROOT = '/crx/server/crx.default/jcr:root/'
CRX_QUERY = '/crx/de/query.jsp' CRX_QUERY = '/crx/de/query.jsp'
WCM_REFERENCES = '/bin/wcm/references.json' WCM_REFERENCES = '/bin/wcm/references.json'
CREATE_ASSET = '.createasset.html'
JSON_DATA_EXTENSION = '.1.json' JSON_DATA_EXTENSION = '.1.json'
QUERY_TYPES = { QUERY_TYPES = {
@@ -66,7 +68,7 @@ class Connection:
""" """
self._session.auth = (username, password) self._session.auth = (username, password)
def query(self, query: str, query_type: str = 'SQL2') -> List[str]: def query(self, query: str, query_type: str = 'SQL2', raise_on_error: bool = True) -> List[str]:
""" """
Perform an query and return the matching paths. Perform an query and return the matching paths.
Query may be an XPATH, SQL or SQL2 Query Query may be an XPATH, SQL or SQL2 Query
@@ -87,6 +89,8 @@ class Connection:
data = response.json() data = response.json()
# TODO check for error # TODO check for error
if not data['success']:
raise ValueError(data['errorMessage'])
return list(map(lambda node: node['path'], data['results'])) return list(map(lambda node: node['path'], data['results']))
@@ -106,6 +110,28 @@ class Connection:
}) })
return response.json()['pages'] return response.json()['pages']
def upload_asset(self, dam_directory: str, filename: str, data: bytes, content_type: str):
"""
Upload an asset to the DAM as if it was uploaded through the GUI
Args:
dam_directory: The directory to upload to (including /content/dam)
filename: The file name of the asset (no path or anything)
data: The content of the asset
content_type: The content type of the asset
Raises:
When an error occurs, Request will raise an error for the incorrect status code
"""""
url: str = self._host + dam_directory + CREATE_ASSET
files = {
'file': (filename, data, content_type),
'fileName': filename,
'_charset_': 'utf-8'
}
resp = self._session.post(url, files=files)
resp.raise_for_status()
def get_node_raw(self, path: str): def get_node_raw(self, path: str):
""" """
Get the raw JSON dictionary of a node. Get the raw JSON dictionary of a node.

View File

@@ -1,5 +1,5 @@
requests >= 2.21.0 requests >= 2.21.0
lxml >= 4.3.0 #lxml >= 4.3.0
# For unprotect function # For unprotect function
# pywin32 >= 224 # pywin32 >= 224