Mercurial > hg > orthanc
annotate Resources/Samples/Python/RestToolbox.py @ 1288:6e7e5ed91c2d
upgrade to year 2015
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 05 Feb 2015 09:39:55 +0100 |
parents | 4e9d517503ae |
children | b1291df2f780 |
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 |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1184
diff
changeset
|
2 # Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1184
diff
changeset
|
3 # Department, University Hospital of Liege, Belgium |
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
4 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
5 # 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
|
6 # 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
|
7 # 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
|
8 # 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
|
9 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
10 # 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
|
11 # 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
|
12 # 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
|
13 # General Public License for more details. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
14 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
15 # 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
|
16 # 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
|
17 |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
424
diff
changeset
|
18 |
340 | 19 import httplib2 |
20 import json | |
1184 | 21 import sys |
22 | |
23 if (sys.version_info >= (3, 0)): | |
24 from urllib.parse import urlencode | |
25 else: | |
26 from urllib import urlencode | |
27 | |
340 | 28 |
346 | 29 _credentials = None |
30 | |
1184 | 31 |
32 def _DecodeJson(s): | |
33 try: | |
34 if (sys.version_info >= (3, 0)): | |
35 return json.loads(s.decode()) | |
36 else: | |
37 return json.loads(s) | |
38 except: | |
39 return s | |
40 | |
41 | |
346 | 42 def SetCredentials(username, password): |
43 global _credentials | |
44 _credentials = (username, password) | |
45 | |
46 def _SetupCredentials(h): | |
47 global _credentials | |
48 if _credentials != None: | |
49 h.add_credentials(_credentials[0], _credentials[1]) | |
50 | |
424 | 51 def DoGet(uri, data = {}, interpretAsJson = True): |
340 | 52 d = '' |
53 if len(data.keys()) > 0: | |
54 d = '?' + urlencode(data) | |
55 | |
56 h = httplib2.Http() | |
346 | 57 _SetupCredentials(h) |
340 | 58 resp, content = h.request(uri + d, 'GET') |
59 if not (resp.status in [ 200 ]): | |
60 raise Exception(resp.status) | |
424 | 61 elif not interpretAsJson: |
1184 | 62 return content.decode() |
340 | 63 else: |
1184 | 64 return _DecodeJson(content) |
340 | 65 |
66 | |
67 def _DoPutOrPost(uri, method, data, contentType): | |
68 h = httplib2.Http() | |
346 | 69 _SetupCredentials(h) |
340 | 70 |
71 if isinstance(data, str): | |
72 body = data | |
73 if len(contentType) != 0: | |
74 headers = { 'content-type' : contentType } | |
354 | 75 else: |
76 headers = { 'content-type' : 'text/plain' } | |
340 | 77 else: |
78 body = json.dumps(data) | |
79 headers = { 'content-type' : 'application/json' } | |
80 | |
81 resp, content = h.request( | |
82 uri, method, | |
83 body = body, | |
84 headers = headers) | |
85 | |
86 if not (resp.status in [ 200, 302 ]): | |
87 raise Exception(resp.status) | |
88 else: | |
1184 | 89 return _DecodeJson(content) |
340 | 90 |
91 | |
92 def DoDelete(uri): | |
93 h = httplib2.Http() | |
346 | 94 _SetupCredentials(h) |
340 | 95 resp, content = h.request(uri, 'DELETE') |
346 | 96 |
340 | 97 if not (resp.status in [ 200 ]): |
98 raise Exception(resp.status) | |
99 else: | |
1184 | 100 return _DecodeJson(content) |
340 | 101 |
102 | |
103 def DoPut(uri, data = {}, contentType = ''): | |
104 return _DoPutOrPost(uri, 'PUT', data, contentType) | |
105 | |
106 | |
107 def DoPost(uri, data = {}, contentType = ''): | |
108 return _DoPutOrPost(uri, 'POST', data, contentType) |