comparison Resources/Samples/Python/RestToolbox.py @ 2795:f2e49b953e86

RestToolbox.DoRawGet
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Jul 2018 16:15:23 +0200
parents 878b59270859
children 4e43e67f8ecf
comparison
equal deleted inserted replaced
2794:f790999a250a 2795:f2e49b953e86
47 def _SetupCredentials(h): 47 def _SetupCredentials(h):
48 global _credentials 48 global _credentials
49 if _credentials != None: 49 if _credentials != None:
50 h.add_credentials(_credentials[0], _credentials[1]) 50 h.add_credentials(_credentials[0], _credentials[1])
51 51
52 def DoGet(uri, data = {}, interpretAsJson = True): 52 def _ComputeGetUri(uri, data):
53 d = '' 53 d = ''
54 if len(data.keys()) > 0: 54 if len(data.keys()) > 0:
55 d = '?' + urlencode(data) 55 d = '?' + urlencode(data)
56 56
57 return uri + d
58
59 def DoGet(uri, data = {}, interpretAsJson = True):
57 h = httplib2.Http() 60 h = httplib2.Http()
58 _SetupCredentials(h) 61 _SetupCredentials(h)
59 resp, content = h.request(uri + d, 'GET') 62 resp, content = h.request(_ComputeGetUri(uri, data), 'GET')
60 if not (resp.status in [ 200 ]): 63 if not (resp.status in [ 200 ]):
61 raise Exception(resp.status) 64 raise Exception(resp.status)
62 elif not interpretAsJson: 65 elif not interpretAsJson:
63 return content.decode() 66 return content.decode()
64 else: 67 else:
65 return _DecodeJson(content) 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
66 79
67 80
68 def _DoPutOrPost(uri, method, data, contentType): 81 def _DoPutOrPost(uri, method, data, contentType):
69 h = httplib2.Http() 82 h = httplib2.Http()
70 _SetupCredentials(h) 83 _SetupCredentials(h)