11import datetime as dt
22from dataclasses import dataclass , field
3- from typing import Mapping
3+ from typing import Any , Mapping
44
55import httpx
66
2020class Client :
2121 base_url = 'https://app.q-detect.com'
2222 username : str
23- client_id : str
2423 auth_token : str | None = None
2524 _client : httpx .AsyncClient = field (
2625 default_factory = httpx .AsyncClient , init = False
@@ -32,7 +31,7 @@ async def _make_request(
3231 url : str ,
3332 * ,
3433 headers : Mapping [str , str ] | None = None ,
35- params : Mapping [str , str | int | None ] | None = None ,
34+ params : Mapping [str , Any ] | None = None ,
3635 ) -> httpx .Response :
3736 """Make an HTTP request using an async client."""
3837 response = await self ._client .request (
@@ -41,19 +40,21 @@ async def _make_request(
4140 response .raise_for_status ()
4241 return response
4342
44- async def create_token (self , secret_key : str ) -> str :
43+ @classmethod
44+ async def create_token (cls , client_id : str , secret_key : str ) -> str :
4545 """Create a new authentication token."""
46-
47- auth_url = f'{ self .base_url } /api/token'
48- params = {'client_id' : self .client_id }
46+ auth_url = f'{ cls .base_url } /api/token'
47+ params = {'client_id' : client_id }
4948 headers = {
5049 'Authorization' : f'Bearer { secret_key } ' ,
5150 'Accept-Encoding' : 'identity' ,
5251 }
5352 try :
54- response = await self ._make_request (
55- 'GET' , auth_url , headers = headers , params = params
56- )
53+ async with httpx .AsyncClient () as client :
54+ response = await client .request (
55+ 'GET' , auth_url , headers = headers , params = params
56+ )
57+ response .raise_for_status ()
5758 except httpx .HTTPStatusError as exc :
5859 raise QuienEsQuienError (exc .response ) from exc
5960 return response .text
@@ -91,37 +92,25 @@ async def search(
9192 self .auth_token
9293 ), 'you must create or reuse an already created auth token'
9394
94- # Validate search criteria
95- by_name = full_name is not None
96- by_rfc = rfc is not None
97- by_curp = curp is not None
98-
99- if not (by_name or by_rfc or by_curp ):
95+ if not (full_name or rfc or curp ):
10096 raise InvalidSearchCriteriaError
10197
10298 # Build base URL with required parameters
10399 search_url = f'{ self .base_url } /api/find'
104100
105- params : dict [str , str | int | None ] = {
106- 'client_id' : self .client_id ,
101+ params = {
107102 'username' : self .username ,
108103 'percent' : match_score ,
104+ 'name' : full_name ,
105+ 'rfc' : rfc ,
106+ 'curp' : curp ,
107+ 'sex' : gender .value if gender else None ,
108+ 'birthday' : birthday .strftime ('%d/%m/%Y' ) if birthday else None ,
109+ 'type' : search_type .value if search_type is not None else None ,
110+ 'list' : ',' .join (search_list ) if search_list else None ,
109111 }
110112
111- if by_name :
112- params ['name' ] = full_name
113- if rfc :
114- params ['rfc' ] = rfc
115- if curp :
116- params ['curp' ] = curp
117- if gender :
118- params ['sex' ] = gender .value
119- if birthday :
120- params ['birthday' ] = birthday .strftime ('%d/%m/%Y' )
121- if search_type is not None :
122- params ['type' ] = search_type .value
123- if search_list :
124- params ['list' ] = ',' .join (search_list )
113+ params = {k : v for k , v in params .items () if v is not None }
125114
126115 headers = {'Authorization' : f'Bearer { self .auth_token } ' }
127116
0 commit comments