From 704653dce7a823dc80eef138341fb14df9862266 Mon Sep 17 00:00:00 2001 From: Maxime Mulder Date: Fri, 15 Nov 2024 02:20:45 -0500 Subject: [PATCH] streaming files --- python/lib/api/client.py | 6 +++++- python/lib/api/endpoints/dicom.py | 11 +++++------ python/requirements.txt | 1 + 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/python/lib/api/client.py b/python/lib/api/client.py index cda93fbed..0a552ae92 100644 --- a/python/lib/api/client.py +++ b/python/lib/api/client.py @@ -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'] @@ -43,7 +44,7 @@ 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, ): @@ -51,6 +52,9 @@ def post( '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}', diff --git a/python/lib/api/endpoints/dicom.py b/python/lib/api/endpoints/dicom.py index 92a463d12..909540786 100644 --- a/python/lib/api/endpoints/dicom.py +++ b/python/lib/api/endpoints/dicom.py @@ -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 @@ -19,7 +21,7 @@ def post_candidate_dicom( overwrite: bool, file_path: str, ): - data = { + multipart = MultipartEncoder(fields={ 'Json': json.dumps({ 'CandID': cand_id, 'PSCID': psc_id, @@ -27,13 +29,10 @@ def post_candidate_dicom( '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'] diff --git a/python/requirements.txt b/python/requirements.txt index fded4e993..1dc864707 100644 --- a/python/requirements.txt +++ b/python/requirements.txt @@ -16,6 +16,7 @@ pyright pytest python-dateutil requests +requests_toolbelt ruff scikit-learn scipy