Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/Python/RestToolbox.py @ 4910:92e84e9d2cca
Orthanc Explorer: sanitize DICOM tag display
author | Alain Mazy <am@osimis.io> |
---|---|
date | Mon, 21 Feb 2022 13:01:18 +0100 |
parents | 43e613a7756b |
children | 0ea402b4d901 |
rev | line source |
---|---|
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
1 # Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 2 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1184
diff
changeset
|
3 # Department, University Hospital of Liege, Belgium |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
4 # Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
5 # Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
6 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
7 # This program is free software: you can redistribute it and/or |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
8 # modify it under the terms of the GNU General Public License as |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
9 # published by the Free Software Foundation, either version 3 of the |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
10 # License, or (at your option) any later version. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
11 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, but |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
13 # WITHOUT ANY WARRANTY; without even the implied warranty of |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
15 # General Public License for more details. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
16 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
17 # You should have received a copy of the GNU General Public License |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
19 |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
20 |
340 | 21 import httplib2 |
22 import json | |
1184 | 23 import sys |
24 | |
25 if (sys.version_info >= (3, 0)): | |
26 from urllib.parse import urlencode | |
27 else: | |
28 from urllib import urlencode | |
29 | |
340 | 30 |
346 | 31 _credentials = None |
32 | |
1184 | 33 |
34 def _DecodeJson(s): | |
35 try: | |
36 if (sys.version_info >= (3, 0)): | |
37 return json.loads(s.decode()) | |
38 else: | |
39 return json.loads(s) | |
40 except: | |
41 return s | |
42 | |
43 | |
346 | 44 def SetCredentials(username, password): |
45 global _credentials | |
46 _credentials = (username, password) | |
47 | |
48 def _SetupCredentials(h): | |
49 global _credentials | |
50 if _credentials != None: | |
51 h.add_credentials(_credentials[0], _credentials[1]) | |
52 | |
2795
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
53 def _ComputeGetUri(uri, data): |
340 | 54 d = '' |
55 if len(data.keys()) > 0: | |
56 d = '?' + urlencode(data) | |
57 | |
2795
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
58 return uri + d |
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
59 |
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
60 def DoGet(uri, data = {}, interpretAsJson = True): |
340 | 61 h = httplib2.Http() |
346 | 62 _SetupCredentials(h) |
2795
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
63 resp, content = h.request(_ComputeGetUri(uri, data), 'GET') |
340 | 64 if not (resp.status in [ 200 ]): |
65 raise Exception(resp.status) | |
424 | 66 elif not interpretAsJson: |
1184 | 67 return content.decode() |
340 | 68 else: |
1184 | 69 return _DecodeJson(content) |
340 | 70 |
71 | |
2795
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
72 def DoRawGet(uri, data = {}): |
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
73 h = httplib2.Http() |
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
74 _SetupCredentials(h) |
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
75 resp, content = h.request(_ComputeGetUri(uri, data), 'GET') |
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
76 if not (resp.status in [ 200 ]): |
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
77 raise Exception(resp.status) |
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
78 else: |
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
79 return content |
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
80 |
f2e49b953e86
RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
81 |
340 | 82 def _DoPutOrPost(uri, method, data, contentType): |
83 h = httplib2.Http() | |
346 | 84 _SetupCredentials(h) |
340 | 85 |
86 if isinstance(data, str): | |
87 body = data | |
88 if len(contentType) != 0: | |
89 headers = { 'content-type' : contentType } | |
354 | 90 else: |
91 headers = { 'content-type' : 'text/plain' } | |
340 | 92 else: |
93 body = json.dumps(data) | |
94 headers = { 'content-type' : 'application/json' } | |
95 | |
96 resp, content = h.request( | |
97 uri, method, | |
98 body = body, | |
99 headers = headers) | |
100 | |
101 if not (resp.status in [ 200, 302 ]): | |
102 raise Exception(resp.status) | |
103 else: | |
1184 | 104 return _DecodeJson(content) |
340 | 105 |
106 | |
107 def DoDelete(uri): | |
108 h = httplib2.Http() | |
346 | 109 _SetupCredentials(h) |
340 | 110 resp, content = h.request(uri, 'DELETE') |
346 | 111 |
340 | 112 if not (resp.status in [ 200 ]): |
113 raise Exception(resp.status) | |
114 else: | |
1184 | 115 return _DecodeJson(content) |
340 | 116 |
117 | |
118 def DoPut(uri, data = {}, contentType = ''): | |
119 return _DoPutOrPost(uri, 'PUT', data, contentType) | |
120 | |
121 | |
122 def DoPost(uri, data = {}, contentType = ''): | |
123 return _DoPutOrPost(uri, 'POST', data, contentType) |