-
Notifications
You must be signed in to change notification settings - Fork 3
Added async client | AUTH-9219 | [DO Not Merge] #37
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
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 |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| from typing import Optional | ||
|
|
||
| import aiohttp | ||
| from aiohttp import ClientResponse | ||
|
|
||
| AUTHOMIZE_API_URL = 'https://api.authomize.com' | ||
| STATUS_OK: int = 200 | ||
|
|
||
|
|
||
| class AsyncClientError(Exception): | ||
| def __init__(self, message): | ||
| self.message = message | ||
|
|
||
|
|
||
| class AsyncBaseClient: | ||
| def __init__(self, auth_token: str, base_url: str = AUTHOMIZE_API_URL): | ||
| self.auth_token = auth_token | ||
| self.base_url = base_url | ||
| self.session = aiohttp.ClientSession() | ||
|
Contributor
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. Isn't it awaitable?
Contributor
Author
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.... the session doesn't needs to be awaited. |
||
| self.session.headers.update({'Authorization': self.authorization_header}) | ||
|
|
||
| @property | ||
| def authorization_header(self) -> str: | ||
| raise NotImplementedError() | ||
|
|
||
| async def http_get(self, url, params=None): | ||
| url = self.base_url + url | ||
|
||
| response: ClientResponse = await self.session.get(url, params=params) | ||
| if response.status == STATUS_OK: | ||
| return response.json() | ||
| try: | ||
| response_json = await response.json() | ||
| detail = response_json.get('detail') | ||
| except Exception: | ||
| detail = None | ||
| if detail: | ||
| raise AsyncClientError(str(detail)) | ||
| response.raise_for_status() | ||
|
|
||
| async def http_post(self, url: str, body: Optional[str] = None): | ||
| url = self.base_url + url | ||
|
||
| response = await self.session.post( | ||
| url, | ||
| headers={'Content-Type': 'application/json'}, | ||
| data=body, | ||
| ) | ||
| if response.status == STATUS_OK: | ||
| return await response.json() | ||
| try: | ||
| response_json = await response.json() | ||
| detail = response_json.get('detail') | ||
| except Exception: | ||
| detail = None | ||
| if detail: | ||
| raise AsyncClientError(str(detail)) | ||
| response.raise_for_status() | ||
|
|
||
| async def http_delete(self, url: str, params=None): | ||
| url = self.base_url + url | ||
|
||
| response = await self.session.delete(url, params=params) | ||
| if response.status == STATUS_OK: | ||
| return await response.json() | ||
| try: | ||
| response_json = await response.json() | ||
| detail = response_json.get('detail') | ||
| except Exception: | ||
| detail = None | ||
| if detail: | ||
| raise AsyncClientError(str(detail)) | ||
| response.raise_for_status() | ||
Uh oh!
There was an error while loading. Please reload this page.