Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion authomize/rest_api_client/client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from authomize.rest_api_client.client.client import Client
from authomize.rest_api_client.client.async_client import Client

__all__ = ['Client']
91 changes: 91 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,91 @@
from typing import Optional

import aiohttp
from furl import furl # type: ignore

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 = furl(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 or ""})

@property
def authorization_header(self) -> str:
raise NotImplementedError()

async def http_get(self, url, params=None):
url = self.base_url.join(url).url
async with self.session.get(url, params=params) as response:
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_post(self, url: str, body: Optional[str] = None):
url = self.base_url.join(url).url
async with self.session.post(
url,
headers={'Content-Type': 'application/json'},
data=body,
) as response:
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_patch(self, url: str, body: Optional[str] = None):
url = self.base_url.join(url).url
async with self.session.patch(
url,
headers={'Content-Type': 'application/json'},
data=body,
) as response:
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.join(url).url
async with self.session.delete(url, params=params) as response:
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 close(self):
await self.session.close()
Loading