-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
APIClient, wrapper around httpx client requests
- Loading branch information
Showing
4 changed files
with
54 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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,28 @@ | ||
from httpx import Response, AsyncClient | ||
|
||
class APIClient: | ||
def __init__(self, base_url: str) -> None: | ||
self.http_client = AsyncClient() | ||
self.base_url = base_url | ||
|
||
async def close(self) -> None: | ||
await self.http_client.aclose() | ||
|
||
def full_url(self, partial_endpoint: str) -> str: | ||
return f"{self.base_url}/{partial_endpoint}" | ||
|
||
async def request(self, method: str, partial_endpoint: str, **kwargs) -> dict: | ||
r: Response = await self.http_client.request(method.upper(), self.full_url(partial_endpoint), **kwargs) | ||
|
||
r.raise_for_status() | ||
|
||
return r.json() | ||
|
||
async def get(self, partial_endpoint: str, **kwargs) -> dict: | ||
return await self.request("GET", partial_endpoint, **kwargs) | ||
|
||
async def post(self, partial_endpoint: str, **kwargs) -> dict: | ||
return await self.request("POST", partial_endpoint, **kwargs) | ||
|
||
async def delete(self, partial_endpoint: str, **kwargs) -> dict: | ||
return await self.request("DELETE", partial_endpoint, **kwargs) |
This file contains 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 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 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