-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_test_client.py
310 lines (234 loc) · 9.05 KB
/
http_test_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
from collections import namedtuple
import json
import logging
import re
import requests
import urllib
import six
import six.moves
class ClientError(Exception):
def __init__(self, status_code, text):
if not text:
message = 'Status code %d' % status_code
else:
message = '%s (status code %d)' % (text, status_code)
super(ClientError, self).__init__(message)
self.status_code = status_code
self.text = text
Response = namedtuple('Response', ['status_code', 'headers', 'text'])
PARAM_RE = re.compile(r'^/(?:[^{]+/)*\{(.+)\}$')
class ApiNamespace(object):
def __init__(self, path, klass):
self.path = path
m = PARAM_RE.match(path)
self.param = m.group(1) if m else None
self.klass = klass
def __get__(self, obj, objtype=None):
if obj is None:
return self
if self.param:
raise AttributeError()
return self.klass(obj._client, obj._url + self.path)
class DummyTransport(object):
'''
Transport that just prints executed requests
'''
def request(self, url, method=None, headers=None, data=None, **kwargs):
if data is not None:
print '%s %s data=%s' % (method, url, json.dumps(data))
else:
print '%s %s' % (method, url)
return Response(200, {}, '')
class HttpTransport(object):
'''
HTTP transport. Use base_url to specifying URL prefix (e.g. host/port)
'''
def __init__(self, base_url):
super(HttpTransport, self).__init__()
self._base_url = base_url
self._session = requests.Session()
def request(self, url, method=None, headers=None, data=None, **kwargs):
request = requests.Request(
url=self._base_url + url, method=method or 'GET',
headers=headers or {},
data=data,
**kwargs
)
return self._session.send(self._session.prepare_request(request))
class Client(object):
'''
Top level API client object. Holds reference to transport and base url.
Has cleanup API to allow resources to register cleanup callbacks.
'''
logger = logging.getLogger(__module__ + '.Client')
def __init__(self, transport, url=''):
"""
Args:
transport Instance HttpSession
url API URL to access a specific resource
"""
self._transport = transport
self._url = url
self._cleanup = []
self._client = self
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, self._url)
def add_cleanup(self, url, func):
'''Add cleanup callback associated with given URL'''
self._cleanup.append((url, func))
def remove_cleanup(self, url):
'''Remove cleanup callbacks associated with given URL'''
self._cleanup = [(url1, func) for (url1, func) in self._cleanup
if url1 != url and not url1.startswith(url + '/')]
def cleanup(self):
'''Execute all cleanup callbacks'''
for _, func in self._cleanup:
func()
self._cleanup = []
def raw_request(self, url, method=None, headers=None, data=None, **kwargs):
'''
Send HTTP request through configured transport and return Response object
'''
if method is None:
method = 'GET' if data is None else 'POST'
url = self._url + url
if data is not None:
data = json.dumps(data)
if headers is None:
headers = {}
headers['Content-Type'] = 'application/json'
self._log_request(url, method=method, headers=headers, data=data)
response = self._transport.request(
url=url, method=method, headers=headers, data=data, **kwargs
)
self._log_response(response)
return response
def request(self, url, method=None, headers=None, data=None, **kwargs):
'''Send HTTP request and return JSON response data'''
response = self.raw_request(
url, method=method, headers=headers, data=data, **kwargs
)
if response.status_code not in six.moves.range(200, 299):
raise ClientError(response.status_code, response.text)
return json.loads(response.text) if len(response.text) > 0 else None
# compatibility with Api
def _raw_request(self, *args, **kwargs):
return super(Client, self).raw_request(*args, **kwargs)
def _request(self, *args, **kwargs):
return super(Client, self).request(*args, **kwargs)
def _log_request(self, url, method, headers, data):
"""Log request"""
s = '%s request to %s' % (method, url)
if headers:
s += ' headers=%s' % repr(headers)
if data:
s += ' body=%s' % data
self.logger.debug('Sending %s', s)
def _log_response(self, response):
"""Log response"""
s = 'code=%d' % response.status_code
if response.headers:
s += ' headers=%s' % repr(response.headers)
if response.text:
s += ' body=%s' % response.text
self.logger.debug('Got response %s', s)
class ApiMeta(type):
def __new__(mcs, name, bases, attrs):
params = {attr_name: attr_value
for attr_name, attr_value in attrs.iteritems()
if isinstance(attr_value, ApiNamespace) and attr_value.param}
if len(params) > 1:
raise ValueError('Multiple param APIs are not supported')
elif len(params) == 1:
param_api = params.values()[0]
def getitem(self, param_value):
return param_api.klass(
self._client, self._url + '/' + urllib.quote(param_value),
)
attrs['__getitem__'] = getitem
return super(ApiMeta, mcs).__new__(mcs, name, bases, attrs)
class Api(object):
'''
Base class for all HTTP APIs. Maintains a client and base url.
'''
__metaclass__ = ApiMeta
def __init__(self, client, url, **kwargs):
"""
Args:
session Instance HttpSession
url API URL to access a specific resource
kwargs Additional properties to store
"""
self._client = client
self._url = url
for k, v in six.iteritems(kwargs):
setattr(self, k, v)
def _raw_request(self, url=None, *args, **kwargs):
return self._client.raw_request(self._url + (url or ''), *args, **kwargs)
def _request(self, url=None, *args, **kwargs):
return self._client.request(self._url + (url or ''), *args, **kwargs)
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, self._url)
def api(path, klass=None):
'''
Returns property that instantiates and returns specified Api class instance
passing it client and suburl, derived from self url and given path.
:param path: (string) URI path relative to parent resource
:param klass: (Api) Api class to instantiate
:return: Property descriptor
'''
def wrapper(klass):
if not issubclass(klass, Api):
klass = type(klass.__name__, (klass, Api), {})
return ApiNamespace(path, klass)
if klass:
return wrapper(klass)
return wrapper
class RestResource(Api):
def get(self, **kwargs):
try:
return self._request(method='GET', **kwargs)
except ClientError as ce:
if ce.status_code != 404:
raise ce
return None
def update(self, data, **kwargs):
return self._request(method='PUT', data=data, **kwargs)
def delete(self, **kwargs):
try:
response = self._request(method='DELETE', **kwargs)
except ClientError as ce:
if ce.status_code != 404:
raise ce
self._client.remove_cleanup(self._url)
class RestResources(Api):
'''
REST resources API implementing access to collection of resources.
Inner class "Resource" defines API for accessing particular resource.
Example:
users = RestResources(client, '/users')
users.list() # => [{'id': '1', 'name': 'John'}, ... ]
users.create({'name': 'Jane'}) # => {'id': '2'}
users['2'].get() # => {'id': '2', 'name': 'Jane'}
users['2'].update({'name': 'Jane Doe'})
users['2'].delete()
Uses Client's Cleanup API to register created resources for cleanup.
'''
def list(self, **kwargs):
return self._request(method='GET', **kwargs)
def create(self, data, **kwargs):
result = self._request(method='POST', data=data, **kwargs)
if isinstance(result, dict) and 'id' in result:
resource_api = self[result['id']]
self._client.add_cleanup(resource_api._url, resource_api.delete)
return result
@api('/{resource_id}')
class Resource(RestResource):
pass
def resources(path, klass=RestResources):
'''Convenience shortcut for defining REST resources API
Example:
class Client(http_test_client.Client):
users = resources('/users')
'''
return api(path, RestResources)