Commit ba1d6548 authored by Samuel GAIST's avatar Samuel GAIST Committed by Amir MOHAMMADI

[webapi] Add upload/download method to send files

parent 9e74c1db
...@@ -69,12 +69,15 @@ class WebAPI(object): ...@@ -69,12 +69,15 @@ class WebAPI(object):
def __exit__(self, *exc): def __exit__(self, *exc):
return return
def _make_authorization(self):
return "Token {}".format(self.token)
def _make_headers(self): def _make_headers(self):
if self.token is None: if self.token is None:
return None return None
return { return {
"Authorization": "Token %s" % (self.token), "Authorization": self._make_authorization(),
"Content-Type": "application/json", "Content-Type": "application/json",
} }
...@@ -111,6 +114,31 @@ class WebAPI(object): ...@@ -111,6 +114,31 @@ class WebAPI(object):
except json.JSONDecodeError: except json.JSONDecodeError:
return answer.text return answer.text
def upload(self, path, data):
url = self.__build_url(path)
answer =
url, files=data, headers={"Authorization": self._make_authorization()}
if answer.status_code not in [200, 201, 204]:
raise WebAPIError("POST", url, answer)
return answer.json()
except json.JSONDecodeError:
return answer.text
def download(self, path):
url = self.__build_url(path)
answer = requests.get(
url, headers={"Authorization": self._make_authorization()}
if answer.status_code not in [200, 204]:
raise WebAPIError("GET", url, answer)
return answer.content
def put(self, path, data=None): def put(self, path, data=None):
url = self.__build_url(path) url = self.__build_url(path)
answer = requests.put(url, json=data, headers=self._make_headers()) answer = requests.put(url, json=data, headers=self._make_headers())
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment