annotate Resources/Samples/Python/RestToolbox.py @ 3860:9ccbbd55bc23

allow Keeping the SOPInstanceUID in /instances/../modify
author Alain Mazy <alain@mazy.be>
date Fri, 24 Apr 2020 17:30:41 +0200
parents 94f4a18a79cc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
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
3640
94f4a18a79cc upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
4 # Copyright (C) 2017-2020 Osimis S.A., Belgium
747
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 424
diff changeset
5 #
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 424
diff changeset
6 # 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
7 # 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
8 # 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
9 # 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
10 #
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 424
diff changeset
11 # 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
12 # 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
13 # 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
14 # General Public License for more details.
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 424
diff changeset
15 #
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 424
diff changeset
16 # 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
17 # 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
18
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 424
diff changeset
19
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 import httplib2
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 import json
1184
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
22 import sys
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
23
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
24 if (sys.version_info >= (3, 0)):
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
25 from urllib.parse import urlencode
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
26 else:
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
27 from urllib import urlencode
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
28
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
30 _credentials = None
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
31
1184
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
32
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
33 def _DecodeJson(s):
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
34 try:
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
35 if (sys.version_info >= (3, 0)):
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
36 return json.loads(s.decode())
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
37 else:
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
38 return json.loads(s)
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
39 except:
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
40 return s
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
41
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
42
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
43 def SetCredentials(username, password):
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
44 global _credentials
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
45 _credentials = (username, password)
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
46
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
47 def _SetupCredentials(h):
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
48 global _credentials
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
49 if _credentials != None:
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
50 h.add_credentials(_credentials[0], _credentials[1])
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
51
2795
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
52 def _ComputeGetUri(uri, data):
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 d = ''
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 if len(data.keys()) > 0:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 d = '?' + urlencode(data)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
2795
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
57 return uri + d
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
58
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
59 def DoGet(uri, data = {}, interpretAsJson = True):
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 h = httplib2.Http()
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
61 _SetupCredentials(h)
2795
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
62 resp, content = h.request(_ComputeGetUri(uri, data), 'GET')
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 if not (resp.status in [ 200 ]):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 raise Exception(resp.status)
424
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 354
diff changeset
65 elif not interpretAsJson:
1184
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
66 return content.decode()
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 else:
1184
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
68 return _DecodeJson(content)
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
2795
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
71 def DoRawGet(uri, data = {}):
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
72 h = httplib2.Http()
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
73 _SetupCredentials(h)
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
74 resp, content = h.request(_ComputeGetUri(uri, data), 'GET')
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
75 if not (resp.status in [ 200 ]):
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
76 raise Exception(resp.status)
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
77 else:
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
78 return content
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
79
f2e49b953e86 RestToolbox.DoRawGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
80
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 def _DoPutOrPost(uri, method, data, contentType):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 h = httplib2.Http()
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
83 _SetupCredentials(h)
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 if isinstance(data, str):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 body = data
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 if len(contentType) != 0:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 headers = { 'content-type' : contentType }
354
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 346
diff changeset
89 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 346
diff changeset
90 headers = { 'content-type' : 'text/plain' }
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 else:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 body = json.dumps(data)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 headers = { 'content-type' : 'application/json' }
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 resp, content = h.request(
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 uri, method,
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 body = body,
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 headers = headers)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 if not (resp.status in [ 200, 302 ]):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 raise Exception(resp.status)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 else:
1184
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
103 return _DecodeJson(content)
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 def DoDelete(uri):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 h = httplib2.Http()
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
108 _SetupCredentials(h)
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 resp, content = h.request(uri, 'DELETE')
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
110
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 if not (resp.status in [ 200 ]):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 raise Exception(resp.status)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 else:
1184
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
114 return _DecodeJson(content)
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 def DoPut(uri, data = {}, contentType = ''):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 return _DoPutOrPost(uri, 'PUT', data, contentType)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 def DoPost(uri, data = {}, contentType = ''):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 return _DoPutOrPost(uri, 'POST', data, contentType)