Skip to content

Commit 67b4a5b

Browse files
author
gabino
committed
Refactor Client class to streamline token creation
1 parent a9d93e6 commit 67b4a5b

16 files changed

Lines changed: 44 additions & 57 deletions

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ Before performing searches, you need to create an authentication token using the
4545
```python
4646
from quienesquien import Client
4747

48-
client = Client(os.environ['QEQ_USER'], os.environ['QEQ_CLIENT_ID'])
49-
50-
auth_token = await client.create_token(os.environ['QEQ_SECRET_ID'])
48+
client = Client(os.environ['QEQ_USER'])
49+
auth_token = await client.create_token(os.environ['QEQ_CLIENT_ID'], os.environ['QEQ_SECRET_ID'])
5150
```
5251

5352
You can reuse this token in subsequent requests.
@@ -70,7 +69,6 @@ from quienesquien.exc import (
7069
# Create the client with the token
7170
client = Client(
7271
os.environ['QEQ_USER'],
73-
os.environ['QEQ_CLIENT_ID'],
7472
auth_token,
7573
)
7674

quienesquien/client.py

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datetime as dt
22
from dataclasses import dataclass, field
3-
from typing import Mapping
3+
from typing import Any, Mapping
44

55
import httpx
66

@@ -20,7 +20,6 @@
2020
class 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

quienesquien/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.0.1.dev4'
1+
__version__ = '1.0.1'

tests/cassettes/test_block_account.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interactions:
1515
user-agent:
1616
- python-httpx/0.28.1
1717
method: GET
18-
uri: https://app.q-detect.com/api/find?client_id=DUMMY_CLIENT_ID&name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
18+
uri: https://app.q-detect.com/api/find?name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
1919
response:
2020
body:
2121
string: "{\"success\":false,\"status\":\"El token proporcionado para realizar

tests/cassettes/test_create_token.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ interactions:
6060
user-agent:
6161
- python-httpx/0.28.1
6262
method: GET
63-
uri: https://app.q-detect.com/api/find?client_id=DUMMY_CLIENT_ID&name=Andres+Manuel+Lopez+Obrador&percent=80&username=DUMMY_USERNAME
63+
uri: https://app.q-detect.com/api/find?name=Andres+Manuel+Lopez+Obrador&percent=80&username=DUMMY_USERNAME
6464
response:
6565
body:
6666
string: '{"success":true,"data":[{"NOMBRECOMP":"Andres Manuel L\u00f3pez Obrador","CURP":"LOOA531113HTCPBN07","RFC":"LOOA531113F15","ID_PERSONA":"QEQ0281252","NOMBRE":"Andres

tests/cassettes/test_insufficient_balance.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interactions:
1515
user-agent:
1616
- python-httpx/0.28.1
1717
method: GET
18-
uri: https://app.q-detect.com/api/find?client_id=DUMMY_CLIENT_ID&name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
18+
uri: https://app.q-detect.com/api/find?name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
1919
response:
2020
body:
2121
string: "{\"success\":false,\"status\":\"No se puede realizar la b\xFAsqueda,

tests/cassettes/test_internal_server_error.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interactions:
1515
user-agent:
1616
- python-httpx/0.28.1
1717
method: GET
18-
uri: https://app.q-detect.com/api/find?client_id=DUMMY_CLIENT_ID&name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
18+
uri: https://app.q-detect.com/api/find?name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
1919
response:
2020
body:
2121
string: '{"success":false,"status":"Internal server error"}'

tests/cassettes/test_invalid_plan.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interactions:
1515
user-agent:
1616
- python-httpx/0.28.1
1717
method: GET
18-
uri: https://app.q-detect.com/api/find?client_id=DUMMY_CLIENT_ID&name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
18+
uri: https://app.q-detect.com/api/find?name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
1919
response:
2020
body:
2121
string: '{"success":false,"status":"Tu plan de consultas ha expirado, por favor

tests/cassettes/test_invalid_token.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interactions:
1515
user-agent:
1616
- python-httpx/0.28.1
1717
method: GET
18-
uri: https://app.q-detect.com/api/find?client_id=DUMMY_CLIENT_ID&name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
18+
uri: https://app.q-detect.com/api/find?name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
1919
response:
2020
body:
2121
string: '{"success":false,"status":"El token proporcionado para realizar esta

tests/cassettes/test_search_by_curp.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interactions:
1515
user-agent:
1616
- python-httpx/0.28.1
1717
method: GET
18-
uri: https://app.q-detect.com/api/find?birthday=13%2F11%2F1953&client_id=DUMMY_CLIENT_ID&curp=LOOA531113HTCPBN07&list=PPE%2CONU&percent=85&sex=M&type=0&username=DUMMY_USERNAME
18+
uri: https://app.q-detect.com/api/find?birthday=13%2F11%2F1953&curp=LOOA531113HTCPBN07&list=PPE%2CONU&percent=85&sex=M&type=0&username=DUMMY_USERNAME
1919
response:
2020
body:
2121
string: '{"success":true,"data":[{"NOMBRECOMP":"Andres Manuel L\u00f3pez Obrador","CURP":"LOOA531113HTCPBN07","RFC":"LOOA531113F15","ID_PERSONA":"QEQ0281252","NOMBRE":"Andres

0 commit comments

Comments
 (0)