Commit 6745fc22 authored by Jeroen F.J. Laros's avatar Jeroen F.J. Laros

More elegant solution for the multipart encoding forms.

parent 9d7aee94
......@@ -24,9 +24,7 @@ class TransferClient(object):
except AttributeError:
sys.stderr = open('/dev/null')
def _request(
self, method, endpoint, headers=None, files=None, json=None,
data=None):
def _request(self, method, endpoint, headers=None, data=None, json=None):
"""
Handle a request.
......@@ -40,16 +38,9 @@ class TransferClient(object):
:returns dict: JSON encoded content of the response.
"""
try:
m = None
if data:
m = requests_toolbelt.MultipartEncoder(
fields=map(
lambda x: {x[0]: (x[1].name, x[1])}, data.items())[0])
headers.update({'Content-Type': m.content_type})
response = requests.request(
method, 'https://{}/{}'.format(self.server_name, endpoint),
headers=headers, data=m, files=files, json=json,
verify=self._verify)
headers=headers, data=data, json=json, verify=self._verify)
except requests.exceptions.SSLError:
raise OSError('SSL error, no server certificate installed?')
if not response.ok:
......@@ -84,9 +75,12 @@ class TransferClient(object):
:returns dict: Transfer JSON object.
"""
multipart = requests_toolbelt.MultipartEncoder(
fields={'metadata': ('metadata.json', json.dumps(metadata))})
return self._request(
'post', 'transfers', {'User-Id': user_id},
{'metadata': json.dumps(metadata)})
'post', 'transfers',
{'User-Id': user_id, 'Content-Type': multipart.content_type},
multipart)
def status(self, user_id, transfer_id):
"""
......@@ -119,9 +113,12 @@ class TransferClient(object):
:returns dict: Transfer JSON object.
"""
multipart = requests_toolbelt.MultipartEncoder(
fields={'upload': (file_handle.name, file_handle)})
return self._request(
'post', 'transfers/{}/uploads'.format(transfer_id),
{'User-Id': user_id}, data={'upload': file_handle})
{'User-Id': user_id, 'Content-Type': multipart.content_type},
multipart)
def completed(self, client_id):
"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment