Skip to content

Additionally upload metadata #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name = "thirdlight",
version = "0.1.0",
version = "0.2.1",
author = "ReThought Ltd",
author_email = "[email protected]",
url = "https://github.com/Rethought/thirdlight.git",
Expand Down
30 changes: 24 additions & 6 deletions src/thirdlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down