Mercurial > hg > orthanc
annotate Resources/Samples/Python/RestToolbox.py @ 2586:ec09641d6f41 jobs
simplifications
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 14 May 2018 21:33:57 +0200 |
parents | 878b59270859 |
children | f2e49b953e86 |
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 |
2447
878b59270859
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
4 # Copyright (C) 2017-2018 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 | 20 import httplib2 |
21 import json | |
1184 | 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 | |
340 | 29 |
346 | 30 _credentials = None |
31 | |
1184 | 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 | |
346 | 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 | |
424 | 52 def DoGet(uri, data = {}, interpretAsJson = True): |
340 | 53 d = '' |
54 if len(data.keys()) > 0: | |
55 d = '?' + urlencode(data) | |
56 | |
57 h = httplib2.Http() | |
346 | 58 _SetupCredentials(h) |
340 | 59 resp, content = h.request(uri + d, 'GET') |
60 if not (resp.status in [ 200 ]): | |
61 raise Exception(resp.status) | |
424 | 62 elif not interpretAsJson: |
1184 | 63 return content.decode() |
340 | 64 else: |
1184 | 65 return _DecodeJson(content) |
340 | 66 |
67 | |
68 def _DoPutOrPost(uri, method, data, contentType): | |
69 h = httplib2.Http() | |
346 | 70 _SetupCredentials(h) |
340 | 71 |
72 if isinstance(data, str): | |
73 body = data | |
74 if len(contentType) != 0: | |
75 headers = { 'content-type' : contentType } | |
354 | 76 else: |
77 headers = { 'content-type' : 'text/plain' } | |
340 | 78 else: |
79 body = json.dumps(data) | |
80 headers = { 'content-type' : 'application/json' } | |
81 | |
82 resp, content = h.request( | |
83 uri, method, | |
84 body = body, | |
85 headers = headers) | |
86 | |
87 if not (resp.status in [ 200, 302 ]): | |
88 raise Exception(resp.status) | |
89 else: | |
1184 | 90 return _DecodeJson(content) |
340 | 91 |
92 | |
93 def DoDelete(uri): | |
94 h = httplib2.Http() | |
346 | 95 _SetupCredentials(h) |
340 | 96 resp, content = h.request(uri, 'DELETE') |
346 | 97 |
340 | 98 if not (resp.status in [ 200 ]): |
99 raise Exception(resp.status) | |
100 else: | |
1184 | 101 return _DecodeJson(content) |
340 | 102 |
103 | |
104 def DoPut(uri, data = {}, contentType = ''): | |
105 return _DoPutOrPost(uri, 'PUT', data, contentType) | |
106 | |
107 | |
108 def DoPost(uri, data = {}, contentType = ''): | |
109 return _DoPutOrPost(uri, 'POST', data, contentType) |