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

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
Contributor 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
Contributor

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
Contributor

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
Contributor

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