Skip to content

Commit 437c83c

Browse files
committed
fix: typing of async
1 parent ec5c8dd commit 437c83c

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

src/checkedid/client.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
from json import JSONDecodeError
2+
from types import TracebackType
23
from typing import Dict
34
from typing import List
45
from typing import Optional
5-
from typing import Self
66
from typing import Type
77
from typing import TypeVar
88

99
import httpx
1010
from httpx import Request
1111
from httpx import Response
12+
from httpx._types import URLTypes
1213

1314
from . import endpoints
1415
from . import errors
@@ -32,7 +33,7 @@ def __init__(self, customer_code: str, base_url: str = "https://api.checkedid.eu
3233
self.access_token: Optional[str] = None
3334
self.customer_code = customer_code
3435

35-
def create_client(self, base_url):
36+
def create_client(self, base_url: URLTypes) -> None:
3637
self.httpx = httpx.Client(base_url=base_url, auth=self.authenticate_request)
3738

3839
def authenticate_request(self, request: Request) -> Request:
@@ -142,27 +143,35 @@ def map_exception(self, response: Response) -> Type[errors.CheckedIDError]:
142143
class ClientAsync(Client):
143144
"""for asyncio"""
144145

145-
def create_client(self, base_url) -> None:
146-
self.client = httpx.AsyncClient(base_url=base_url)
146+
aclient: httpx.AsyncClient
147147

148-
async def dossier(self, dossier_number: str) -> endpoints.DossierEndpoint.response:
149-
response = await self.client.get(
148+
def create_client(self, base_url: URLTypes) -> None:
149+
self.aclient = httpx.AsyncClient(base_url=base_url)
150+
151+
async def adossier(self, dossier_number: str) -> Optional[models.ReportResponse]:
152+
response = await self.aclient.get(
150153
url=endpoints.DossierEndpoint.url(dossier_number=dossier_number)
151154
)
152155

153156
return self.process_response(response, endpoints.DossierEndpoint.response)
154157

155-
def close(self) -> None:
156-
self.client.aclose()
158+
async def close(self) -> None:
159+
if self.aclient:
160+
await self.aclient.aclose()
157161

158162
def open(self) -> None:
159163
self.create_client(self.base_url)
160164

161-
def __enter__(self) -> Self:
165+
async def __aenter__(self) -> "ClientAsync":
162166
"""Open the httpx client"""
163167
self.open()
164168
return self
165169

166-
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
170+
async def __aexit__(
171+
self,
172+
exc_type: Optional[Type[BaseException]] = None,
173+
exc_value: Optional[BaseException] = None,
174+
traceback: Optional[TracebackType] = None,
175+
) -> None:
167176
"""Close the httpx client"""
168-
self.close()
177+
await self.close()

src/checkedid/endpoints.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any
2+
from typing import Optional
13
from typing import Type
24

35
from pydantic import BaseModel
@@ -7,8 +9,8 @@
79

810
class Endpoint:
911
path: str
10-
request: Type[BaseModel]
11-
response = Type[BaseModel]
12+
request: Optional[Type[BaseModel]]
13+
response = Optional[Type[BaseModel]]
1214

1315

1416
class DossierEndpoint(Endpoint):
@@ -17,5 +19,5 @@ class DossierEndpoint(Endpoint):
1719
response = models.ReportResponse
1820

1921
@classmethod
20-
def url(cls, **kwargs):
22+
def url(cls, **kwargs: Any) -> str:
2123
return cls.path.format(**kwargs)

0 commit comments

Comments
 (0)