Skip to content
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

Added async client | AUTH-9219 | [DO Not Merge] #37

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions authomize/rest_api_client/client/async_base_client.py
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:
aashishAuthomized marked this conversation as resolved.
Show resolved Hide resolved
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it awaitable?

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use furl for URL construction.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use furl for URL construction.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use furl for URL construction.

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()
Loading