annotate Resources/Samples/Python/RestToolbox.py @ 1125:20ddab7e9ae8

IStorageArea abstraction
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 05 Sep 2014 16:28:47 +0200
parents 44382c8bcd15
children 4e9d517503ae
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
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 424
diff changeset
2 # Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 424
diff changeset
3 # Belgium
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
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 import httplib2
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 import json
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 from urllib import urlencode
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
23 _credentials = None
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
24
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
25 def SetCredentials(username, password):
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
26 global _credentials
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
27 _credentials = (username, password)
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
28
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
29 def _SetupCredentials(h):
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
30 global _credentials
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
31 if _credentials != None:
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
32 h.add_credentials(_credentials[0], _credentials[1])
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
33
424
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 354
diff changeset
34 def DoGet(uri, data = {}, interpretAsJson = True):
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 d = ''
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 if len(data.keys()) > 0:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 d = '?' + urlencode(data)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 h = httplib2.Http()
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
40 _SetupCredentials(h)
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 resp, content = h.request(uri + d, 'GET')
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 if not (resp.status in [ 200 ]):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 raise Exception(resp.status)
424
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 354
diff changeset
44 elif not interpretAsJson:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 354
diff changeset
45 return content
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 else:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 try:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 return json.loads(content)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 except:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 return content
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 def _DoPutOrPost(uri, method, data, contentType):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 h = httplib2.Http()
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
55 _SetupCredentials(h)
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 if isinstance(data, str):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 body = data
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 if len(contentType) != 0:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 headers = { 'content-type' : contentType }
354
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 346
diff changeset
61 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 346
diff changeset
62 headers = { 'content-type' : 'text/plain' }
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 else:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 body = json.dumps(data)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 headers = { 'content-type' : 'application/json' }
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 resp, content = h.request(
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 uri, method,
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 body = body,
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 headers = headers)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 if not (resp.status in [ 200, 302 ]):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 raise Exception(resp.status)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 else:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 try:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 return json.loads(content)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 except:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 return content
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 def DoDelete(uri):
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 resp, content = h.request(uri, 'DELETE')
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
85
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 if not (resp.status in [ 200 ]):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 raise Exception(resp.status)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 else:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 try:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 return json.loads(content)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 except:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 return content
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
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 def DoPut(uri, data = {}, contentType = ''):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 return _DoPutOrPost(uri, 'PUT', data, contentType)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 def DoPost(uri, data = {}, contentType = ''):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 return _DoPutOrPost(uri, 'POST', data, contentType)