forked from maximkulkin/http-test-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test_client.py
245 lines (188 loc) · 7.41 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
from collections import namedtuple
import json
import logging
import requests
import urllib
import six
import six.moves
class ClientError(Exception):
def __init__(self, status_code, message):
if not message:
message = 'Status code %d' % status_code
else:
message += ' (status code %d)' % status_code
super(ClientError, self).__init__(message)
self.status_code = status_code
Response = namedtuple('Response', ['status_code', 'headers', 'text'])
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):
if response.status_code == 404:
return None
else:
raise ClientError(response.status_code, response.text)
return json.loads(response.text) if len(response.text) > 0 else None
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 Api(object):
'''
Base class for all HTTP APIs. Maintains a client and base url.
'''
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 __repr__(self):
return '<%s %s>' % (self.__class__.__name__, 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 = RestResource(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._client.request(self._url, **kwargs)
def create(self, data, **kwargs):
result = self._client.request(self._url, method='POST', data=data, **kwargs)
if isinstance(result, dict) and 'id' in result:
id = result['id']
self._client.add_cleanup(
self._url + '/' + urllib.quote(id), self[id].delete,
)
return result
def __getitem__(self, id):
return self.Resource(
self._client, self._url + '/' + urllib.quote(id),
parent_ids=getattr(self, 'parent_ids', []) + [id],
)
class Resource(Api):
def get(self, **kwargs):
return self._client.request(self._url, **kwargs)
def update(self, data, **kwargs):
return self._client.request(self._url, method='PUT', data=data, **kwargs)
def delete(self, **kwargs):
response = self._client.raw_request(self._url, method='DELETE', **kwargs)
if response.status_code not in six.moves.range(200, 299) and \
response.status_code != 404:
raise ClientError(response.status_code, response.text)
self._client.remove_cleanup(self._url)
def api(path, klass):
'''
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
'''
assert issubclass(klass, Api), 'Resource klass should be subclass of Api'
def get(self):
return klass(self._client, self._url + path)
return property(get)
def resources(path, klass=RestResources):
'''Convenience synonym to `api()`'''
return api(path, klass)