-
Notifications
You must be signed in to change notification settings - Fork 101
/
client.py
296 lines (256 loc) · 9.69 KB
/
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
"""HTTP Client library"""
import json
import logging
from .exceptions import handle_error
try:
# Python 3
import urllib.request as urllib
from urllib.parse import urlencode
from urllib.error import HTTPError
except ImportError:
# Python 2
import urllib2 as urllib
from urllib2 import HTTPError
from urllib import urlencode
_logger = logging.getLogger(__name__)
class Response(object):
"""Holds the response from an API call."""
def __init__(self, response):
"""
:param response: The return value from a open call
on a urllib.build_opener()
:type response: urllib response object
"""
self._status_code = response.getcode()
self._body = response.read()
self._headers = response.info()
@property
def status_code(self):
"""
:return: integer, status code of API call
"""
return self._status_code
@property
def body(self):
"""
:return: response from the API
"""
return self._body
@property
def headers(self):
"""
:return: dict of response headers
"""
return self._headers
@property
def to_dict(self):
"""
:return: dict of response from the API
"""
if self.body:
return json.loads(self.body.decode('utf-8'))
else:
return None
class Client(object):
"""Quickly and easily access any REST or REST-like API."""
# These are the supported HTTP verbs
methods = {'delete', 'get', 'patch', 'post', 'put'}
def __init__(self,
host,
request_headers=None,
version=None,
url_path=None,
append_slash=False,
timeout=None):
"""
:param host: Base URL for the api. (e.g. https://api.sendgrid.com)
:type host: string
:param request_headers: A dictionary of the headers you want
applied on all calls
:type request_headers: dictionary
:param version: The version number of the API.
Subclass _build_versioned_url for custom behavior.
Or just pass the version as part of the URL
(e.g. client._("/v3"))
:type version: integer
:param url_path: A list of the url path segments
:type url_path: list of strings
"""
self.host = host
self.request_headers = request_headers or {}
self._version = version
# _url_path keeps track of the dynamically built url
self._url_path = url_path or []
# APPEND SLASH set
self.append_slash = append_slash
self.timeout = timeout
def _build_versioned_url(self, url):
"""Subclass this function for your own needs.
Or just pass the version as part of the URL
(e.g. client._('/v3'))
:param url: URI portion of the full URL being requested
:type url: string
:return: string
"""
return '{}/v{}{}'.format(self.host, str(self._version), url)
def _build_url(self, query_params):
"""Build the final URL to be passed to urllib
:param query_params: A dictionary of all the query parameters
:type query_params: dictionary
:return: string
"""
url = ''
count = 0
while count < len(self._url_path):
url += '/{}'.format(self._url_path[count])
count += 1
# add slash
if self.append_slash:
url += '/'
if query_params:
url_values = urlencode(sorted(query_params.items()), True)
url = '{}?{}'.format(url, url_values)
if self._version:
url = self._build_versioned_url(url)
else:
url = '{}{}'.format(self.host, url)
return url
def _update_headers(self, request_headers):
"""Update the headers for the request
:param request_headers: headers to set for the API call
:type request_headers: dictionary
:return: dictionary
"""
self.request_headers.update(request_headers)
def _build_client(self, name=None):
"""Make a new Client object
:param name: Name of the url segment
:type name: string
:return: A Client object
"""
url_path = self._url_path + [name] if name else self._url_path
return Client(host=self.host,
version=self._version,
request_headers=self.request_headers,
url_path=url_path,
append_slash=self.append_slash,
timeout=self.timeout)
def _make_request(self, opener, request, timeout=None):
"""Make the API call and return the response. This is separated into
it's own function, so we can mock it easily for testing.
:param opener:
:type opener:
:param request: url payload to request
:type request: urllib.Request object
:param timeout: timeout value or None
:type timeout: float
:return: urllib response
"""
timeout = timeout or self.timeout
try:
return opener.open(request, timeout=timeout)
except HTTPError as err:
exc = handle_error(err)
exc.__cause__ = None
_logger.debug('{method} Response: {status} {body}'.format(
method=request.get_method(),
status=exc.status_code,
body=exc.body))
raise exc
def _(self, name):
"""Add variable values to the url.
(e.g. /your/api/{variable_value}/call)
Another example: if you have a Python reserved word, such as global,
in your url, you must use this method.
:param name: Name of the url segment
:type name: string
:return: Client object
"""
return self._build_client(name)
def __getattr__(self, name):
"""Dynamically add method calls to the url, then call a method.
(e.g. client.name.name.method())
You can also add a version number by using .version(<int>)
:param name: Name of the url segment or method call
:type name: string or integer if name == version
:return: mixed
"""
if name == 'version':
def get_version(*args, **kwargs):
"""
:param args: dict of settings
:param kwargs: unused
:return: string, version
"""
self._version = args[0]
return self._build_client()
return get_version
# We have reached the end of the method chain, make the API call
if name in self.methods:
method = name.upper()
def http_request(
request_body=None,
query_params=None,
request_headers=None,
timeout=None,
**_):
"""Make the API call
:param timeout: HTTP request timeout. Will be propagated to
urllib client
:type timeout: float
:param request_headers: HTTP headers. Will be merged into
current client object state
:type request_headers: dict
:param query_params: HTTP query parameters
:type query_params: dict
:param request_body: HTTP request body
:type request_body: string or json-serializable object
:param kwargs:
:return: Response object
"""
if request_headers:
self._update_headers(request_headers)
if request_body is None:
data = None
else:
# Don't serialize to a JSON formatted str
# if we don't have a JSON Content-Type
if 'Content-Type' in self.request_headers and \
self.request_headers['Content-Type'] != \
'application/json':
data = request_body.encode('utf-8')
else:
self.request_headers.setdefault(
'Content-Type', 'application/json')
data = json.dumps(request_body).encode('utf-8')
opener = urllib.build_opener()
request = urllib.Request(
self._build_url(query_params),
headers=self.request_headers,
data=data,
)
request.get_method = lambda: method
_logger.debug('{method} Request: {url}'.format(
method=method,
url=request.get_full_url()))
if request.data:
_logger.debug('PAYLOAD: {data}'.format(
data=request.data))
_logger.debug('HEADERS: {headers}'.format(
headers=request.headers))
response = Response(
self._make_request(opener, request, timeout=timeout)
)
_logger.debug('{method} Response: {status} {body}'.format(
method=method,
status=response.status_code,
body=response.body))
return response
return http_request
else:
# Add a segment to the URL
return self._(name)
def __getstate__(self):
return self.__dict__
def __setstate__(self, state):
self.__dict__ = state