Skip to content

Commit

Permalink
Merge pull request #8 from PerfectFit-project/285-file-sharing
Browse files Browse the repository at this point in the history
285 file sharing
  • Loading branch information
wbaccinelli authored Nov 9, 2022
2 parents 9066a11 + 8de80b3 commit 7d3b935
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions niceday_client/niceday_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def _call_api(self,
method: str,
url: str,
query_params: typing.Optional[dict] = None,
body: typing.Optional[dict] = None) -> requests.Response:
body: typing.Optional[dict] = None,
files: typing.List[typing.Tuple[str, typing.Tuple[str, typing.Any, str]]] = None) -> requests.Response:
"""
Handles http requests with the niceday-api.
Expand All @@ -62,7 +63,7 @@ def _call_api(self,
if method == 'GET':
r = requests.get(url, params=query_params, headers=headers)
elif method == 'POST':
r = requests.post(url, params=query_params, headers=headers, json=body)
r = requests.post(url, params=query_params, headers=headers, data=body, files=files)
else:
raise NotImplementedError('Other methods are not implemented yet')
r.raise_for_status()
Expand Down Expand Up @@ -234,4 +235,33 @@ def set_tracker_reminder(self, user_id: int, tracker_name: str, reminder_title:
"userId": str(user_id),
"recurringSchedule": recurring_schedule
}
return self._call_api('POST', url, body=body)
return self._call_api('POST', url, body=body)

def upload_file(self, user_id: int, filepath: str, file):
"""
Get smoking tracker data for specific user
Args:
user_id: ID of the user we want to set tracker statuses for
filepath: The local path of the file to be uploaded
file: file stream created
Returns:
The answer of the NiceDay server.
"""
url = self._niceday_api_uri + 'files'

filename = filepath.split('/')[-1]
files = [('file', (filename, file, 'application/octet-stream'))]

body = {
'receiver_id': str(user_id),
'file_path': filepath
}

query_response = self._call_api('POST', url, body=body, files=files)
# convert the json response into a list of dict
response_json = json.loads(query_response.content)

return response_json

0 comments on commit 7d3b935

Please sign in to comment.