Skip to content

Commit 95d7044

Browse files
rogelioLpzvedi
authored andcommitted
Multiple clients (#11)
1 parent 5ee486b commit 95d7044

File tree

11 files changed

+285
-23
lines changed

11 files changed

+285
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pip install mati
1616

1717
```
1818
make venv
19-
source venv/bin/active
19+
source venv/bin/activate
2020
make test
2121
```
2222

mati/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ def get_valid_bearer_token(
4848
except KeyError:
4949
expired = True
5050
if expired: # renew token
51-
self.bearer_tokens[score] = self.access_tokens.create(score)
51+
self.bearer_tokens[score] = self.access_tokens.create(
52+
score, client=self
53+
)
5254
return self.bearer_tokens[score]
5355

5456
def get(self, endpoint: str, **kwargs: Any) -> Dict[str, Any]:

mati/resources/access_tokens.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@ class AccessToken(Resource):
2222
user_id: Optional[str]
2323

2424
@classmethod
25-
def create(cls, score: Optional[str] = None) -> 'AccessToken':
25+
def create(cls, score: Optional[str] = None, client=None) -> 'AccessToken':
26+
client = client or cls._client
2627
data = dict(grant_type='client_credentials')
2728
endpoint = cls._endpoint
2829
if score:
2930
data['score'] = score
3031
endpoint += '/token'
31-
resp = cls._client.post(
32-
endpoint,
33-
data=data,
34-
auth=basic_auth_str(*cls._client.basic_auth_creds),
32+
resp = client.post(
33+
endpoint, data=data, auth=basic_auth_str(*client.basic_auth_creds),
3534
)
3635
try:
3736
expires_in = resp['expiresIn']

mati/resources/identities.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,34 @@ class Identity(Resource):
2525
metadata: Union[dict, List[str]] = field(default_factory=dict)
2626
fullName: Optional[str] = None
2727
facematchScore: Optional[float] = None
28+
photo: Optional[str] = None
29+
video: Optional[str] = None
2830

2931
@classmethod
30-
def create(cls, **metadata) -> 'Identity':
31-
resp = cls._client.post(cls._endpoint, json=dict(metadata=metadata))
32+
def create(cls, client=None, **metadata) -> 'Identity':
33+
client = client or cls._client
34+
resp = client.post(cls._endpoint, json=dict(metadata=metadata))
3235
resp['id'] = resp.pop('_id')
3336
return cls(**resp)
3437

3538
@classmethod
36-
def retrieve(cls, identity_id: str) -> 'Identity':
39+
def retrieve(cls, identity_id: str, client=None) -> 'Identity':
40+
client = client or cls._client
3741
endpoint = f'{cls._endpoint}/{identity_id}'
38-
resp = cls._client.get(endpoint)
42+
resp = client.get(endpoint)
3943
resp['id'] = resp.pop('_id')
4044
return cls(**resp)
4145

42-
def refresh(self) -> None:
43-
identity = self.retrieve(self.id)
46+
def refresh(self, client=None) -> None:
47+
client = client or self._client
48+
identity = self.retrieve(self.id, client=client)
4449
for k, v in identity.__dict__.items():
4550
setattr(self, k, v)
4651

4752
def upload_validation_data(
48-
self, user_validation_files: List[UserValidationFile]
53+
self, user_validation_files: List[UserValidationFile], client=None
4954
) -> List[dict]:
50-
return UserValidationData.upload(self.id, user_validation_files)
55+
client = client or self._client
56+
return UserValidationData.upload(
57+
self.id, user_validation_files, client=client
58+
)

mati/resources/user_verification_data.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,18 @@ def _append_file(
5252

5353
@classmethod
5454
def upload(
55-
cls, identity_id: str, user_validation_files: List[UserValidationFile]
55+
cls,
56+
identity_id: str,
57+
user_validation_files: List[UserValidationFile],
58+
client=None,
5659
) -> List[Dict[str, Any]]:
5760
endpoint = cls._endpoint.format(identity_id=identity_id)
5861
files_metadata: List[Dict[str, Any]] = []
5962
files_with_types: List[Tuple[str, BinaryIO]] = []
6063
for file in user_validation_files:
6164
cls._append_file(files_metadata, file)
6265
files_with_types.append((get_file_type(file), file.content))
63-
resp = cls._client.post(
66+
resp = client.post(
6467
endpoint,
6568
data=dict(inputs=json.dumps(files_metadata)),
6669
files=files_with_types,

mati/resources/verifications.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime as dt
12
from dataclasses import dataclass, field
23
from typing import Any, ClassVar, Dict, List, Optional
34

@@ -18,11 +19,13 @@ class Verification(Resource):
1819
identity: Dict[str, str] = field(default_factory=dict)
1920
hasProblem: Optional[bool] = None
2021
computed: Optional[Dict[str, Any]] = None
22+
obfuscatedAt: Optional[dt.datetime] = None
2123

2224
@classmethod
23-
def retrieve(cls, verification_id: str) -> 'Verification':
25+
def retrieve(cls, verification_id: str, client=None) -> 'Verification':
26+
client = client or cls._client
2427
endpoint = f'{cls._endpoint}/{verification_id}'
25-
resp = cls._client.get(endpoint, token_score=cls._token_score)
28+
resp = client.get(endpoint, token_score=cls._token_score)
2629
docs = []
2730
for doc in resp['documents']:
2831
doc['steps'] = [

mati/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.2.9' # pragma: no cover
1+
__version__ = '0.2.10' # pragma: no cover
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
interactions:
2+
- request:
3+
body: grant_type=client_credentials
4+
headers:
5+
Accept:
6+
- '*/*'
7+
Accept-Encoding:
8+
- gzip, deflate
9+
Connection:
10+
- keep-alive
11+
Content-Length:
12+
- '29'
13+
Content-Type:
14+
- application/x-www-form-urlencoded
15+
User-Agent:
16+
- mati-python/0.2.9
17+
method: POST
18+
uri: https://api.getmati.com/oauth
19+
response:
20+
body:
21+
string: '{"access_token": "ACCESS_TOKEN", "expiresIn": 3600, "payload": {"user":
22+
{"_id": "ID", "firstName": "FIRST_NAME", "lastName": "LAST_NAME"}}}'
23+
headers:
24+
Connection:
25+
- keep-alive
26+
Content-Length:
27+
- '504'
28+
Content-Type:
29+
- application/json; charset=utf-8
30+
Date:
31+
- Mon, 06 Jan 2020 19:28:48 GMT
32+
X-Request-Id:
33+
- fba06368-b340-4386-8174-77cf8a1d428c
34+
status:
35+
code: 200
36+
message: OK
37+
- request:
38+
body: '{"metadata": {"nombres": "Georg Wilhelm", "primer_apellido": "Friedrich",
39+
"segundo_apellido": "Hegel", "dob": "1770-08-27"}}'
40+
headers:
41+
Accept:
42+
- '*/*'
43+
Accept-Encoding:
44+
- gzip, deflate
45+
Connection:
46+
- keep-alive
47+
Content-Length:
48+
- '124'
49+
Content-Type:
50+
- application/json
51+
User-Agent:
52+
- mati-python/0.2.9
53+
method: POST
54+
uri: https://api.getmati.com/v2/identities
55+
response:
56+
body:
57+
string: '{"_id":"5e138a7016dc68001b379e13","alive":null,"dateCreated":"2020-01-06T19:28:48.248Z","dateUpdated":"2020-01-06T19:28:48.248Z","metadata":{"nombres":"Georg
58+
Wilhelm","primer_apellido":"Friedrich","segundo_apellido":"Hegel","dob":"1770-08-27"},"status":"pending","user":"5cec5d4e69eb4d001b8544ce"}'
59+
headers:
60+
Connection:
61+
- keep-alive
62+
Content-Length:
63+
- '297'
64+
Content-Type:
65+
- application/json; charset=utf-8
66+
Date:
67+
- Mon, 06 Jan 2020 19:28:48 GMT
68+
X-Request-Id:
69+
- 5d621f09-0bae-4678-bcf6-e24f2b6f6aa1
70+
status:
71+
code: 200
72+
message: OK
73+
- request:
74+
body: grant_type=client_credentials
75+
headers:
76+
Accept:
77+
- '*/*'
78+
Accept-Encoding:
79+
- gzip, deflate
80+
Connection:
81+
- keep-alive
82+
Content-Length:
83+
- '29'
84+
Content-Type:
85+
- application/x-www-form-urlencoded
86+
User-Agent:
87+
- mati-python/0.2.9
88+
method: POST
89+
uri: https://api.getmati.com/oauth
90+
response:
91+
body:
92+
string: '{"access_token": "ACCESS_TOKEN", "expiresIn": 3600, "payload": {"user":
93+
{"_id": "ID", "firstName": "FIRST_NAME", "lastName": "LAST_NAME"}}}'
94+
headers:
95+
Connection:
96+
- keep-alive
97+
Content-Length:
98+
- '413'
99+
Content-Type:
100+
- application/json; charset=utf-8
101+
Date:
102+
- Mon, 06 Jan 2020 19:28:48 GMT
103+
X-Request-Id:
104+
- 78a64d42-ea9a-4216-9abb-64f93aef5f72
105+
status:
106+
code: 200
107+
message: OK
108+
- request:
109+
body: '{"metadata": {"nombres": "Georg Wilhelm", "primer_apellido": "Friedrich",
110+
"segundo_apellido": "Hegel", "dob": "1770-08-27"}}'
111+
headers:
112+
Accept:
113+
- '*/*'
114+
Accept-Encoding:
115+
- gzip, deflate
116+
Connection:
117+
- keep-alive
118+
Content-Length:
119+
- '124'
120+
Content-Type:
121+
- application/json
122+
User-Agent:
123+
- mati-python/0.2.9
124+
method: POST
125+
uri: https://api.getmati.com/v2/identities
126+
response:
127+
body:
128+
string: '{"_id":"5e138a7016dc68001b379e1a","alive":null,"dateCreated":"2020-01-06T19:28:48.778Z","dateUpdated":"2020-01-06T19:28:48.778Z","metadata":{"nombres":"Georg
129+
Wilhelm","primer_apellido":"Friedrich","segundo_apellido":"Hegel","dob":"1770-08-27"},"status":"pending","user":"5e00fdf522f92f001b69993c"}'
130+
headers:
131+
Connection:
132+
- keep-alive
133+
Content-Length:
134+
- '297'
135+
Content-Type:
136+
- application/json; charset=utf-8
137+
Date:
138+
- Mon, 06 Jan 2020 19:28:48 GMT
139+
X-Request-Id:
140+
- ef550cc0-6d92-464c-88b4-6bc860292634
141+
status:
142+
code: 200
143+
message: OK
144+
- request:
145+
body: null
146+
headers:
147+
Accept:
148+
- '*/*'
149+
Accept-Encoding:
150+
- gzip, deflate
151+
Connection:
152+
- keep-alive
153+
User-Agent:
154+
- mati-python/0.2.9
155+
method: GET
156+
uri: https://api.getmati.com/v2/identities/5e138a7016dc68001b379e13
157+
response:
158+
body:
159+
string: '{"_id":"5e138a7016dc68001b379e13","alive":null,"dateCreated":"2020-01-06T19:28:48.248Z","dateUpdated":"2020-01-06T19:28:48.248Z","metadata":{"nombres":"Georg
160+
Wilhelm","primer_apellido":"Friedrich","segundo_apellido":"Hegel","dob":"1770-08-27"},"status":"pending","user":"5cec5d4e69eb4d001b8544ce"}'
161+
headers:
162+
Connection:
163+
- keep-alive
164+
Content-Length:
165+
- '297'
166+
Content-Type:
167+
- application/json; charset=utf-8
168+
Date:
169+
- Mon, 06 Jan 2020 19:28:48 GMT
170+
X-Request-Id:
171+
- f003a554-315f-4a72-9a27-5c475e6b7b9a
172+
status:
173+
code: 200
174+
message: OK
175+
- request:
176+
body: null
177+
headers:
178+
Accept:
179+
- '*/*'
180+
Accept-Encoding:
181+
- gzip, deflate
182+
Connection:
183+
- keep-alive
184+
User-Agent:
185+
- mati-python/0.2.9
186+
method: GET
187+
uri: https://api.getmati.com/v2/identities/5e138a7016dc68001b379e1a
188+
response:
189+
body:
190+
string: '{"_id":"5e138a7016dc68001b379e1a","alive":null,"dateCreated":"2020-01-06T19:28:48.778Z","dateUpdated":"2020-01-06T19:28:48.778Z","metadata":{"nombres":"Georg
191+
Wilhelm","primer_apellido":"Friedrich","segundo_apellido":"Hegel","dob":"1770-08-27"},"status":"pending","user":"5e00fdf522f92f001b69993c"}'
192+
headers:
193+
Connection:
194+
- keep-alive
195+
Content-Length:
196+
- '297'
197+
Content-Type:
198+
- application/json; charset=utf-8
199+
Date:
200+
- Mon, 06 Jan 2020 19:28:49 GMT
201+
X-Request-Id:
202+
- 3369b285-4e7a-42f0-a75b-19c1ab3f9ae7
203+
status:
204+
code: 200
205+
message: OK
206+
version: 1

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,13 @@ def vcr_config() -> dict:
156156

157157
@pytest.fixture
158158
def client() -> Generator:
159-
# using credentials from env
160-
yield Client()
159+
yield Client('api_key', 'secret_key')
161160

162161

163162
@pytest.fixture
164163
def identity(client: Client) -> Generator:
165164
yield client.identities.create(
165+
client=client,
166166
nombres='Georg Wilhelm',
167167
primer_apellido='Friedrich',
168168
segundo_apellido='Hegel',

tests/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@pytest.mark.vcr
88
@pytest.mark.parametrize('score', [None, 'identity'])
99
def test_client_renew_access_token(score):
10-
client = Client()
10+
client = Client('api_key', 'secret_key')
1111
assert client.bearer_tokens.get(score) is None
1212
client.get_valid_bearer_token(score)
1313
assert not client.bearer_tokens[score].expired

0 commit comments

Comments
 (0)