-
Notifications
You must be signed in to change notification settings - Fork 8
/
http_client.py
216 lines (185 loc) · 9.16 KB
/
http_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
import asyncio
import logging
import ssl
from collections import Mapping
from distutils.version import LooseVersion
from typing import Any
from typing import Callable # noqa: F401
from typing import cast
from typing import Dict
from typing import Optional
from typing import Sequence
from typing import Union
import aiohttp
from aiohttp.formdata import FormData
from bravado import http_future # noqa
from bravado.config import RequestConfig
from bravado.http_client import HttpClient
from bravado.http_future import HttpFuture
from bravado_core.operation import Operation
from bravado_core.schema import is_list_like
from multidict import MultiDict
from yelp_bytes import from_bytes
from bravado_asyncio.definitions import RunMode
from bravado_asyncio.future_adapter import AsyncioFutureAdapter
from bravado_asyncio.future_adapter import FutureAdapter
from bravado_asyncio.response_adapter import AioHTTPResponseAdapter
from bravado_asyncio.response_adapter import AsyncioHTTPResponseAdapter
from bravado_asyncio.thread_loop import get_thread_loop
log = logging.getLogger(__name__)
#: module variable holding the current :class:`aiohttp.ClientSession`, so that we can share it between
#: :class:`AsyncioClient` instances. Use :func:`get_client_session` to retrieve it and create one if none
#: is present.
client_session = None # type: Optional[aiohttp.ClientSession]
def get_client_session(loop: asyncio.AbstractEventLoop) -> aiohttp.ClientSession:
"""Get a shared ClientSession object that can be reused. If none exists yet it will
create one using the passed-in loop.
:param loop: an active (i.e. not closed) asyncio event loop
:return: a ClientSession instance that can be used to do HTTP requests
"""
global client_session
if client_session:
return client_session
client_session = aiohttp.ClientSession(loop=loop)
return client_session
class AsyncioClient(HttpClient):
"""Asynchronous HTTP client using the asyncio event loop. Can either use an event loop
in a separate thread or operate fully asynchronous within the current thread, using
async / await.
"""
def __init__(
self,
run_mode: RunMode=RunMode.THREAD,
loop: Optional[asyncio.AbstractEventLoop]=None,
ssl_verify: Optional[Union[bool, str]]=None,
ssl_cert: Optional[Union[str, Sequence[str]]]=None,
) -> None:
"""Instantiate a client using the given run_mode. If you do not pass in an event loop, then
either a shared loop in a separate thread (THREAD mode) or the default asyncio
event loop (FULL_ASYNCIO mode) will be used.
Not passing in an event loop will make sure we share the :py:class:`aiohttp.ClientSession` object
between AsyncioClient instances.
: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.
"""
self.run_mode = run_mode
if self.run_mode == RunMode.THREAD:
self.loop = loop or get_thread_loop()
self.run_coroutine_func = asyncio.run_coroutine_threadsafe # type: Callable
self.response_adapter = AioHTTPResponseAdapter
self.bravado_future_class = HttpFuture
self.future_adapter = FutureAdapter # type: http_future.FutureAdapter
elif run_mode == RunMode.FULL_ASYNCIO:
from aiobravado.http_future import HttpFuture as AsyncioHttpFuture
self.loop = loop or asyncio.get_event_loop()
self.run_coroutine_func = asyncio.ensure_future
self.response_adapter = AsyncioHTTPResponseAdapter
self.bravado_future_class = AsyncioHttpFuture
self.future_adapter = AsyncioFutureAdapter
else:
raise ValueError('Don\'t know how to handle run mode {}'.format(str(run_mode)))
# don't use the shared client_session if we've been passed an explicit loop argument
if loop:
self.client_session = aiohttp.ClientSession(loop=loop)
else:
self.client_session = get_client_session(self.loop)
# translate the requests-type SSL options to a ssl.SSLContext object as used by aiohttp.
# see https://aiohttp.readthedocs.io/en/stable/client_advanced.html#ssl-control-for-tcp-sockets
if isinstance(ssl_verify, str) or ssl_cert:
self.ssl_verify = None # type: Optional[bool]
cafile = None
if isinstance(ssl_verify, str):
cafile = ssl_verify
self.ssl_context = ssl.create_default_context(cafile=cafile) # type: Optional[ssl.SSLContext]
if ssl_cert:
if isinstance(ssl_cert, str):
ssl_cert = [ssl_cert]
self.ssl_context.load_cert_chain(*ssl_cert)
else:
self.ssl_verify = ssl_verify
self.ssl_context = None
def request(
self,
request_params: Dict[str, Any],
operation: Optional[Operation]=None,
request_config: Optional[RequestConfig]=None,
) -> HttpFuture:
"""Sets up the request params for aiohttp and executes the request in the background.
:param request_params: request parameters for the http request.
:param operation: operation that this http request is for. Defaults
to None - in which case, we're obviously just retrieving a Swagger
Spec.
:param request_config:RequestConfig request_config: Per-request config that is passed to
:class:`bravado.http_future.HttpFuture`.
:rtype: :class: `bravado_core.http_future.HttpFuture`
"""
orig_data = request_params.get('data', {})
if isinstance(orig_data, Mapping):
data = FormData()
for name, value in orig_data.items():
str_value = str(value) if not is_list_like(value) else [str(v) for v in value]
data.add_field(name, str_value)
else:
data = orig_data
if isinstance(data, FormData):
for name, file_tuple in request_params.get('files', {}):
stream_obj = file_tuple[1]
if not hasattr(stream_obj, 'name') or not isinstance(stream_obj.name, str):
# work around an issue in aiohttp: it's not able to deal with names of type int. We've observed
# this case in the real world and it is a documented possibility:
# https://docs.python.org/3/library/io.html#raw-file-i-o
stream_obj = stream_obj.read()
data.add_field(name, stream_obj, filename=file_tuple[0])
params = self.prepare_params(request_params.get('params'))
connect_timeout = request_params.get('connect_timeout')
if connect_timeout:
log.warning(
'bravado-asyncio does not support setting a connect_timeout '
'(you passed a value of {})'.format(connect_timeout),
)
timeout = request_params.get('timeout')
# aiohttp always adds a Content-Type header, and this breaks some servers that don't
# expect it for non-POST/PUT requests: https://github.com/aio-libs/aiohttp/issues/457
skip_auto_headers = ['Content-Type'] if request_params.get('method') not in ['POST', 'PUT'] else None
coroutine = self.client_session.request(
method=request_params.get('method') or 'GET',
url=cast(str, request_params.get('url', '')),
params=params,
data=data,
headers={
# Convert not string headers to string
k: from_bytes(v) if isinstance(v, bytes) else str(v)
for k, v in request_params.get('headers', {}).items()
},
skip_auto_headers=skip_auto_headers,
timeout=timeout,
**self._get_ssl_params()
)
future = self.run_coroutine_func(coroutine, loop=self.loop)
return self.bravado_future_class(
self.future_adapter(future),
self.response_adapter(loop=self.loop),
operation,
request_config=request_config,
)
def prepare_params(self, params: Optional[Dict[str, Any]]) -> Union[Optional[Dict[str, Any]], MultiDict]:
if not params:
return params
items = []
for key, value in params.items():
entries = [(key, str(value))] if not is_list_like(value) else [(key, str(v)) for v in value]
items.extend(entries)
return MultiDict(items)
def _get_ssl_params(self) -> Dict[str, Any]:
aiohttp_version = LooseVersion(aiohttp.__version__)
if aiohttp_version < LooseVersion('3'):
if (self.ssl_verify is not None) or (self.ssl_context is not None):
log.warning('SSL options are not supported and will be ignored for aiohttp versions below 3.')
return {}
else:
return {
'ssl': self.ssl_context if self.ssl_context else self.ssl_verify
}