-
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.
- Loading branch information
1 parent
1d1fa15
commit d371879
Showing
7 changed files
with
91 additions
and
38 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from collections.abc import Sequence | ||
from dataclasses import dataclass | ||
from types import TracebackType | ||
|
||
import aiohttp | ||
from yarl import URL | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Job: | ||
id: str | ||
uri: URL | ||
|
||
|
||
class ApiClient: | ||
_client: aiohttp.ClientSession | ||
|
||
def __init__( | ||
self, | ||
url: URL, | ||
token: str | None = None, | ||
timeout: aiohttp.ClientTimeout = aiohttp.client.DEFAULT_TIMEOUT, | ||
trace_configs: Sequence[aiohttp.TraceConfig] = (), | ||
): | ||
super().__init__() | ||
|
||
self._base_url = url / "api/v1" | ||
self._token = token | ||
self._timeout = timeout | ||
self._trace_configs = trace_configs | ||
|
||
async def __aenter__(self) -> "ApiClient": | ||
self._client = self._create_http_client() | ||
return self | ||
|
||
async def __aexit__( | ||
self, | ||
exc_type: type[BaseException] | None, | ||
exc_val: BaseException | None, | ||
exc_tb: TracebackType | None, | ||
) -> None: | ||
await self.aclose() | ||
|
||
def _create_http_client(self) -> aiohttp.ClientSession: | ||
return aiohttp.ClientSession( | ||
headers=self._create_default_headers(), | ||
timeout=self._timeout, | ||
trace_configs=list(self._trace_configs), | ||
) | ||
|
||
async def aclose(self) -> None: | ||
assert self._client | ||
await self._client.close() | ||
|
||
def _create_default_headers(self) -> dict[str, str]: | ||
result = {} | ||
if self._token: | ||
result["Authorization"] = f"Bearer {self._token}" | ||
return result | ||
|
||
async def get_job(self, id_: str) -> Job: | ||
async with self._client.get(self._base_url / "jobs" / id_) as response: | ||
response.raise_for_status() | ||
response_json = await response.json() | ||
return Job( | ||
id=response_json["id"], | ||
uri=URL(response_json["uri"]), | ||
) |
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