Updated Python SDK

This commit is contained in:
Eldad Fux
2020-07-26 10:14:51 +03:00
parent 571f7fb8b8
commit 9f6080ae7b
11 changed files with 469 additions and 159 deletions
+33 -6
View File
@@ -1,6 +1,6 @@
import io
import requests
class Client:
def __init__(self):
self._self_signed = False
@@ -47,24 +47,34 @@ class Client:
data = {}
json = {}
files = {}
self._global_headers.update(headers)
headers = {**self._global_headers, **headers}
if method != 'get':
data = params
params = {}
if headers['content-type'] == 'application/json':
if headers['content-type'].startswith('application/json'):
json = data
data = {}
if headers['content-type'].startswith('multipart/form-data'):
del headers['content-type']
for key in data.copy():
if isinstance(data[key], io.BufferedIOBase):
files[key] = data[key]
del data[key]
response = requests.request( # call method dynamically https://stackoverflow.com/a/4246075/2299554
method=method,
url=self._endpoint + path,
params=params,
data=data,
params=self.flatten(params),
data=self.flatten(data),
json=json,
headers=self._global_headers,
files=files,
headers=headers,
verify=self._self_signed,
)
@@ -77,3 +87,20 @@ class Client:
return response._content
def flatten(self, data, prefix=''):
output = {}
i = 0
for key in data:
value = data[key] if isinstance(data, dict) else key
finalKey = prefix + '[' + key +']' if prefix else key
finalKey = prefix + '[' + str(i) +']' if isinstance(data, list) else finalKey
i += 1
if isinstance(value, list) or isinstance(value, dict):
output = {**output, **self.flatten(value, finalKey)}
else:
output[finalKey] = value
return output