|
1
|
|
|
import requests |
|
|
|
|
|
|
2
|
|
|
import logging |
|
|
|
|
|
|
3
|
|
|
import os |
|
|
|
|
|
|
4
|
|
|
import sys |
|
|
|
|
|
|
5
|
|
|
from dotenv import load_dotenv |
|
6
|
|
|
|
|
7
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
|
8
|
|
|
logger = logging.getLogger('Orthanc Query:') |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
View Code Duplication |
def getOrthanc(endpoint): |
|
|
|
|
|
|
12
|
|
|
""" |
|
13
|
|
|
Get from a Orthanc endpoint |
|
14
|
|
|
:param endpoint: |
|
15
|
|
|
:return: bool on if such PSCID (INSTITUTIONID + PROJECTID + SUBJECTID) exist already. |
|
16
|
|
|
""" |
|
17
|
|
|
logger = logging.getLogger('Orthanc_get') |
|
|
|
|
|
|
18
|
|
|
logger.info("Getting Orthanc endpoint: "+ endpoint + "at") |
|
19
|
|
|
load_dotenv() |
|
20
|
|
|
url = os.getenv("OrthancURL") |
|
21
|
|
|
updatedurl = url + endpoint |
|
22
|
|
|
logger.info(updatedurl) |
|
23
|
|
|
|
|
24
|
|
|
with requests.Session() as s: |
|
|
|
|
|
|
25
|
|
|
r = s.get(updatedurl) |
|
|
|
|
|
|
26
|
|
|
logger.info("Get Result:" + str(r.status_code) + r.reason) |
|
27
|
|
|
|
|
28
|
|
|
return r.status_code, r.json() |
|
29
|
|
|
|
|
30
|
|
View Code Duplication |
def postOrthanc(endpoint, data): |
|
|
|
|
|
|
31
|
|
|
""" |
|
32
|
|
|
Get from a Orthanc endpoint |
|
33
|
|
|
:param endpoint: |
|
34
|
|
|
:return: bool on if such PSCID (INSTITUTIONID + PROJECTID + SUBJECTID) exist already. |
|
35
|
|
|
""" |
|
36
|
|
|
logger = logging.getLogger('Orthanc_post') |
|
|
|
|
|
|
37
|
|
|
logger.info("Post Orthanc endpoint: "+ endpoint + "at") |
|
38
|
|
|
load_dotenv() |
|
39
|
|
|
url = os.getenv("OrthancURL") |
|
40
|
|
|
updatedurl = url + endpoint |
|
41
|
|
|
logger.info(updatedurl) |
|
42
|
|
|
|
|
43
|
|
|
with requests.Session() as s: |
|
|
|
|
|
|
44
|
|
|
r = s.post(updatedurl, files=data) |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
logger.info("Post Result:" + str(r.status_code) + r.reason) |
|
47
|
|
|
|
|
48
|
|
|
return r.status_code, r |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
def deleteOrthanc(endpoint): |
|
|
|
|
|
|
52
|
|
|
""" |
|
53
|
|
|
Delete from a Orthanc endpoint |
|
54
|
|
|
:param endpoint: |
|
55
|
|
|
:return: bool on if such PSCID (INSTITUTIONID + PROJECTID + SUBJECTID) exist already. |
|
56
|
|
|
""" |
|
57
|
|
|
logger = logging.getLogger('Orthanc_delete') |
|
|
|
|
|
|
58
|
|
|
logger.info("Deleting Orthanc endpoint: "+ endpoint + "at") |
|
59
|
|
|
load_dotenv() |
|
60
|
|
|
url = os.getenv("OrthancURL") |
|
61
|
|
|
updatedurl = url + endpoint |
|
62
|
|
|
logger.info(updatedurl) |
|
63
|
|
|
|
|
64
|
|
|
with requests.Session() as s: |
|
|
|
|
|
|
65
|
|
|
r = s.delete(updatedurl) |
|
|
|
|
|
|
66
|
|
|
logger.info("Deletion Result:" + str(r.status_code) + r.reason) |
|
67
|
|
|
|
|
68
|
|
|
return r.status_code, r.json() |
|
69
|
|
|
|
|
70
|
|
|
""" |
|
71
|
|
|
def zipOrthanc(endpoint): |
|
72
|
|
|
query = orthanc_url + '/studies/' + orthanc_study_id + '/archive' |
|
73
|
|
|
response_study = requests.get(query, verify=False, \ |
|
74
|
|
|
auth=(orthanc_user, orthanc_password)) |
|
75
|
|
|
if response_study.status_code != 200: |
|
76
|
|
|
print |
|
77
|
|
|
'Problem retrieving study: ' + orthanc_study_id |
|
78
|
|
|
print |
|
79
|
|
|
response_study.status, response_study.reason |
|
80
|
|
|
continue |
|
81
|
|
|
print |
|
82
|
|
|
' Retrieved: %s' % orthanc_study_id |
|
83
|
|
|
zip_content = response_study.content |
|
84
|
|
|
|
|
85
|
|
|
file_like_object = io.BytesIO(zip_content) |
|
86
|
|
|
zip_object = zipfile.ZipFile(file_like_object) |
|
87
|
|
|
for zip_name in zip_object.namelist(): |
|
88
|
|
|
zip_object.extract(zip_name, some_destination_dir) |
|
89
|
|
|
""" |
|
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.