├── .gitignore ├── README.md ├── tcia-rest-client-java ├── .DS_Store ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── README.md ├── pom.xml ├── src │ ├── main │ │ └── java │ │ │ └── edu │ │ │ └── emory │ │ │ └── cci │ │ │ └── tcia │ │ │ └── client │ │ │ ├── DICOMAttributes.java │ │ │ ├── ITCIAClient.java │ │ │ ├── OutputFormat.java │ │ │ ├── TCIAClientException.java │ │ │ ├── TCIAClientImpl.java │ │ │ └── WebClientDevWrapper.java │ └── test │ │ └── java │ │ └── edu │ │ └── emory │ │ └── cci │ │ └── tcia │ │ └── client │ │ └── test │ │ └── TestTCIAClient.java └── target │ └── classes │ └── META-INF │ ├── MANIFEST.MF │ └── maven │ └── tcia │ └── tcia-client │ ├── pom.properties │ └── pom.xml └── tcia-rest-client-python ├── .project ├── .pydevproject ├── README.md └── src ├── sample.py └── tciaclient.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 98 | __pypackages__/ 99 | 100 | # Celery stuff 101 | celerybeat-schedule 102 | celerybeat.pid 103 | 104 | # SageMath parsed files 105 | *.sage.py 106 | 107 | # Environments 108 | .env 109 | .venv 110 | env/ 111 | venv/ 112 | ENV/ 113 | env.bak/ 114 | venv.bak/ 115 | 116 | # Spyder project settings 117 | .spyderproject 118 | .spyproject 119 | 120 | # Rope project settings 121 | .ropeproject 122 | 123 | # mkdocs documentation 124 | /site 125 | 126 | # mypy 127 | .mypy_cache/ 128 | .dmypy.json 129 | dmypy.json 130 | 131 | # Pyre type checker 132 | .pyre/ 133 | 134 | # pytype static type analyzer 135 | .pytype/ 136 | 137 | # Cython debug symbols 138 | cython_debug/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A SDK for the REST API of the Cancer Imaging Archive 2 | ==================== 3 | 4 | This repository contains code sample/library that can be used to access TCIA REST API securely. It shows how an "API-Key" can be embedded in the RESTfull call. 5 | Python & Java SDK has been provided. 6 | -------------------------------------------------------------------------------- /tcia-rest-client-java/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TCIA-Community/TCIA-API-SDK/730f26e0b8cd4434efba5ad1b3fbdad6b10523fd/tcia-rest-client-java/.DS_Store -------------------------------------------------------------------------------- /tcia-rest-client-java/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /tcia-rest-client-java/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /tcia-rest-client-java/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | tcia-rest-client-java 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /tcia-rest-client-java/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /tcia-rest-client-java/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /tcia-rest-client-java/README.md: -------------------------------------------------------------------------------- 1 | Java Client 2 | ================================================ 3 | This project has a dependency on Apache HttpClient. 4 | For more information visit http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e43 5 | 6 | * Import tcia-rest-client-java as a maven project in Eclipse IDE 7 | * Test cases can be found under tcia-rest-client-java/src/test/java 8 | 9 | 10 | -------------------------------------------------------------------------------- /tcia-rest-client-java/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | tcia 5 | tcia-client 6 | 0.0.1-SNAPSHOT 7 | Bindaas Client 8 | Java Wrapper for making secure Bindaas RESTful calls 9 | 10 | 11 | 12 | org.apache.httpcomponents 13 | httpclient 14 | 4.5.13 15 | 16 | 17 | junit 18 | junit 19 | 4.13.1 20 | test 21 | 22 | 23 | com.google.code.gson 24 | gson 25 | 2.8.9 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /tcia-rest-client-java/src/main/java/edu/emory/cci/tcia/client/DICOMAttributes.java: -------------------------------------------------------------------------------- 1 | package edu.emory.cci.tcia.client; 2 | 3 | public class DICOMAttributes { 4 | public static final String COLLECTION = "Collection"; 5 | public static final String PATIENT_ID = "PatientID"; 6 | public static final String STUDY_INSTANCE_UID = "StudyInstanceUID"; 7 | public static final String SERIES_INSTANCE_UID = "SeriesInstanceUID"; 8 | public static final String MODALITY = "Modality"; 9 | public static final String BODY_PART_EXAMINED = "BodyPartExamined"; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /tcia-rest-client-java/src/main/java/edu/emory/cci/tcia/client/ITCIAClient.java: -------------------------------------------------------------------------------- 1 | package edu.emory.cci.tcia.client; 2 | 3 | import java.io.InputStream; 4 | 5 | 6 | public interface ITCIAClient { 7 | public String getModalityValues(String collection,String bodyPartExamined,String modality , OutputFormat format) throws TCIAClientException; 8 | public String getManufacturerValues(String collection,String bodyPartExamined,String modality, OutputFormat format) throws TCIAClientException; 9 | public String getCollectionValues(OutputFormat format) throws TCIAClientException; 10 | public String getBodyPartValues(String collection,String bodyPartExamined,String modality, OutputFormat format) throws TCIAClientException; 11 | public String getPatientStudy(String collection,String patientID , String studyInstanceUID, OutputFormat format) throws TCIAClientException; 12 | public String getSeries(String collection,String modality,String studyInstanceUID, OutputFormat format) throws TCIAClientException; 13 | public String getPatient(String collection, OutputFormat format) throws TCIAClientException; 14 | public ImageResult getImage(String seriesInstanceUID) throws TCIAClientException; 15 | public String getSharedList(String name, OutputFormat json) throws TCIAClientException; 16 | 17 | public static class ImageResult { 18 | private InputStream rawData; 19 | private Integer imageCount; 20 | public InputStream getRawData() { 21 | return rawData; 22 | } 23 | public void setRawData(InputStream rawData) { 24 | this.rawData = rawData; 25 | } 26 | public Integer getImageCount() { 27 | return imageCount; 28 | } 29 | public void setImageCount(Integer imageCount) { 30 | this.imageCount = imageCount; 31 | } 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /tcia-rest-client-java/src/main/java/edu/emory/cci/tcia/client/OutputFormat.java: -------------------------------------------------------------------------------- 1 | package edu.emory.cci.tcia.client; 2 | 3 | public enum OutputFormat { 4 | xml,csv,json 5 | } 6 | -------------------------------------------------------------------------------- /tcia-rest-client-java/src/main/java/edu/emory/cci/tcia/client/TCIAClientException.java: -------------------------------------------------------------------------------- 1 | package edu.emory.cci.tcia.client; 2 | 3 | public class TCIAClientException extends Exception { 4 | 5 | private static final long serialVersionUID = -2548423882689490254L; 6 | private String requestUrl; 7 | 8 | 9 | public TCIAClientException(String requestUrl) { 10 | super(); 11 | this.requestUrl = requestUrl; 12 | } 13 | 14 | public TCIAClientException(String message, Throwable cause , String requestUrl) { 15 | super(message, cause); 16 | this.requestUrl = requestUrl; 17 | } 18 | 19 | public TCIAClientException(String message, String requestUrl) { 20 | super(message); 21 | this.requestUrl = requestUrl; 22 | } 23 | 24 | public TCIAClientException(Throwable cause , String requestUrl) { 25 | super(cause); 26 | this.requestUrl = requestUrl; 27 | } 28 | 29 | public String toString() 30 | { 31 | return String.format("Request URL =[%s]\t%s", this.requestUrl , super.toString()); 32 | } 33 | 34 | @Override 35 | public String getMessage() { 36 | 37 | return String.format("Request URL =[%s]\t%s", this.requestUrl , super.getMessage()); 38 | } 39 | 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /tcia-rest-client-java/src/main/java/edu/emory/cci/tcia/client/TCIAClientImpl.java: -------------------------------------------------------------------------------- 1 | package edu.emory.cci.tcia.client; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.net.URI; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.HttpResponse; 9 | import org.apache.http.client.ClientProtocolException; 10 | import org.apache.http.client.methods.HttpGet; 11 | import org.apache.http.client.utils.URIBuilder; 12 | import org.apache.http.impl.client.DefaultHttpClient; 13 | 14 | public class TCIAClientImpl implements ITCIAClient{ 15 | 16 | private final static String API_KEY_FIELD = "api_key"; 17 | private String apiKey; 18 | private DefaultHttpClient httpClient ; 19 | private String baseUrl; 20 | private static String getImage = "/query/getImage"; 21 | private static String getManufacturerValues = "/query/getManufacturerValues"; 22 | private static String getModalityValues = "/query/getModalityValues"; 23 | private static String getCollectionValues = "/query/getCollectionValues"; 24 | private static String getBodyPartValues = "/query/getBodyPartValues"; 25 | private static String getPatientStudy = "/query/getPatientStudy"; 26 | private static String getSeries = "/query/getSeries"; 27 | private static String getPatient = "/query/getPatient"; 28 | private static String getSharedList = "/query/ContentsByName"; 29 | 30 | 31 | public TCIAClientImpl(String apiKey , String baseUrl) 32 | { 33 | this.apiKey = apiKey; 34 | this.baseUrl = baseUrl; 35 | httpClient = new DefaultHttpClient(); 36 | httpClient = WebClientDevWrapper.wrapClient(httpClient); 37 | } 38 | 39 | static String convertStreamToString(java.io.InputStream is) { 40 | java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); 41 | return s.hasNext() ? s.next() : ""; 42 | } 43 | 44 | public String getModalityValues(String collection, String bodyPartExamined, 45 | String modality, OutputFormat format) throws TCIAClientException { 46 | try { 47 | URI baseUri = new URI(baseUrl); 48 | URIBuilder uriBuilder = new URIBuilder( baseUri.toString() + getModalityValues); 49 | 50 | if(collection!=null) 51 | uriBuilder.addParameter(DICOMAttributes.COLLECTION, collection); 52 | 53 | if(bodyPartExamined!=null) 54 | uriBuilder.addParameter(DICOMAttributes.BODY_PART_EXAMINED, bodyPartExamined); 55 | 56 | if(modality!=null) 57 | uriBuilder.addParameter(DICOMAttributes.MODALITY, modality); 58 | 59 | uriBuilder.addParameter("format", format.name()); 60 | uriBuilder.addParameter(API_KEY_FIELD, apiKey); 61 | 62 | URI uri = uriBuilder.build(); 63 | InputStream is = getRawData(uri); 64 | return convertStreamToString(is); 65 | 66 | } 67 | catch (TCIAClientException e) { 68 | throw e; 69 | } 70 | catch (Exception e) { 71 | throw new TCIAClientException( e , baseUrl); 72 | } 73 | 74 | } 75 | 76 | private InputStream getRawData(URI uri) throws TCIAClientException, ClientProtocolException, IOException 77 | { 78 | // create a new HttpGet request 79 | HttpGet request = new HttpGet(uri); 80 | 81 | // add api_key to the header 82 | request.setHeader(API_KEY_FIELD, apiKey); 83 | HttpResponse response = httpClient.execute(request); 84 | if (response.getStatusLine().getStatusCode() != 200) // TCIA Server 85 | // error 86 | { 87 | if (response.getStatusLine().getStatusCode() == 401) // Unauthorized 88 | { 89 | throw new TCIAClientException(uri.toString(), 90 | "Unauthorized access"); 91 | } else if (response.getStatusLine().getStatusCode() == 404) { 92 | throw new TCIAClientException(uri.toString(), 93 | "Resource not found"); 94 | } else { 95 | throw new TCIAClientException(uri.toString(), "Server Error : " 96 | + response.getStatusLine().getReasonPhrase()); 97 | } 98 | 99 | } else { 100 | HttpEntity entity = response.getEntity(); 101 | if (entity != null && entity.getContent() != null) { 102 | return entity.getContent(); 103 | } else { 104 | throw new TCIAClientException(baseUrl, "No Content"); 105 | } 106 | } 107 | } 108 | 109 | public String getManufacturerValues(String collection, 110 | String bodyPartExamined, String modality, OutputFormat format) 111 | throws TCIAClientException { 112 | try { 113 | URI baseUri = new URI(baseUrl); 114 | URIBuilder uriBuilder = new URIBuilder( baseUri.toString() + getManufacturerValues); 115 | 116 | if(collection!=null) 117 | uriBuilder.addParameter(DICOMAttributes.COLLECTION, collection); 118 | 119 | if(bodyPartExamined!=null) 120 | uriBuilder.addParameter(DICOMAttributes.BODY_PART_EXAMINED, bodyPartExamined); 121 | 122 | if(modality!=null) 123 | uriBuilder.addParameter(DICOMAttributes.MODALITY, modality); 124 | 125 | uriBuilder.addParameter("format", format.name()); 126 | uriBuilder.addParameter(API_KEY_FIELD, apiKey); 127 | 128 | URI uri = uriBuilder.build(); 129 | InputStream is = getRawData(uri); 130 | return convertStreamToString(is); 131 | 132 | } 133 | catch (TCIAClientException e) { 134 | throw e; 135 | } 136 | catch (Exception e) { 137 | throw new TCIAClientException( e , baseUrl); 138 | } 139 | } 140 | public String getCollectionValues(OutputFormat format) 141 | throws TCIAClientException { 142 | try { 143 | URI baseUri = new URI(baseUrl); 144 | URIBuilder uriBuilder = new URIBuilder( baseUri.toString() + getCollectionValues); 145 | uriBuilder.addParameter("format", format.name()); 146 | uriBuilder.addParameter(API_KEY_FIELD, apiKey); 147 | 148 | URI uri = uriBuilder.build(); 149 | InputStream is = getRawData(uri); 150 | return convertStreamToString(is); 151 | 152 | } 153 | catch (TCIAClientException e) { 154 | throw e; 155 | } 156 | catch (Exception e) { 157 | throw new TCIAClientException( e , baseUrl); 158 | } 159 | } 160 | public String getBodyPartValues(String collection, String bodyPartExamined, 161 | String modality, OutputFormat format) throws TCIAClientException { 162 | try { 163 | URI baseUri = new URI(baseUrl); 164 | URIBuilder uriBuilder = new URIBuilder( baseUri.toString() + getBodyPartValues); 165 | 166 | if(collection!=null) 167 | uriBuilder.addParameter(DICOMAttributes.COLLECTION, collection); 168 | 169 | if(bodyPartExamined!=null) 170 | uriBuilder.addParameter(DICOMAttributes.BODY_PART_EXAMINED, bodyPartExamined); 171 | 172 | if(modality!=null) 173 | uriBuilder.addParameter(DICOMAttributes.MODALITY, modality); 174 | 175 | uriBuilder.addParameter("format", format.name()); 176 | uriBuilder.addParameter(API_KEY_FIELD, apiKey); 177 | 178 | URI uri = uriBuilder.build(); 179 | InputStream is = getRawData(uri); 180 | return convertStreamToString(is); 181 | 182 | } 183 | catch (TCIAClientException e) { 184 | throw e; 185 | } 186 | catch (Exception e) { 187 | throw new TCIAClientException( e , baseUrl); 188 | } 189 | 190 | } 191 | public String getPatientStudy(String collection, String patientID, 192 | String studyInstanceUID, OutputFormat format) 193 | throws TCIAClientException { 194 | try { 195 | URI baseUri = new URI(baseUrl); 196 | URIBuilder uriBuilder = new URIBuilder( baseUri.toString() + getPatientStudy); 197 | 198 | if(collection!=null) 199 | uriBuilder.addParameter(DICOMAttributes.COLLECTION, collection); 200 | 201 | if(patientID!=null) 202 | uriBuilder.addParameter(DICOMAttributes.PATIENT_ID, patientID); 203 | 204 | if(studyInstanceUID!=null) 205 | uriBuilder.addParameter(DICOMAttributes.STUDY_INSTANCE_UID, studyInstanceUID); 206 | 207 | uriBuilder.addParameter("format", format.name()); 208 | uriBuilder.addParameter(API_KEY_FIELD, apiKey); 209 | 210 | URI uri = uriBuilder.build(); 211 | InputStream is = getRawData(uri); 212 | return convertStreamToString(is); 213 | 214 | } 215 | catch (TCIAClientException e) { 216 | throw e; 217 | } 218 | catch (Exception e) { 219 | throw new TCIAClientException( e , baseUrl); 220 | } 221 | } 222 | 223 | public String getSeries(String collection, String modality, 224 | String studyInstanceUID, OutputFormat format) 225 | throws TCIAClientException { 226 | try { 227 | URI baseUri = new URI(baseUrl); 228 | URIBuilder uriBuilder = new URIBuilder( baseUri.toString() + getSeries); 229 | 230 | if(collection!=null) 231 | uriBuilder.addParameter(DICOMAttributes.COLLECTION, collection); 232 | 233 | if(modality!=null) 234 | uriBuilder.addParameter(DICOMAttributes.MODALITY, modality); 235 | 236 | if(studyInstanceUID!=null) 237 | uriBuilder.addParameter(DICOMAttributes.STUDY_INSTANCE_UID, studyInstanceUID); 238 | 239 | uriBuilder.addParameter("format", format.name()); 240 | uriBuilder.addParameter(API_KEY_FIELD, apiKey); 241 | 242 | URI uri = uriBuilder.build(); 243 | InputStream is = getRawData(uri); 244 | return convertStreamToString(is); 245 | 246 | } 247 | catch (TCIAClientException e) { 248 | throw e; 249 | } 250 | catch (Exception e) { 251 | throw new TCIAClientException( e , baseUrl); 252 | } 253 | } 254 | public String getPatient(String collection, OutputFormat format) 255 | throws TCIAClientException { 256 | try { 257 | URI baseUri = new URI(baseUrl); 258 | URIBuilder uriBuilder = new URIBuilder( baseUri.toString() + getPatient); 259 | 260 | if(collection!=null) 261 | uriBuilder.addParameter(DICOMAttributes.COLLECTION, collection); 262 | 263 | uriBuilder.addParameter("format", format.name()); 264 | uriBuilder.addParameter(API_KEY_FIELD, apiKey); 265 | 266 | URI uri = uriBuilder.build(); 267 | InputStream is = getRawData(uri); 268 | return convertStreamToString(is); 269 | 270 | } 271 | catch (TCIAClientException e) { 272 | throw e; 273 | } 274 | catch (Exception e) { 275 | throw new TCIAClientException( e , baseUrl); 276 | } 277 | } 278 | 279 | public String getSharedList(String name, OutputFormat format) 280 | throws TCIAClientException { 281 | try { 282 | URI baseUri = new URI(baseUrl); 283 | URIBuilder uriBuilder = new URIBuilder( baseUri.toString() + getSharedList); 284 | 285 | if(name!=null) 286 | uriBuilder.addParameter("name", name); 287 | 288 | uriBuilder.addParameter("format", format.name()); 289 | uriBuilder.addParameter(API_KEY_FIELD, apiKey); 290 | 291 | 292 | URI uri = uriBuilder.build(); 293 | InputStream is = getRawData(uri); 294 | return convertStreamToString(is); 295 | 296 | } 297 | catch (TCIAClientException e) { 298 | throw e; 299 | } 300 | catch (Exception e) { 301 | throw new TCIAClientException( e , baseUrl); 302 | } 303 | } 304 | 305 | 306 | public ImageResult getImage(String seriesInstanceUID) 307 | throws TCIAClientException { 308 | try { 309 | URI baseUri = new URI(baseUrl); 310 | URIBuilder uriBuilder = new URIBuilder( baseUri.toString() + getImage); 311 | 312 | if(seriesInstanceUID!=null) 313 | uriBuilder.addParameter(DICOMAttributes.SERIES_INSTANCE_UID, seriesInstanceUID); 314 | 315 | uriBuilder.addParameter(API_KEY_FIELD, apiKey); 316 | 317 | URI uri = uriBuilder.build(); 318 | // create a new HttpGet request 319 | HttpGet request = new HttpGet(uri); 320 | 321 | long startTime = System.currentTimeMillis(); 322 | HttpResponse response = httpClient.execute(request); 323 | long diff = System.currentTimeMillis() - startTime; 324 | 325 | System.out.println("Server Response Received in " + diff + " ms"); 326 | if (response.getStatusLine().getStatusCode() != 200) // TCIA Server 327 | // error 328 | { 329 | if (response.getStatusLine().getStatusCode() == 401) // Unauthorized 330 | { 331 | throw new TCIAClientException(uri.toString(), 332 | "Unauthorized access"); 333 | } else if (response.getStatusLine().getStatusCode() == 404) { 334 | throw new TCIAClientException(uri.toString(), 335 | "Resource not found"); 336 | } else { 337 | throw new TCIAClientException(uri.toString(), "Server Error : " 338 | + response.getStatusLine().getReasonPhrase()); 339 | } 340 | 341 | } else { 342 | HttpEntity entity = response.getEntity(); 343 | if (entity != null && entity.getContent() != null) { 344 | ImageResult imageResult = new ImageResult(); 345 | imageResult.setRawData(entity.getContent()); 346 | imageResult.setImageCount(Integer.parseInt(response.getFirstHeader("imageCount").getValue())); 347 | return imageResult; 348 | } else { 349 | throw new TCIAClientException(baseUrl, "No Content"); 350 | } 351 | } 352 | 353 | } 354 | catch (TCIAClientException e) { 355 | throw e; 356 | } 357 | catch (Exception e) { 358 | throw new TCIAClientException( e , baseUrl); 359 | } 360 | } 361 | 362 | 363 | 364 | } 365 | -------------------------------------------------------------------------------- /tcia-rest-client-java/src/main/java/edu/emory/cci/tcia/client/WebClientDevWrapper.java: -------------------------------------------------------------------------------- 1 | package edu.emory.cci.tcia.client; 2 | import java.security.cert.CertificateException; 3 | import java.security.cert.X509Certificate; 4 | import javax.net.ssl.SSLContext; 5 | import javax.net.ssl.TrustManager; 6 | import javax.net.ssl.X509TrustManager; 7 | import org.apache.http.client.HttpClient; 8 | import org.apache.http.conn.ClientConnectionManager; 9 | import org.apache.http.conn.scheme.Scheme; 10 | import org.apache.http.conn.scheme.SchemeRegistry; 11 | import org.apache.http.conn.ssl.SSLSocketFactory; 12 | import org.apache.http.impl.client.DefaultHttpClient; 13 | public class WebClientDevWrapper { 14 | 15 | public static DefaultHttpClient wrapClient(HttpClient base) { 16 | try { 17 | SSLContext ctx = SSLContext.getInstance("TLS"); 18 | X509TrustManager tm = new X509TrustManager() { 19 | 20 | public void checkClientTrusted(X509Certificate[] xcs, 21 | String string) throws CertificateException { 22 | } 23 | 24 | public void checkServerTrusted(X509Certificate[] xcs, 25 | String string) throws CertificateException { 26 | } 27 | 28 | public X509Certificate[] getAcceptedIssuers() { 29 | return null; 30 | } 31 | }; 32 | ctx.init(null, new TrustManager[] { tm }, null); 33 | SSLSocketFactory ssf = new SSLSocketFactory(ctx); 34 | ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 35 | ClientConnectionManager ccm = base.getConnectionManager(); 36 | SchemeRegistry sr = ccm.getSchemeRegistry(); 37 | sr.register(new Scheme("https", ssf, 443)); 38 | return new DefaultHttpClient(ccm, base.getParams()); 39 | } catch (Exception ex) { 40 | ex.printStackTrace(); 41 | return null; 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /tcia-rest-client-java/src/test/java/edu/emory/cci/tcia/client/test/TestTCIAClient.java: -------------------------------------------------------------------------------- 1 | package edu.emory.cci.tcia.client.test; 2 | 3 | import static org.junit.Assert.fail; 4 | 5 | import java.io.ByteArrayOutputStream; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | 10 | import org.junit.Test; 11 | 12 | import edu.emory.cci.tcia.client.ITCIAClient; 13 | import edu.emory.cci.tcia.client.ITCIAClient.ImageResult; 14 | import edu.emory.cci.tcia.client.OutputFormat; 15 | import edu.emory.cci.tcia.client.TCIAClientException; 16 | import edu.emory.cci.tcia.client.TCIAClientImpl; 17 | 18 | 19 | 20 | 21 | /** 22 | * Refer https://wiki.cancerimagingarchive.net/display/Public/REST+API+Usage+Guide for complete list of API 23 | */ 24 | public class TestTCIAClient { 25 | 26 | private static String baseUrl = "https://services.cancerimagingarchive.net/services/v2"; // Base URL of the service 27 | private static String resourceTCIA = "/TCIA"; 28 | private static String resourceSharedList = "/SharedList"; 29 | 30 | //NOTE: Insert your API Key here 31 | private static String apiKey = ""; 32 | 33 | /** 34 | * Method : GetCollectionValues 35 | * Description : Returns set of all collection values 36 | */ 37 | 38 | @Test 39 | public void testGetCollectionValues() 40 | { 41 | // create TCIA Client by passing API-Key and baseUrl in the constructor 42 | ITCIAClient client = new TCIAClientImpl(apiKey , baseUrl+resourceTCIA); 43 | 44 | try { 45 | // Make the RESTfull call . Response comes back as InputStream. 46 | String respXML = client.getCollectionValues(OutputFormat.xml); 47 | String respJSON = client.getCollectionValues(OutputFormat.json); 48 | String respCSV = client.getCollectionValues(OutputFormat.csv); 49 | 50 | // Print server response 51 | System.out.println(respXML); 52 | System.out.println(respJSON); 53 | System.out.println(respCSV); 54 | 55 | } catch (TCIAClientException e) { 56 | fail(e.getMessage()); // request failed 57 | } catch (Exception e) { 58 | fail(e.getMessage()); // request failed 59 | } 60 | } 61 | 62 | /** 63 | * Method : GetImage 64 | * Description : Returns images in a zip file 65 | */ 66 | 67 | @Test 68 | public void testGetImage() 69 | { 70 | 71 | // create TCIA Client by passing API-Key and baseUrl in the constructor 72 | ITCIAClient client = new TCIAClientImpl(apiKey , baseUrl+resourceTCIA); 73 | String seriesInstanceUID = "1.3.6.1.4.1.14519.5.2.1.7695.4001.306204232344341694648035234440"; 74 | try { 75 | // Make the RESTfull call . Response comes back as InputStream. 76 | ImageResult imageResult = client.getImage(seriesInstanceUID); 77 | double averageDICOMFileSize = 200 * 1024d ; // 200KB 78 | double compressionRatio = 0.75 ; // approx 79 | int estimatedFileSize = (int) (averageDICOMFileSize * compressionRatio * imageResult.getImageCount()); 80 | saveTo(imageResult.getRawData(), seriesInstanceUID + ".zip", ".", estimatedFileSize); 81 | 82 | } catch (TCIAClientException e) { 83 | fail(e.getMessage()); // request failed 84 | } catch (Exception e) { 85 | fail(e.getMessage()); // request failed 86 | } 87 | 88 | } 89 | 90 | 91 | @Test 92 | public void testGetSeries() 93 | { 94 | // create TCIA Client by passing API-Key and baseUrl in the constructor 95 | ITCIAClient client = new TCIAClientImpl(apiKey , baseUrl+resourceTCIA); 96 | String collection = "TCGA-GBM"; // optional 97 | String modality = "MR"; // optional 98 | String studyInstanceUID = null; // optional 99 | 100 | try { 101 | // Make the RESTfull call . Response comes back as InputStream. 102 | String respJSON = client.getSeries(collection, modality, studyInstanceUID, OutputFormat.json); 103 | 104 | 105 | // Print server response 106 | System.out.println(respJSON); 107 | 108 | 109 | } catch (TCIAClientException e) { 110 | fail(e.getMessage()); // request failed 111 | } catch (Exception e) { 112 | fail(e.getMessage()); // request failed 113 | } 114 | 115 | } 116 | 117 | 118 | @Test 119 | public void testGetPatientStudy() 120 | { 121 | // create TCIA Client by passing API-Key and baseUrl in the constructor 122 | ITCIAClient client = new TCIAClientImpl(apiKey , baseUrl+resourceTCIA); 123 | String collection = "TCGA-GBM"; // optional 124 | String patientID = null; // optional 125 | String studyInstanceUID = null; // optional 126 | 127 | try { 128 | // Make the RESTfull call . Response comes back as InputStream. 129 | String respJSON = client.getPatientStudy(collection, patientID, studyInstanceUID, OutputFormat.json); 130 | 131 | // Print server response 132 | System.out.println(respJSON); 133 | 134 | 135 | } catch (TCIAClientException e) { 136 | fail(e.getMessage()); // request failed 137 | } catch (Exception e) { 138 | fail(e.getMessage()); // request failed 139 | } 140 | 141 | } 142 | 143 | @Test 144 | public void testGetPatient() 145 | { 146 | // create TCIA Client by passing API-Key and baseUrl in the constructor 147 | ITCIAClient client = new TCIAClientImpl(apiKey , baseUrl+resourceTCIA); 148 | String collection = "TCGA-GBM"; // optional 149 | 150 | try { 151 | // Make the RESTfull call . Response comes back as InputStream. 152 | String respJSON = client.getPatient(collection , OutputFormat.json); 153 | 154 | // Print server response 155 | System.out.println(respJSON); 156 | 157 | 158 | } catch (TCIAClientException e) { 159 | fail(e.getMessage()); // request failed 160 | } catch (Exception e) { 161 | fail(e.getMessage()); // request failed 162 | } 163 | 164 | } 165 | 166 | @Test 167 | public void testGetBodyPartValues() 168 | { 169 | // create TCIA Client by passing API-Key and baseUrl in the constructor 170 | ITCIAClient client = new TCIAClientImpl(apiKey , baseUrl+resourceTCIA); 171 | String collection = null ; // optional 172 | String bodyPartExamined = null; // optional 173 | String modality = "MR"; // optional 174 | 175 | try { 176 | // Make the RESTfull call . Response comes back as InputStream. 177 | String respJSON = client.getBodyPartValues(collection, bodyPartExamined, modality, OutputFormat.json); 178 | 179 | // Print server response 180 | System.out.println(respJSON); 181 | 182 | 183 | } catch (TCIAClientException e) { 184 | fail(e.getMessage()); // request failed 185 | } catch (Exception e) { 186 | fail(e.getMessage()); // request failed 187 | } 188 | 189 | } 190 | 191 | @Test 192 | public void testGetModalityValues() 193 | { 194 | // create TCIA Client by passing API-Key and baseUrl in the constructor 195 | ITCIAClient client = new TCIAClientImpl(apiKey , baseUrl+resourceTCIA); 196 | String collection = null ; // optional 197 | String bodyPartExamined = "BRAIN"; // optional 198 | String modality = "MR"; // optional 199 | 200 | try { 201 | // Make the RESTfull call . Response comes back as InputStream. 202 | String respJSON = client.getModalityValues(collection, bodyPartExamined, modality, OutputFormat.json); 203 | 204 | // Print server response 205 | System.out.println(respJSON); 206 | 207 | 208 | } catch (TCIAClientException e) { 209 | fail(e.getMessage()); // request failed 210 | } catch (Exception e) { 211 | fail(e.getMessage()); // request failed 212 | } 213 | 214 | } 215 | 216 | @Test 217 | public void testGetSharedList() 218 | { 219 | ITCIAClient client = new TCIAClientImpl(apiKey , baseUrl+resourceSharedList); 220 | String name = "sharedListApiUnitTest"; 221 | try { 222 | // Make the RESTfull call . Response comes back as InputStream. 223 | String respJSON = client.getSharedList(name, OutputFormat.json); 224 | 225 | // Print server response 226 | System.out.println(respJSON); 227 | 228 | 229 | } catch (TCIAClientException e) { 230 | fail(e.getMessage()); // request failed 231 | } catch (Exception e) { 232 | fail(e.getMessage()); // request failed 233 | } 234 | 235 | } 236 | 237 | @Test 238 | public void testGetManufacturerValues() 239 | { 240 | // create TCIA Client by passing API-Key and baseUrl in the constructor 241 | ITCIAClient client = new TCIAClientImpl(apiKey , baseUrl+resourceTCIA); 242 | String collection = null ; // optional 243 | String bodyPartExamined = "BRAIN"; // optional 244 | String modality = "MR"; // optional 245 | 246 | try { 247 | // Make the RESTfull call . Response comes back as InputStream. 248 | String respJSON = client.getManufacturerValues(collection, bodyPartExamined, modality, OutputFormat.json); 249 | 250 | // Print server response 251 | System.out.println(respJSON); 252 | 253 | 254 | } catch (TCIAClientException e) { 255 | fail(e.getMessage()); // request failed 256 | } catch (Exception e) { 257 | fail(e.getMessage()); // request failed 258 | } 259 | 260 | } 261 | 262 | 263 | 264 | 265 | 266 | public static void saveTo(InputStream in , String name , String directory , int estimatedBytes) throws IOException 267 | { 268 | FileOutputStream fos = new FileOutputStream(directory + "/" + name); 269 | byte[] buffer = new byte[4096]; 270 | int read = -1; 271 | int sum = 0; 272 | while((read = in.read(buffer)) > 0) 273 | { 274 | fos.write(buffer , 0 , read); 275 | long mseconds = System.currentTimeMillis(); 276 | sum += read; 277 | 278 | if(mseconds % 10 == 0) 279 | { 280 | System.out.println(String.format("Bytes Written %s out of estimated %s : " , sum , estimatedBytes)); 281 | } 282 | } 283 | 284 | fos.close(); 285 | in.close(); 286 | } 287 | public static String toString(InputStream in) throws IOException 288 | { 289 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 290 | byte[] buffer = new byte[2048]; 291 | int bytesRead = -1; 292 | 293 | while((bytesRead = in.read(buffer)) > 0) 294 | { 295 | baos.write(buffer, 0, bytesRead); 296 | } 297 | 298 | baos.close(); 299 | return (new String(baos.toByteArray())); 300 | } 301 | 302 | 303 | } 304 | -------------------------------------------------------------------------------- /tcia-rest-client-java/target/classes/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Built-By: ashish 3 | Build-Jdk: 1.7.0_67 4 | Created-By: Maven Integration for Eclipse 5 | 6 | -------------------------------------------------------------------------------- /tcia-rest-client-java/target/classes/META-INF/maven/tcia/tcia-client/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven Integration for Eclipse 2 | #Fri Oct 24 16:29:28 EDT 2014 3 | version=0.0.1-SNAPSHOT 4 | groupId=tcia 5 | m2e.projectName=tcia-rest-client-java 6 | m2e.projectLocation=/Users/ashish/Development/TCIA-Repos/tcia-rest-api-example/tcia-rest-client-java 7 | artifactId=tcia-client 8 | -------------------------------------------------------------------------------- /tcia-rest-client-java/target/classes/META-INF/maven/tcia/tcia-client/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | tcia 5 | tcia-client 6 | 0.0.1-SNAPSHOT 7 | Bindaas Client 8 | Java Wrapper for making secure Bindaas RESTful calls 9 | 10 | 11 | 12 | org.apache.httpcomponents 13 | httpclient 14 | 4.5.13 15 | 16 | 17 | junit 18 | junit 19 | 4.13.1 20 | test 21 | 22 | 23 | com.google.code.gson 24 | gson 25 | 2.8.9 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /tcia-rest-client-python/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | tcia-rest-client-python 4 | 5 | 6 | 7 | 8 | 9 | org.python.pydev.PyDevBuilder 10 | 11 | 12 | 13 | 14 | 15 | org.python.pydev.pythonNature 16 | 17 | 18 | -------------------------------------------------------------------------------- /tcia-rest-client-python/.pydevproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | /tcia-rest-client-python/src 7 | 8 | python 2.7 9 | python 10 | 11 | -------------------------------------------------------------------------------- /tcia-rest-client-python/README.md: -------------------------------------------------------------------------------- 1 | Python Client 2 | ================================================ 3 | 4 | * Requires Python 2.7.4 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /tcia-rest-client-python/src/sample.py: -------------------------------------------------------------------------------- 1 | from tciaclient import TCIAClient 2 | import urllib.request, urllib.error, urllib.parse, urllib.request, urllib.parse, urllib.error,sys 3 | #################################### Function to print server response ####### 4 | def printServerResponse(response): 5 | if response.getcode() == 200: 6 | print("Server Returned:\n") 7 | print(response.read()) 8 | print("\n") 9 | else: 10 | print("Error: " + str(response.getcode())) 11 | 12 | #################################### Create Clients for Two Different Resources #### 13 | tcia_client = TCIAClient(baseUrl="https://services.cancerimagingarchive.net/services/v4",resource = "TCIA") 14 | tcia_client2 = TCIAClient(baseUrl="https://services.cancerimagingarchive.net/services/v4",resource="SharedList") 15 | 16 | # Test content_by_name 17 | try: 18 | response = tcia_client2.contents_by_name(name = "sharedListApiUnitTest") 19 | printServerResponse(response); 20 | except urllib.error.HTTPError as err: 21 | print("Errror executing program:\nError Code: ", str(err.code), "\nMessage:", err.read()) 22 | 23 | # Test get_manufacturer_values no query parameters 24 | try: 25 | response = tcia_client.get_manufacturer_values(collection = 26 | None,bodyPartExamined = 27 | None,modality = 28 | None,outputFormat = "json") 29 | printServerResponse(response); 30 | 31 | except urllib.error.HTTPError as err: 32 | print("Errror executing program:\nError Code: ", str(err.code), "\nMessage:", err.read()) 33 | 34 | # Test get_series_size 35 | try: 36 | response = tcia_client.get_series_size(SeriesInstanceUID="1.3.6.1.4.1.14519.5.2.1.7695.4001.306204232344341694648035234440") 37 | printServerResponse(response); 38 | except urllib.error.HTTPError as err: 39 | print("Errror executing program:\nError Code: ", str(err.code), "\nMessage:", err.read()) 40 | 41 | # Test get_manufacturer_values with query Parameter 42 | try: 43 | response = tcia_client.get_manufacturer_values(collection = "TCGA-GBM",bodyPartExamined = None,modality = None, outputFormat = "csv") 44 | printServerResponse(response); 45 | 46 | except urllib.error.HTTPError as err: 47 | print("Errror executing program:\nError Code: ", str(err.code), "\nMessage:", err.read()) 48 | 49 | # Test get_image. 50 | # NOTE: Image response consumed differently 51 | tcia_client.get_image(seriesInstanceUid ="1.3.6.1.4.1.14519.5.2.1.7695.4001.306204232344341694648035234440" , downloadPath ="./", zipFileName ="images.zip"); 52 | -------------------------------------------------------------------------------- /tcia-rest-client-python/src/tciaclient.py: -------------------------------------------------------------------------------- 1 | import os 2 | import urllib.request, urllib.error, urllib.parse 3 | import urllib.request, urllib.parse, urllib.error 4 | import sys 5 | import math 6 | # 7 | # Refer https://wiki.cancerimagingarchive.net/display/Public/REST+API+Usage+Guide for complete list of API 8 | # 9 | class TCIAClient: 10 | GET_IMAGE = "getImage" 11 | GET_MANUFACTURER_VALUES = "getManufacturerValues" 12 | GET_MODALITY_VALUES = "getModalityValues" 13 | GET_COLLECTION_VALUES = "getCollectionValues" 14 | GET_BODY_PART_VALUES = "getBodyPartValues" 15 | GET_PATIENT_STUDY = "getPatientStudy" 16 | GET_SERIES = "getSeries" 17 | GET_PATIENT = "getPatient" 18 | GET_SERIES_SIZE = "getSeriesSize" 19 | CONTENTS_BY_NAME = "ContentsByName" 20 | 21 | def __init__(self, baseUrl, resource): 22 | self.baseUrl = baseUrl + "/" + resource 23 | 24 | def execute(self, url, queryParameters={}): 25 | queryParameters = dict((k, v) for k, v in queryParameters.items() if v) 26 | queryString = "?%s" % urllib.parse.urlencode(queryParameters) 27 | requestUrl = url + queryString 28 | request = urllib.request.Request(url=requestUrl) 29 | resp = urllib.request.urlopen(request) 30 | return resp 31 | 32 | def get_modality_values(self,collection = None , bodyPartExamined = None , modality = None , outputFormat = "json" ): 33 | serviceUrl = self.baseUrl + "/query/" + self.GET_MODALITY_VALUES 34 | queryParameters = {"Collection" : collection , "BodyPartExamined" : bodyPartExamined , "Modality" : modality , "format" : outputFormat } 35 | resp = self.execute(serviceUrl , queryParameters) 36 | return resp 37 | 38 | def get_series_size(self, SeriesInstanceUID = None, outputFormat = "json"): 39 | serviceUrl = self.baseUrl + "/query/" + self.GET_SERIES_SIZE 40 | queryParameters = {"SeriesInstanceUID" : SeriesInstanceUID, "format" : 41 | outputFormat} 42 | resp = self.execute(serviceUrl, queryParameters) 43 | return resp 44 | 45 | def contents_by_name(self, name = None): 46 | serviceUrl = self.baseUrl + "/query/" + self.CONTENTS_BY_NAME 47 | queryParameters = {"name" : name} 48 | print(serviceUrl) 49 | resp = self.execute(serviceUrl,queryParameters) 50 | return resp 51 | 52 | def get_manufacturer_values(self,collection = None , bodyPartExamined = None, modality = None , outputFormat = "json"): 53 | serviceUrl = self.baseUrl + "/query/" + self.GET_MANUFACTURER_VALUES 54 | queryParameters = {"Collection" : collection , "BodyPartExamined" : bodyPartExamined , "Modality" : modality , "format" : outputFormat } 55 | resp = self.execute(serviceUrl , queryParameters) 56 | return resp 57 | 58 | def get_collection_values(self,outputFormat = "json" ): 59 | serviceUrl = self.baseUrl + "/query/" + self.GET_COLLECTION_VALUES 60 | queryParameters = { "format" : outputFormat } 61 | resp = self.execute(serviceUrl , queryParameters) 62 | return resp 63 | 64 | def get_body_part_values(self,collection = None , bodyPartExamined = None , modality = None , outputFormat = "json" ): 65 | serviceUrl = self.baseUrl + "/query/" + self.GET_BODY_PART_VALUES 66 | queryParameters = {"Collection" : collection , "BodyPartExamined" : bodyPartExamined , "Modality" : modality , "format" : outputFormat } 67 | resp = self.execute(serviceUrl , queryParameters) 68 | return resp 69 | 70 | def get_patient_study(self,collection = None , patientId = None , studyInstanceUid = None , outputFormat = "json" ): 71 | serviceUrl = self.baseUrl + "/query/" + self.GET_PATIENT_STUDY 72 | queryParameters = {"Collection" : collection , "PatientID" : patientId , "StudyInstanceUID" : studyInstanceUid , "format" : outputFormat } 73 | resp = self.execute(serviceUrl , queryParameters) 74 | return resp 75 | 76 | def get_series(self, collection = None , modality = None , studyInstanceUid = None , outputFormat = "json" ): 77 | serviceUrl = self.baseUrl + "/query/" + self.GET_SERIES 78 | queryParameters = {"Collection" : collection , "StudyInstanceUID" : studyInstanceUid , "Modality" : modality , "format" : outputFormat } 79 | resp = self.execute(serviceUrl , queryParameters) 80 | return resp 81 | 82 | def get_patient(self,collection = None , outputFormat = "json" ): 83 | serviceUrl = self.baseUrl + "/query/" + self.GET_PATIENT 84 | queryParameters = {"Collection" : collection , "format" : outputFormat } 85 | resp = self.execute(serviceUrl , queryParameters) 86 | return resp 87 | 88 | def get_image(self , seriesInstanceUid , downloadPath, zipFileName): 89 | serviceUrl = self.baseUrl + "/query/" + self.GET_IMAGE 90 | queryParameters = { "SeriesInstanceUID" : seriesInstanceUid } 91 | os.umask(0o002) 92 | try: 93 | file = os.path.join(downloadPath, zipFileName) 94 | resp = self.execute( serviceUrl , queryParameters) 95 | downloaded = 0 96 | CHUNK = 256 * 10240 97 | with open(file, 'wb') as fp: 98 | while True: 99 | chunk = resp.read(CHUNK) 100 | downloaded += len(chunk) 101 | if not chunk: break 102 | fp.write(chunk) 103 | except urllib.error.HTTPError as e: 104 | print("HTTP Error:",e.code , serviceUrl) 105 | return False 106 | except urllib.error.URLError as e: 107 | print("URL Error:",e.reason , serviceUrl) 108 | return False 109 | 110 | return True --------------------------------------------------------------------------------