annotate Resources/Samples/Python/RestToolbox.py @ 711:8f62e8d5a384

test main dicom tags
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 13 Feb 2014 15:51:10 +0100
parents 76bc8770741b
children 44382c8bcd15
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 import httplib2
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 import json
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 from urllib import urlencode
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
5 _credentials = None
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
6
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
7 def SetCredentials(username, password):
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
8 global _credentials
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
9 _credentials = (username, password)
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
10
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
11 def _SetupCredentials(h):
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
12 global _credentials
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
13 if _credentials != None:
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
14 h.add_credentials(_credentials[0], _credentials[1])
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
15
424
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 354
diff changeset
16 def DoGet(uri, data = {}, interpretAsJson = True):
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 d = ''
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 if len(data.keys()) > 0:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 d = '?' + urlencode(data)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 h = httplib2.Http()
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
22 _SetupCredentials(h)
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 resp, content = h.request(uri + d, 'GET')
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 if not (resp.status in [ 200 ]):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 raise Exception(resp.status)
424
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 354
diff changeset
26 elif not interpretAsJson:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 354
diff changeset
27 return content
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 else:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 try:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 return json.loads(content)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 except:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 return content
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 def _DoPutOrPost(uri, method, data, contentType):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 h = httplib2.Http()
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
37 _SetupCredentials(h)
340
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 if isinstance(data, str):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 body = data
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 if len(contentType) != 0:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 headers = { 'content-type' : contentType }
354
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 346
diff changeset
43 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 346
diff changeset
44 headers = { 'content-type' : 'text/plain' }
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 else:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 body = json.dumps(data)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 headers = { 'content-type' : 'application/json' }
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 resp, content = h.request(
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 uri, method,
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 body = body,
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 headers = headers)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 if not (resp.status in [ 200, 302 ]):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 raise Exception(resp.status)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 else:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 try:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 return json.loads(content)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 except:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 return content
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 def DoDelete(uri):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 h = httplib2.Http()
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
65 _SetupCredentials(h)
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 resp, content = h.request(uri, 'DELETE')
346
107da6a8c630 credentials in python rest toolbox
jodogne
parents: 341
diff changeset
67
340
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 if not (resp.status in [ 200 ]):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 raise Exception(resp.status)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 else:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 try:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 return json.loads(content)
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 except:
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 return content
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 def DoPut(uri, data = {}, contentType = ''):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 return _DoPutOrPost(uri, 'PUT', data, contentType)
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 DoPost(uri, data = {}, contentType = ''):
61f6a3d66b85 changes loop sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 return _DoPutOrPost(uri, 'POST', data, contentType)