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