Skip to content

Commit

Permalink
Manually construct URL query strings
Browse files Browse the repository at this point in the history
This has the advantage that the full URL will be printed to the console,
which makes it easier to understand QIDO-RS search requests.
  • Loading branch information
hackermd committed Jul 6, 2018
1 parent 093f897 commit 2ffe9e1
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions src/dicomweb_client/api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
'''Active Programming Interface (API).'''
import re
import os
import sys
import logging
import email
import six
from urllib3.filepost import choose_boundary
from io import BytesIO
from collections import OrderedDict
if sys.version_info.major < 3:
from urllib import quote_plus
else:
from urllib.parse import quote_plus

import requests
import pydicom
Expand Down Expand Up @@ -316,6 +321,19 @@ def _get_instances_url(self, study_instance_uid=None,
service=self.base_url, sop_instance_uid=sop_instance_uid
)

@staticmethod
def _build_query_string(params):
components = []
for key, value in params.items():
if isinstance(value, (list, tuple, set)):
for v in value:
c = '='.join([key, quote_plus(str(v))])
components.append(c)
else:
c = '='.join([key, quote_plus(str(value))])
components.append(c)
return '?{}'.format('&'.join(components))

def _http_get(self, url, params, headers):
'''Performs a HTTP GET request.
Expand All @@ -334,8 +352,9 @@ def _http_get(self, url, params, headers):
HTTP response message
'''
url += self._build_query_string(params)
logger.debug('GET: {}'.format(url))
resp = self._session.get(url=url, params=params, headers=headers)
resp = self._session.get(url=url, headers=headers)
resp.raise_for_status()
return resp

Expand Down Expand Up @@ -554,13 +573,15 @@ def _http_post(self, url, data, headers):
response.raise_for_status()
return response

def _http_post_multipart_application_dicom(self, url, datasets):
def _http_post_multipart_application_dicom(self, url, data):
'''Performs a HTTP POST request
Parameters
----------
url: str
unique resource locator
data: List[bytes]
DICOM data sets that should be posted
datasets: List[pydicom.dataset.Dataset]
DICOM data sets that should be posted
Expand All @@ -570,14 +591,8 @@ def _http_post_multipart_application_dicom(self, url, datasets):
'type="application/dicom"; '
'boundary="boundary"'
)
encoded_datasets = list()
# TODO: can we do this more memory efficient? Concatenations?
for ds in datasets:
with BytesIO() as b:
pydicom.dcmwrite(b, ds)
encoded_datasets.append(b.getvalue())
data = self._encode_multipart_message(encoded_datasets, content_type)
self._http_post(url, data, headers={'Content-Type': content_type})
content = self._encode_multipart_message(data, content_type)
self._http_post(url, content, headers={'Content-Type': content_type})

def search_for_studies(self, fuzzymatching=None, limit=None, offset=None,
fields=None, search_filters={}):
Expand Down Expand Up @@ -869,7 +884,13 @@ def store_instances(self, datasets, study_instance_uid=None):
'''
url = self._get_studies_url(study_instance_uid)
self._http_post_multipart_application_dicom(url, datasets)
encoded_datasets = list()
# TODO: can we do this more memory efficient? Concatenations?
for ds in datasets:
with BytesIO() as b:
pydicom.dcmwrite(b, ds)
encoded_datasets.append(b.getvalue())
self._http_post_multipart_application_dicom(url, encoded_datasets)

def retrieve_instance_metadata(self, study_instance_uid,
series_instance_uid, sop_instance_uid):
Expand Down

0 comments on commit 2ffe9e1

Please sign in to comment.