-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aiohttp2 support+ssl fix #1
Changes from all commits
493526c
2c08fb6
1511bf9
151822d
966386b
12d60ca
5559e0f
6ce95ce
6948ee8
b3ef896
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
import asyncio | ||
import ssl | ||
|
||
import aiohttp | ||
from aiohttp.errors import ClientError, FingerprintMismatch | ||
from elasticsearch.connection import Connection | ||
from elasticsearch.exceptions import (ConnectionError, ConnectionTimeout, | ||
|
||
from aioelasticsearch.compat import AIOHTTP_2, create_future | ||
|
||
if AIOHTTP_2: | ||
from aiohttp import ClientError | ||
else: | ||
from aiohttp.errors import ClientError | ||
|
||
from elasticsearch.connection import Connection # noqa # isort:skip | ||
from elasticsearch.exceptions import (ConnectionError, ConnectionTimeout, # noqa # isort:skip | ||
SSLError) | ||
from yarl import URL | ||
from yarl import URL # noqa # isort:skip | ||
|
||
|
||
class AIOHttpConnection(Connection): | ||
|
@@ -16,6 +24,7 @@ def __init__( | |
port=9200, | ||
http_auth=None, | ||
use_ssl=False, | ||
ssl_context=None, | ||
verify_certs=False, | ||
maxsize=10, | ||
headers=None, | ||
|
@@ -54,51 +63,118 @@ def __init__( | |
connector=aiohttp.TCPConnector( | ||
limit=maxsize, | ||
use_dns_cache=kwargs.get('use_dns_cache', False), | ||
ssl_context=ssl_context, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to double check one more time list of parameters from init here... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what exactly to double check and what for? |
||
verify_ssl=self.verify_certs, | ||
conn_timeout=None, | ||
loop=self.loop, | ||
), | ||
) | ||
|
||
def close(self): | ||
return self.session.close() | ||
coro = self.session.close() | ||
if not AIOHTTP_2: | ||
return coro | ||
|
||
future = create_future(loop=self.loop) | ||
future.set_result(None) | ||
return future | ||
|
||
@asyncio.coroutine | ||
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()): # noqa | ||
def perform_request( | ||
self, | ||
method, | ||
url, | ||
params=None, | ||
body=None, | ||
timeout=None, | ||
ignore=() | ||
): | ||
url_path = url | ||
|
||
url = (self.base_url / url.lstrip('/')).with_query(params) | ||
|
||
start = self.loop.time() | ||
response = None | ||
try: | ||
with aiohttp.Timeout(timeout or self.timeout, loop=self.loop): # noqa | ||
response = yield from self.session.request(method, url, data=body, headers=self.headers, timeout=None) # noqa | ||
with aiohttp.Timeout(timeout or self.timeout, loop=self.loop): | ||
response = yield from self.session.request( | ||
method, | ||
url, | ||
data=body, | ||
headers=self.headers, | ||
timeout=None, | ||
) | ||
raw_data = yield from response.text() | ||
|
||
duration = self.loop.time() - start | ||
|
||
except ssl.CertificateError as exc: | ||
self.log_request_fail( | ||
method, | ||
url, | ||
url_path, | ||
body, | ||
self.loop.time() - start, | ||
exception=exc, | ||
) | ||
raise SSLError('N/A', str(exc), exc) | ||
|
||
except asyncio.TimeoutError as exc: | ||
self.log_request_fail(method, url, url_path, body, self.loop.time() - start, exception=exc) # noqa | ||
self.log_request_fail( | ||
method, | ||
url, | ||
url_path, | ||
body, | ||
self.loop.time() - start, | ||
exception=exc, | ||
) | ||
raise ConnectionTimeout('TIMEOUT', str(exc), exc) | ||
|
||
except FingerprintMismatch as exc: | ||
self.log_request_fail(method, url, url_path, body, self.loop.time() - start, exception=exc) # noqa | ||
raise SSLError('N/A', str(exc), exc) | ||
|
||
except ClientError as exc: | ||
self.log_request_fail(method, url, url_path, body, self.loop.time() - start, exception=exc) # noqa | ||
raise ConnectionError('N/A', str(exc), exc) | ||
self.log_request_fail( | ||
method, | ||
url, | ||
url_path, | ||
body, | ||
self.loop.time() - start, | ||
exception=exc, | ||
) | ||
|
||
_exc = str(exc) | ||
# aiohttp wraps ssl error | ||
if 'SSL: CERTIFICATE_VERIFY_FAILED' in _exc: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No other way to check it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its just a message, so no. |
||
raise SSLError('N/A', _exc, exc) | ||
|
||
raise ConnectionError('N/A', _exc, exc) | ||
|
||
finally: | ||
if response is not None: | ||
yield from response.release() | ||
|
||
# raise errors based on http status codes, let the client handle those if needed # noqa | ||
if not (200 <= response.status < 300) and response.status not in ignore: # noqa | ||
self.log_request_fail(method, url, url_path, body, duration, response.status, raw_data) # noqa | ||
# raise errors based on http status codes | ||
# let the client handle those if needed | ||
if ( | ||
not (200 <= response.status < 300) and | ||
response.status not in ignore | ||
): | ||
self.log_request_fail( | ||
method, | ||
url, | ||
url_path, | ||
body, | ||
duration, | ||
response.status, | ||
raw_data, | ||
) | ||
self._raise_error(response.status, raw_data) | ||
|
||
self.log_request_success(method, url, url_path, body, response.status, raw_data, duration) # noqa | ||
self.log_request_success( | ||
method, | ||
url, | ||
url_path, | ||
body, | ||
response.status, | ||
raw_data, | ||
duration, | ||
) | ||
|
||
return response.status, response.headers, raw_data |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
aiohttp==1.3.5 | ||
aiohttp==2.0.5 | ||
appdirs==1.4.3 | ||
appnope==0.1.0 | ||
async-timeout==1.2.0 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
module level import not on the top because of if statement for aiohttp