99from httpx import Request
1010from httpx import Response
1111
12+ from . import endpoints
13+ from . import errors
1214from . import models
13- from .errors import CheckedIDAuthenticationError
14- from .errors import CheckedIDError
15- from .errors import CheckedIDNotFoundError
16- from .errors import CheckedIDValidationError
1715
1816
1917_T = TypeVar ("_T" )
2018
2119
2220class Client :
23- ERROR_RESPONSE_MAPPING : Dict [int , Type [CheckedIDError ]] = {
24- 422 : CheckedIDValidationError ,
25- 403 : CheckedIDAuthenticationError ,
26- 404 : CheckedIDNotFoundError ,
21+ ERROR_RESPONSE_MAPPING : Dict [int , Type [errors . CheckedIDError ]] = {
22+ 422 : errors . CheckedIDValidationError ,
23+ 403 : errors . CheckedIDAuthenticationError ,
24+ 404 : errors . CheckedIDNotFoundError ,
2725 }
2826
2927 def __init__ (self , customer_code : str , base_url : str = "https://api.checkedid.eu/" ):
30- self .httpx = httpx .Client (base_url = base_url , auth = self .authenticate_request )
28+ self .client : Optional [httpx .Client ] = None
29+ self .base_url = base_url
30+ self .create_client (base_url )
3131 self .access_token : Optional [str ] = None
3232 self .customer_code = customer_code
3333
34+ def create_client (self , base_url ):
35+ self .httpx = httpx .Client (base_url = base_url , auth = self .authenticate_request )
36+
3437 def authenticate_request (self , request : Request ) -> Request :
3538 if self .access_token :
3639 request .headers ["Authorization" ] = f"Bearer { self .access_token } "
@@ -112,7 +115,7 @@ def dossier_with_scope(
112115
113116 def handle_error_response (self , response : Response ) -> None :
114117 if response .status_code == 400 :
115- raise CheckedIDValidationError (
118+ raise errors . CheckedIDValidationError (
116119 response .text , status_code = response .status_code
117120 )
118121
@@ -128,8 +131,37 @@ def handle_error_response(self, response: Response) -> None:
128131 status_code = response .status_code , json = json , message = "Error from server"
129132 )
130133
131- def map_exception (self , response : Response ) -> Type [CheckedIDError ]:
134+ def map_exception (self , response : Response ) -> Type [errors . CheckedIDError ]:
132135 exception_type = self .ERROR_RESPONSE_MAPPING .get (
133- response .status_code , CheckedIDError
136+ response .status_code , errors . CheckedIDError
134137 )
135138 return exception_type
139+
140+
141+ class ClientAsync (Client ):
142+ """for asyncio"""
143+
144+ def create_client (self , base_url ):
145+ self .client = httpx .AsyncClient (base_url = base_url )
146+
147+ async def dossier (self , dossier_number : str ) -> endpoints .DossierEndpoint .response :
148+ response = await self .client .get (
149+ url = endpoints .DossierEndpoint .url (dossier_number = dossier_number )
150+ )
151+
152+ return self .process_response (response , endpoints .DossierEndpoint .response )
153+
154+ def close (self ):
155+ self .client .aclose ()
156+
157+ def open (self ):
158+ self .create_client (self .base_url )
159+
160+ def __enter__ (self ):
161+ """Open the httpx client"""
162+ self .open ()
163+ return self
164+
165+ def __exit__ (self , exc_type , exc_val , exc_tb ):
166+ """Close the httpx client"""
167+ self .close ()
0 commit comments