diff --git a/setup.py b/setup.py index 04720a8..4e06b31 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name = "thirdlight", - version = "0.1.0", + version = "0.2.1", author = "ReThought Ltd", author_email = "code@rethought-solutions.com", url = "https://github.com/Rethought/thirdlight.git", diff --git a/src/thirdlight.py b/src/thirdlight.py index b7ad584..58b03f2 100644 --- a/src/thirdlight.py +++ b/src/thirdlight.py @@ -108,7 +108,7 @@ class ThirdLight(object): API_VERSION = "1.0" FOLDER_TREE = None - def __init__(self, thirdlight_url, api_key): + def __init__(self, thirdlight_url, api_key, api_user=None): """ Construct a ThirdLight object which will hook into `api.json.tlx` at `thirdlight_url` which will likely be of the form @@ -117,6 +117,7 @@ def __init__(self, thirdlight_url, api_key): self.api_url = urllib.basejoin(thirdlight_url, ThirdLight.API_ENDPOINT) self.api_key = api_key + self.api_user = api_user self.session_key = None def _is_tl_method(self, name): @@ -194,7 +195,7 @@ def _query(self, **inParams): response = requests.post( self.thirdlight.api_url, data=json.dumps(params), - ).json + ).json() # note some methods return None - such as adding files to # an asynchronous upload. @@ -211,8 +212,12 @@ def connect(self): response = self.Core_LoginWithKey(apikey=self.api_key) self.session_key = response.sessionId + if self.api_user: + response = self.Core_ImpersonateUser(userRef=self.api_user, lookupType='username') + self.session_key = response.sessionId + def upload_image(self, source, folderId=None, folderPath=None, caption="", - keywords=[], block=True): + keywords=[], block=True, extra_meta={}): """Upload image at 'source' to folder with the given folderId and captioned and keyworded accordingly. Asynchronous upload, you get the uploadKey returned. @@ -252,9 +257,19 @@ def upload_image(self, source, folderId=None, folderPath=None, caption="", if folderId is None: folderId = self.resolve_folder_id(folderPath) - response = self.Upload_CreateUpload(params=dict(destination=folderId, - synchronous=False, - lifetime=60)) + edit_md = dict( + caption='OPTIONAL', + keywords='OPTIONAL' + ) + for key in extra_meta: + edit_md.update({key: 'OPTIONAL'}) + + response = self.Upload_CreateUpload(params=dict( + destination=folderId, + synchronous=False, + lifetime=60, + editablemetadata=edit_md + )) uploadKey = response.uploadKey # get the file base64 encoded - we'll look to sort out the big file @@ -272,6 +287,9 @@ def upload_image(self, source, folderId=None, folderPath=None, caption="", "data": b64, "metadata": {'caption': caption, 'keywords': keywords}, } + # Any additional file metadata to include in the upload + if extra_meta: + fileData['metadata'].update(extra_meta) fileData = dict(upload_file=fileData) self.Upload_AddFilesToUpload(uploadKey=uploadKey, fileData=fileData)