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):
def __exit__(self, *exc):
return
def _make_authorization(self):
return "Token {}".format(self.token)
def _make_headers(self):
if self.token is None:
return None
return {
"Authorization": "Token %s" % (self.token),
"Authorization": self._make_authorization(),
"Content-Type": "application/json",
}
......@@ -111,6 +114,31 @@ class WebAPI(object):
except json.JSONDecodeError:
return answer.text
def upload(self, path, data):
url = self.__build_url(path)
answer = requests.post(
url, files=data, headers={"Authorization": self._make_authorization()}
)
if answer.status_code not in [200, 201, 204]:
raise WebAPIError("POST", url, answer)
try:
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):
url = self.__build_url(path)
answer = requests.put(url, json=data, headers=self._make_headers())
......
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