Repository navigation
refactor: extract aiohttp transport #470
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| """ | ||
| HTTP layer under the async Unleash API client: sends one request with aiohttp | ||
| and returns the response. | ||
| """ | ||
|
|
||
| from typing import Optional | ||
|
|
||
| from UnleashClient._transport import Request, Response | ||
| from UnleashClient.constants import REQUEST_TIMEOUT | ||
| from UnleashClient.errors import AlreadyClosedError | ||
|
|
||
| try: | ||
| import aiohttp | ||
| except ImportError as exc: # pragma: no cover | ||
| raise ImportError( | ||
| "AsyncUnleashClient requires aiohttp, which is not installed. " | ||
| "Install it with: pip install UnleashClient[async]" | ||
| ) from exc | ||
|
|
||
|
|
||
| class AiohttpTransport: | ||
| """ | ||
| Sends requests with ``aiohttp``, on one session reused across requests. | ||
|
|
||
| The session is opened on the first request, since aiohttp needs a running | ||
| event loop to create one, and closed by :meth:`aclose`. A closed transport | ||
| stays closed. | ||
|
|
||
| :param timeout: Seconds to wait for the connection and for each read. | ||
| """ | ||
|
|
||
| def __init__(self, *, timeout: float = REQUEST_TIMEOUT) -> None: | ||
| self._timeout = timeout | ||
| self._session: Optional["aiohttp.ClientSession"] = None | ||
| self._is_closed: bool = False | ||
|
|
||
| def _get_session(self) -> "aiohttp.ClientSession": | ||
| if self._is_closed: | ||
| raise AlreadyClosedError() | ||
| if self._session is None: | ||
| self._session = aiohttp.ClientSession() | ||
| return self._session | ||
|
|
||
| def _client_timeout(self) -> "aiohttp.ClientTimeout": | ||
| # sock_connect/sock_read rather than total: the sync client's scalar | ||
| # timeout bounds each socket operation, not the whole request. | ||
| return aiohttp.ClientTimeout( | ||
| total=None, sock_connect=self._timeout, sock_read=self._timeout | ||
| ) | ||
|
|
||
| async def execute(self, request: Request) -> Response: | ||
| async with self._get_session().request( | ||
| request.method, | ||
| request.url, | ||
| headers=dict(request.headers), | ||
| params=dict(request.params) or None, | ||
| data=request.body, | ||
| timeout=self._client_timeout(), | ||
| ) as resp: | ||
| body = await resp.read() | ||
| return Response(status=resp.status, headers=resp.headers, body=body) | ||
|
|
||
| async def aclose(self) -> None: | ||
| """ | ||
| Close the session. Every later request raises | ||
| :class:`AlreadyClosedError`. Closing a closed transport does nothing. | ||
| """ | ||
| self._is_closed = True | ||
| session, self._session = self._session, None | ||
| if session is not None: | ||
| await session.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.