Skip to content

Commit

Permalink
streaming files
Browse files Browse the repository at this point in the history
  • Loading branch information
maximemulder committed Nov 15, 2024
1 parent cb4e2f9 commit 704653d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
6 changes: 5 additions & 1 deletion python/lib/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import requests
from requests import HTTPError
from requests_toolbelt import MultipartEncoder

# TODO: Turn into a type declaration with Python 3.12
ApiVersion = Literal['v0.0.3', 'v0.0.4-dev']
Expand Down Expand Up @@ -43,14 +44,17 @@ def post(
self,
version: ApiVersion,
route: str,
data: dict[str, str] = {},
data: dict[str, str] | MultipartEncoder = {},
json: dict[str, Any] | None = None,
files: dict[str, Any] | None = None,
):
headers = {
'Authorization': f'Bearer {self.api_token}',
}

if isinstance(data, MultipartEncoder):
headers['Content-Type'] = data.content_type

try:
response = requests.post(
f'https://{self.loris_url}/api/{version}/{route}',
Expand Down
11 changes: 5 additions & 6 deletions python/lib/api/endpoints/dicom.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import os

from requests_toolbelt import MultipartEncoder

from lib.api.client import ApiClient
from lib.api.models.dicom import GetDicom, GetDicomProcess, GetDicomProcesses, PostDicomProcesses

Expand All @@ -19,21 +21,18 @@ def post_candidate_dicom(
overwrite: bool,
file_path: str,
):
data = {
multipart = MultipartEncoder(fields={
'Json': json.dumps({
'CandID': cand_id,
'PSCID': psc_id,
'VisitLabel': visit_label,
'IsPhantom': is_phantom,
'Overwrite': overwrite,
}),
}

files = {
'File': (os.path.basename(file_path), open(file_path, 'rb'), 'application/x-tar'),
}
})

response = api.post('v0.0.4-dev', f'candidates/{cand_id}/{visit_label}/dicoms', data=data, files=files)
response = api.post('v0.0.4-dev', f'candidates/{cand_id}/{visit_label}/dicoms', data=multipart)
return response.headers['Location']


Expand Down
1 change: 1 addition & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pyright
pytest
python-dateutil
requests
requests_toolbelt
ruff
scikit-learn
scipy
Expand Down

0 comments on commit 704653d

Please sign in to comment.