-
Notifications
You must be signed in to change notification settings - Fork 116
/
requests_client.py
447 lines (375 loc) · 15.1 KB
/
requests_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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# -*- coding: utf-8 -*-
import copy
import logging
import requests.auth
import requests.exceptions
import six
import typing
from bravado_core.operation import Operation
from bravado_core.response import IncomingResponse
from six import iteritems
from six.moves.urllib import parse as urlparse
from bravado._equality_util import are_objects_equal as _are_objects_equal
from bravado.config import RequestConfig
from bravado.http_client import HttpClient
from bravado.http_future import FutureAdapter
from bravado.http_future import HttpFuture
if getattr(typing, 'TYPE_CHECKING', False):
T = typing.TypeVar('T')
log = logging.getLogger(__name__)
class Authenticator(object):
"""Authenticates requests.
:param host: Host to authenticate for.
"""
def __init__(self, host):
# type: (str) -> None
self.host = host
def __repr__(self):
# type: () -> str
return "%s(%s)" % (self.__class__.__name__, self.host)
def matches(self, url):
# type: (typing.Text) -> bool
"""Returns true if this authenticator applies to the given url.
:param url: URL to check.
:return: True if matches host and port, False otherwise.
"""
split = urlparse.urlsplit(url)
return self.host == split.netloc
def apply(self, request):
# type: (requests.Request) -> requests.Request
"""Apply authentication to a request.
:param request: Request to add authentication information to.
"""
raise NotImplementedError(u"%s: Method not implemented",
self.__class__.__name__)
# noinspection PyDocstring
class ApiKeyAuthenticator(Authenticator):
"""?api_key authenticator.
This authenticator adds an API key via query parameter or header.
:param host: Host to authenticate for.
:param api_key: API key.
:param param_name: Query parameter specifying the API key.
:param param_in: How to send the API key. Can be 'query' or 'header'.
"""
def __init__(
self,
host, # type: str
api_key, # type: typing.Text
param_name=u'api_key', # type: typing.Text
param_in=u'query', # type: typing.Text
):
# type: (...) -> None
super(ApiKeyAuthenticator, self).__init__(host)
self.param_name = param_name
self.param_in = param_in
self.api_key = api_key
def apply(self, request):
# type: (requests.Request) -> requests.Request
if self.param_in == 'header':
request.headers.setdefault(self.param_name, self.api_key)
else:
request.params[self.param_name] = self.api_key
return request
class BasicAuthenticator(Authenticator):
"""HTTP Basic authenticator.
:param host: Host to authenticate for.
:param username: Username.
:param password: Password
"""
def __init__(
self,
host, # type: str
username, # type: typing.Union[bytes, str]
password, # type: typing.Union[bytes, str]
):
# type: (...) -> None
super(BasicAuthenticator, self).__init__(host)
self.auth = requests.auth.HTTPBasicAuth(username, password)
def apply(self, request):
# type: (requests.Request) -> requests.Request
request.auth = self.auth
return request
class RequestsResponseAdapter(IncomingResponse):
"""Wraps a requests.models.Response object to provide a uniform interface
to the response innards.
:type requests_lib_response: :class:`requests.models.Response`
"""
def __init__(self, requests_lib_response):
# type: (requests.Response) -> None
self._delegate = requests_lib_response
@property
def status_code(self):
# type: () -> int
return self._delegate.status_code
@property
def text(self):
# type: () -> typing.Text
return self._delegate.text
@property
def raw_bytes(self):
# type: () -> bytes
return self._delegate.content
@property
def reason(self):
# type: () -> typing.Text
return self._delegate.reason
@property
def headers(self):
# type: () -> typing.Mapping[typing.Text, typing.Text]
# we don't use typing.cast here since that's broken on Python 3.5.1
return self._delegate.headers # type: ignore
def json(self, **kwargs):
# type: (typing.Any) -> typing.Mapping[typing.Text, typing.Any]
return self._delegate.json(**kwargs)
class RequestsFutureAdapter(FutureAdapter):
"""Mimics a :class:`concurrent.futures.Future` for the purposes of making
HTTP calls with the Requests library in a future-y sort of way.
"""
timeout_errors = (requests.exceptions.ReadTimeout,) # type: typing.Tuple[typing.Type[Exception], ...]
connection_errors = (requests.exceptions.ConnectionError,) # type: typing.Tuple[typing.Type[Exception], ...]
def __init__(
self,
session, # type: requests.Session
request, # type: requests.Request
misc_options, # type: typing.Mapping[str, typing.Any]
):
# type: (...) -> None
"""Kicks API call for Requests client
:param session: session object to use for making the request
:param request: dict containing API request parameters
:param misc_options: misc options to apply when sending a HTTP request.
e.g. timeout, connect_timeout, etc
:type misc_options: dict
"""
self.session = session
self.request = request
self.misc_options = misc_options
def build_timeout(
self,
result_timeout, # type: typing.Optional[float]
):
# type: (...) -> typing.Union[typing.Optional[float], typing.Tuple[typing.Optional[float], typing.Optional[float]]] # noqa
"""
Build the appropriate timeout object to pass to `session.send(...)`
based on connect_timeout, the timeout passed to the service call, and
the timeout passed to the result call.
:param result_timeout: timeout that was passed into `future.result(..)`
:return: timeout
:rtype: float or tuple(connect_timeout, timeout)
"""
# The API provides two ways to pass a timeout :( We're stuck
# dealing with it until we're ready to make a non-backwards
# compatible change.
#
# - If both timeouts are the same, no problem
# - If either has a non-None value, use the non-None value
# - If both have a non-None value, use the greater of the two
timeout = None
has_service_timeout = 'timeout' in self.misc_options
service_timeout = self.misc_options.get('timeout')
if not has_service_timeout:
timeout = result_timeout
elif service_timeout == result_timeout:
timeout = service_timeout
else:
if service_timeout is None:
timeout = result_timeout
elif result_timeout is None:
timeout = service_timeout
else:
timeout = max(service_timeout, result_timeout)
log.warning(
"Two different timeouts have been passed: "
"_request_options['timeout'] = %s and "
"future.result(timeout=%s). Using timeout of %s.",
service_timeout, result_timeout, timeout,
)
# Requests is weird in that if you want to specify a connect_timeout
# and idle timeout, then the timeout is passed as a tuple
if 'connect_timeout' in self.misc_options:
return self.misc_options['connect_timeout'], timeout
return timeout
def result(self, timeout=None):
# type: (typing.Optional[float]) -> requests.Response
"""Blocking call to wait for API response
:param timeout: timeout in seconds to wait for response. Defaults to
None to wait indefinitely.
:type timeout: float
:return: raw response from the server
:rtype: dict
"""
request = self.request
# Ensure that all the headers are converted to strings.
# This is need to workaround https://github.com/requests/requests/issues/3491
request.headers = {
k: str(v) if not isinstance(v, six.binary_type) else v
for k, v in iteritems(request.headers)
}
prepared_request = self.session.prepare_request(request)
settings = self.session.merge_environment_settings(
prepared_request.url,
proxies={},
stream=None,
verify=self.misc_options['ssl_verify'],
cert=self.misc_options['ssl_cert'],
)
response = self.session.send(
prepared_request,
timeout=self.build_timeout(timeout),
allow_redirects=self.misc_options['follow_redirects'],
**settings
)
return response
def cancel(self):
# type: () -> None
pass
class RequestsClient(HttpClient):
"""Synchronous HTTP client implementation.
"""
def __init__(
self,
ssl_verify=True, # type: bool
ssl_cert=None, # type: typing.Any
future_adapter_class=RequestsFutureAdapter, # type: typing.Type[RequestsFutureAdapter]
response_adapter_class=RequestsResponseAdapter, # type: typing.Type[RequestsResponseAdapter]
):
# type: (...) -> None
"""
:param ssl_verify: Set to False to disable SSL certificate validation. Provide the path to a
CA bundle if you need to use a custom one.
:param ssl_cert: Provide a client-side certificate to use. Either a sequence of strings pointing
to the certificate (1) and the private key (2), or a string pointing to the combined certificate
and key.
:param future_adapter_class: Custom future adapter class,
should be a subclass of :class:`RequestsFutureAdapter`
:param response_adapter_class: Custom response adapter class,
should be a subclass of :class:`RequestsResponseAdapter`
"""
self.session = requests.Session()
self.authenticator = None # type: typing.Optional[Authenticator]
self.ssl_verify = ssl_verify
self.ssl_cert = ssl_cert
self.future_adapter_class = future_adapter_class
self.response_adapter_class = response_adapter_class
def __hash__(self):
# type: () -> int
return hash((
self.session,
self.authenticator,
self.ssl_verify,
self.ssl_cert,
self.future_adapter_class,
self.response_adapter_class,
))
def __ne__(self, other):
# type: (typing.Any) -> bool
return not (self == other)
def __eq__(self, other):
# type: (typing.Any) -> bool
return (
_are_objects_equal(
self, other,
# requests.Session does not define equality methods
attributes_to_ignore={'session'},
) and
# We're checking for requests.Session to be mostly the same as custom
# configurations (ie. max_redirects, proxies, SSL verification, etc.)
# might be possible.
_are_objects_equal(
self.session, other.session,
attributes_to_ignore={
'adapters', # requests.adapters.HTTPAdapter do not define equality
'prefetch', # attribute present in requests.Session.__attrs__ but never initialised or used
},
)
)
def separate_params(
self,
request_params, # type: typing.MutableMapping[str, typing.Any]
):
# type: (...) -> typing.Tuple[typing.Mapping[str, typing.Any], typing.Mapping[str, typing.Any]]
"""Splits the passed in dict of request_params into two buckets.
- sanitized_params are valid kwargs for constructing a
requests.Request(..)
- misc_options are things like timeouts which can't be communicated
to the Requests library via the requests.Request(...) constructor.
:param request_params: kitchen sink of request params. Treated as a
read-only dict.
:returns: tuple(sanitized_params, misc_options)
"""
sanitized_params = copy.copy(request_params)
misc_options = {
'ssl_verify': self.ssl_verify,
'ssl_cert': self.ssl_cert,
'follow_redirects': sanitized_params.pop('follow_redirects', False),
}
if 'connect_timeout' in sanitized_params:
misc_options['connect_timeout'] = \
sanitized_params.pop('connect_timeout')
if 'timeout' in sanitized_params:
misc_options['timeout'] = sanitized_params.pop('timeout')
return sanitized_params, misc_options
def request(
self,
request_params, # type: typing.MutableMapping[str, typing.Any]
operation=None, # type: typing.Optional[Operation]
request_config=None, # type: typing.Optional[RequestConfig]
):
# type: (...) -> HttpFuture[T]
"""
:param request_params: complete request data.
:type request_params: dict
:param operation: operation that this http request is for. Defaults
to None - in which case, we're obviously just retrieving a Swagger
Spec.
:type operation: :class:`bravado_core.operation.Operation`
:param RequestConfig request_config: per-request configuration
:returns: HTTP Future object
:rtype: :class: `bravado_core.http_future.HttpFuture`
"""
sanitized_params, misc_options = self.separate_params(request_params)
requests_future = self.future_adapter_class(
self.session,
self.authenticated_request(sanitized_params),
misc_options,
)
return HttpFuture(
requests_future,
self.response_adapter_class,
operation,
request_config,
)
def set_basic_auth(
self,
host, # type: str
username, # type: typing.Union[bytes, str]
password, # type: typing.Union[bytes, str]
):
# type: (...) -> None
self.authenticator = BasicAuthenticator(
host=host,
username=username,
password=password,
)
def set_api_key(
self,
host, # type: str
api_key, # type: typing.Text
param_name=u'api_key', # type: typing.Text
param_in=u'query', # type: typing.Text
):
# type: (...) -> None
self.authenticator = ApiKeyAuthenticator(
host=host,
api_key=api_key,
param_name=param_name,
param_in=param_in,
)
def authenticated_request(self, request_params):
# type: (typing.Mapping[str, typing.Any]) -> requests.Request
return self.apply_authentication(requests.Request(**request_params))
def apply_authentication(self, request):
# type: (requests.Request) -> requests.Request
if self.authenticator and self.authenticator.matches(request.url):
return self.authenticator.apply(request)
return request