Skip to content

Commit 6d733ae

Browse files
Ricardofelipao-mxEC2 Default User
authored
Update api key (#105)
* Adding Updateable to api_keys * Lint * Lint * Tests * Lint * This is a way to solve it... * This one is clearer * Lint and test * Another lint * Fix test * Add another test just in case and its corresponding fix * To be clear * Add customized session * Customized session for ApiKey creation * Bump versions * version * remove validate method * tests * lint * remove metadata * test * fix * casseettes * cassettes * updated dependency * updated cuenca-validations * added local session Co-authored-by: Felipe López <flh.1989@gmail.com> Co-authored-by: EC2 Default User <ec2-user@ip-10-5-163-56.us-east-2.compute.internal>
1 parent 3fafefc commit 6d733ae

File tree

9 files changed

+160
-100
lines changed

9 files changed

+160
-100
lines changed

cuenca/resources/api_keys.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import datetime as dt
22
from typing import ClassVar, Optional, cast
33

4-
from cuenca_validations.types import ApiKeyQuery
4+
from cuenca_validations.types import ApiKeyQuery, ApiKeyUpdateRequest
55
from pydantic.dataclasses import dataclass
66

7-
from ..http import session
8-
from .base import Creatable, Queryable, Retrievable
7+
from ..http import Session, session as global_session
8+
from .base import Creatable, Queryable, Retrievable, Updateable
99

1010

1111
@dataclass
12-
class ApiKey(Creatable, Queryable, Retrievable):
12+
class ApiKey(Creatable, Queryable, Retrievable, Updateable):
1313
_resource: ClassVar = 'api_keys'
1414
_query_params: ClassVar = ApiKeyQuery
1515

1616
secret: str
1717
deactivated_at: Optional[dt.datetime]
18+
user_id: Optional[str]
1819

1920
@property
2021
def active(self) -> bool:
@@ -24,11 +25,17 @@ def active(self) -> bool:
2425
)
2526

2627
@classmethod
27-
def create(cls) -> 'ApiKey':
28-
return cast('ApiKey', cls._create())
28+
def create(cls, *, session: Session = global_session) -> 'ApiKey':
29+
return cast('ApiKey', cls._create(session=session))
2930

3031
@classmethod
31-
def deactivate(cls, api_key_id: str, minutes: int = 0) -> 'ApiKey':
32+
def deactivate(
33+
cls,
34+
api_key_id: str,
35+
minutes: int = 0,
36+
*,
37+
session: Session = global_session,
38+
) -> 'ApiKey':
3239
"""
3340
deactivate an ApiKey in a certain number of minutes. If minutes is
3441
negative, the API will treat it the same as 0. You can't deactivate
@@ -39,3 +46,20 @@ def deactivate(cls, api_key_id: str, minutes: int = 0) -> 'ApiKey':
3946
url = cls._resource + f'/{api_key_id}'
4047
resp = session.delete(url, dict(minutes=minutes))
4148
return cast('ApiKey', cls._from_dict(resp))
49+
50+
@classmethod
51+
def update(
52+
cls,
53+
api_key_id: str,
54+
metadata: Optional[dict] = None,
55+
user_id: Optional[str] = None,
56+
*,
57+
session: Session = global_session,
58+
) -> 'ApiKey':
59+
"""
60+
If the current user has enough permissions, it associates an ApiKey to
61+
the `user_id` or updates the correspoding metadata
62+
"""
63+
req = ApiKeyUpdateRequest(metadata=metadata, user_id=user_id)
64+
resp = cls._update(api_key_id, **req.dict(), session=session)
65+
return cast('ApiKey', resp)

cuenca/resources/cards.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def update(
5151
status: Optional[CardStatus] = None,
5252
*,
5353
session: Session = global_session,
54-
):
54+
) -> 'Card':
5555
"""
5656
Updates card properties that are not sensitive or fixed data. It allows
5757
reconfigure properties like status, and manufacturer.
@@ -65,9 +65,7 @@ def update(
6565
req = CardUpdateRequest(
6666
user_id=user_id, ledger_account_id=ledger_account_id, status=status
6767
)
68-
resp = cls._update(
69-
card_id, session=session, **req.dict(exclude_none=True)
70-
)
68+
resp = cls._update(card_id, session=session, **req.dict())
7169
return cast('Card', resp)
7270

7371
@classmethod

cuenca/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = '0.6.1'
1+
__version__ = '0.6.2'
22
CLIENT_VERSION = __version__
33
API_VERSION = '2020-03-19'

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
requests==2.25.0
2-
cuenca-validations==0.7.2
2+
cuenca-validations==0.7.5
33
dataclasses>=0.7;python_version<"3.7"

tests/resources/cassettes/test_api_key_deactivate.yaml

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
interactions:
22
- request:
3-
body: null
3+
body: '{}'
44
headers:
55
Accept:
66
- '*/*'
@@ -10,41 +10,39 @@ interactions:
1010
- DUMMY
1111
Connection:
1212
- keep-alive
13+
Content-Length:
14+
- '2'
15+
Content-Type:
16+
- application/json
1317
User-Agent:
14-
- cuenca-python/0.0.1dev0
18+
- cuenca-python/0.6.2.dev1
1519
X-Cuenca-Api-Version:
1620
- '2020-03-19'
17-
method: GET
18-
uri: https://sandbox.cuenca.com/api_keys/test
21+
method: POST
22+
uri: https://sandbox.cuenca.com/api_keys
1923
response:
2024
body:
21-
string: '{"id":"test","created_at":"2020-05-11T17:20:25.603000","secret":"********","deactivated_at":null}'
25+
string: '{"id":"AK65Xx4QRmSp-kY-svK_kW6g","created_at":"2021-02-11T20:06:17.348677","secret":"vZKDb2BFb17lmAQTaljKd5whcncJ0TihswF2iKxpHPvaJv7z-R12WwMrAuobko0VXlmTUxVPqDP3TGBlKS_Hyw","deactivated_at":"2212-10-08T19:54:42.308379","user":{"id":"USJaz512A7ROOh7N3pqKGMZg","created_at":"2021-02-11T19:44:54.016164"},"updated_at":"2021-02-11T19:54:42.308426","user_id":"USJaz512A7ROOh7N3pqKGMZg"}'
2226
headers:
2327
Connection:
2428
- keep-alive
2529
Content-Length:
26-
- '119'
30+
- '381'
2731
Content-Type:
2832
- application/json
2933
Date:
30-
- Mon, 11 May 2020 18:30:32 GMT
31-
Via:
32-
- 1.1 d8c8706cff3de7db4b8e8b9726774938.cloudfront.net (CloudFront)
33-
X-Amz-Cf-Id:
34-
- AKuzZDU7v-hmyGu-9zB9VHgy9S4pbSGqKNIJJMcPnRqzIm8Crk1fWQ==
35-
X-Amz-Cf-Pop:
36-
- DFW3-C1
34+
- Thu, 11 Feb 2021 20:06:17 GMT
35+
Server:
36+
- nginx/1.18.0
3737
X-Amzn-Trace-Id:
38-
- Root=1-5eb999c8-e36f1e7c4aa751f8ce4452c0;Sampled=0
39-
X-Cache:
40-
- Miss from cloudfront
38+
- Root=1-60258e39-e3ac93f6c3a844adbb028c86;Sampled=0
4139
x-amz-apigw-id:
42-
- MYT3VGxDoAMFbZQ=
40+
- amMo8HWMiYcF8Mw=
4341
x-amzn-RequestId:
44-
- d1f6ab21-3e4f-493e-8d9b-d1aff4ef82a9
42+
- 14929ccd-1f3b-4e5a-9364-fedc0e0051c3
4543
status:
46-
code: 200
47-
message: OK
44+
code: 201
45+
message: Created
4846
- request:
4947
body: '{"minutes": 0}'
5048
headers:
@@ -61,37 +59,31 @@ interactions:
6159
Content-Type:
6260
- application/json
6361
User-Agent:
64-
- cuenca-python/0.0.1dev0
62+
- cuenca-python/0.6.2.dev1
6563
X-Cuenca-Api-Version:
6664
- '2020-03-19'
6765
method: DELETE
68-
uri: https://sandbox.cuenca.com/api_keys/test
66+
uri: https://sandbox.cuenca.com/api_keys/AK65Xx4QRmSp-kY-svK_kW6g
6967
response:
7068
body:
71-
string: '{"id":"test","created_at":"2020-05-11T17:20:25.603000","secret":"********","deactivated_at":"2020-05-11T18:30:35.210967"}'
69+
string: '{"id":"AK65Xx4QRmSp-kY-svK_kW6g","created_at":"2021-02-11T20:06:17.348677","secret":"********","deactivated_at":"2021-02-11T20:06:17.391434","user":{"id":"USJaz512A7ROOh7N3pqKGMZg","created_at":"2021-02-11T19:44:54.016164"},"updated_at":"2021-02-11T20:06:17.391471","user_id":"USJaz512A7ROOh7N3pqKGMZg"}'
7270
headers:
7371
Connection:
7472
- keep-alive
7573
Content-Length:
76-
- '143'
74+
- '303'
7775
Content-Type:
7876
- application/json
7977
Date:
80-
- Mon, 11 May 2020 18:30:35 GMT
81-
Via:
82-
- 1.1 d8c8706cff3de7db4b8e8b9726774938.cloudfront.net (CloudFront)
83-
X-Amz-Cf-Id:
84-
- DFKQoRapavD6PEHAapEf_BvZiywXiIK3Yi3Fh-kkCJe6Yu1lKX8IvA==
85-
X-Amz-Cf-Pop:
86-
- DFW3-C1
78+
- Thu, 11 Feb 2021 20:06:17 GMT
79+
Server:
80+
- nginx/1.18.0
8781
X-Amzn-Trace-Id:
88-
- Root=1-5eb999cb-890940501ec67728f7f59fa0;Sampled=0
89-
X-Cache:
90-
- Miss from cloudfront
82+
- Root=1-60258e39-de7477cedab1524a0f820190;Sampled=0
9183
x-amz-apigw-id:
92-
- MYT3wGh9oAMFTSQ=
84+
- amMo-F9aCYcFUMw=
9385
x-amzn-RequestId:
94-
- b87437d5-18f4-4e7b-adf3-4302d3f144b3
86+
- 39d35856-da34-4e97-b249-06c3eb396ad1
9587
status:
9688
code: 200
9789
message: OK
@@ -107,37 +99,31 @@ interactions:
10799
Connection:
108100
- keep-alive
109101
User-Agent:
110-
- cuenca-python/0.0.1dev0
102+
- cuenca-python/0.6.2.dev1
111103
X-Cuenca-Api-Version:
112104
- '2020-03-19'
113105
method: GET
114-
uri: https://sandbox.cuenca.com/api_keys/test
106+
uri: https://sandbox.cuenca.com/api_keys/AK65Xx4QRmSp-kY-svK_kW6g
115107
response:
116108
body:
117-
string: '{"id":"test","created_at":"2020-05-11T17:20:25.603000","secret":"********","deactivated_at":"2020-05-11T18:30:35.210000"}'
109+
string: '{"id":"AK65Xx4QRmSp-kY-svK_kW6g","created_at":"2021-02-11T20:06:17.348677","secret":"********","deactivated_at":"2021-02-11T20:06:17.391434","user":{"id":"USJaz512A7ROOh7N3pqKGMZg","created_at":"2021-02-11T19:44:54.016164"},"updated_at":"2021-02-11T20:06:17.391471","user_id":"USJaz512A7ROOh7N3pqKGMZg"}'
118110
headers:
119111
Connection:
120112
- keep-alive
121113
Content-Length:
122-
- '143'
114+
- '303'
123115
Content-Type:
124116
- application/json
125117
Date:
126-
- Mon, 11 May 2020 18:30:35 GMT
127-
Via:
128-
- 1.1 d8c8706cff3de7db4b8e8b9726774938.cloudfront.net (CloudFront)
129-
X-Amz-Cf-Id:
130-
- Q6iMsqOj5TZMk9XnoMk1Pj_bfQ2ycqs1CrTcZPsUk5VXaUMVeaprIA==
131-
X-Amz-Cf-Pop:
132-
- DFW3-C1
118+
- Thu, 11 Feb 2021 20:06:17 GMT
119+
Server:
120+
- nginx/1.18.0
133121
X-Amzn-Trace-Id:
134-
- Root=1-5eb999cb-ae60e1a60989f617bbbb1675;Sampled=0
135-
X-Cache:
136-
- Miss from cloudfront
122+
- Root=1-60258e39-158b9de5eb2b3d7bc6695c95;Sampled=0
137123
x-amz-apigw-id:
138-
- MYT3yEZqIAMFyvw=
124+
- amMo_E6-CYcFaxw=
139125
x-amzn-RequestId:
140-
- 7f4b362f-6e5d-4867-883a-2b2b44e12852
126+
- 9017ff70-2871-431e-9c96-f75041573e60
141127
status:
142128
code: 200
143129
message: OK

tests/resources/cassettes/test_api_keys_create.yaml

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,31 @@ interactions:
1515
Content-Type:
1616
- application/json
1717
User-Agent:
18-
- cuenca-python/0.0.1dev0
18+
- cuenca-python/0.6.2.dev1
1919
X-Cuenca-Api-Version:
2020
- '2020-03-19'
2121
method: POST
2222
uri: https://sandbox.cuenca.com/api_keys
2323
response:
2424
body:
25-
string: '{"id":"PKjtv8Qn-3Tp2Eh1bBzXphOg==","created_at":"2020-05-11T18:25:36.369866","secret":"fcLkDkymPF9nJloQfEbYwnBthMtrwFssxyuzpMs-qIDEnmJNAr8nC3_xkC-MY-reV6WjT10DLwpF_v_G4MqBPw","deactivated_at":null}'
25+
string: '{"id":"AKEvJ9OckUTFCN77vkQRLPIA","created_at":"2021-02-11T20:07:55.891354","secret":"Vl6FcGMjWj9MULfOwKdSR-X3NRX97m1o2EJ2Aej_18PQNLwHcU5KGAPHikiiLpTJTZ3Z_EnxJZKL9OlfKveq5Q","deactivated_at":"2212-10-08T19:54:42.308379","user":{"id":"USJaz512A7ROOh7N3pqKGMZg","created_at":"2021-02-11T19:44:54.016164"},"updated_at":"2021-02-11T19:54:42.308426","user_id":"USJaz512A7ROOh7N3pqKGMZg"}'
2626
headers:
2727
Connection:
2828
- keep-alive
2929
Content-Length:
30-
- '197'
30+
- '381'
3131
Content-Type:
3232
- application/json
3333
Date:
34-
- Mon, 11 May 2020 18:25:38 GMT
35-
Via:
36-
- 1.1 991cddfa34f7096aab42079b287a0629.cloudfront.net (CloudFront)
37-
X-Amz-Cf-Id:
38-
- C3GZwW0t28QJ75J-3Md6RzRHjehrab6-GVSBYzCli7UMhu6rsPoY0w==
39-
X-Amz-Cf-Pop:
40-
- DFW3-C1
34+
- Thu, 11 Feb 2021 20:07:55 GMT
35+
Server:
36+
- nginx/1.18.0
4137
X-Amzn-Trace-Id:
42-
- Root=1-5eb998a0-5b85148fe075039e951ce376;Sampled=0
43-
X-Cache:
44-
- Miss from cloudfront
38+
- Root=1-60258e9b-d1e9ed255cca393920a29b81;Sampled=0
4539
x-amz-apigw-id:
46-
- MYTJEE4iIAMFyDA=
40+
- amM4WHXDiYcFcog=
4741
x-amzn-RequestId:
48-
- 98c2462c-9c8f-4f04-a590-fc76d96cdca2
42+
- 3c0c47f8-198d-4bab-a592-009f362f5c1c
4943
status:
5044
code: 201
5145
message: Created

tests/resources/cassettes/test_api_keys_retrieve.yaml

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,31 @@ interactions:
1111
Connection:
1212
- keep-alive
1313
User-Agent:
14-
- cuenca-python/0.0.1dev0
14+
- cuenca-python/0.6.2.dev1
1515
X-Cuenca-Api-Version:
1616
- '2020-03-19'
1717
method: GET
18-
uri: https://sandbox.cuenca.com/api_keys/test
18+
uri: https://sandbox.cuenca.com/api_keys/AKMPSxy2UeSKqU1J6spDNwqA
1919
response:
2020
body:
21-
string: '{"id":"test","created_at":"2020-05-04T19:43:40.439000","secret":"********","deactivated_at":null}'
21+
string: '{"id":"AKMPSxy2UeSKqU1J6spDNwqA","created_at":"2021-02-11T19:45:52.826286","secret":"********","deactivated_at":"2212-10-08T19:37:22.722505","user":{"id":"USJaz512A7ROOh7N3pqKGMZg","created_at":"2021-02-11T19:44:54.016164"},"updated_at":"2021-02-11T19:54:42.308426","user_id":"USJaz512A7ROOh7N3pqKGMZg"}'
2222
headers:
2323
Connection:
2424
- keep-alive
2525
Content-Length:
26-
- '119'
26+
- '303'
2727
Content-Type:
2828
- application/json
2929
Date:
30-
- Mon, 11 May 2020 18:25:38 GMT
31-
Via:
32-
- 1.1 e11ff5647028a0e212255f92e5436d8b.cloudfront.net (CloudFront)
33-
X-Amz-Cf-Id:
34-
- ZXFzIAdYi9neivcMO1KZ1Lpq2SBf-9Q-bJDPujzzryBSvb8mxAxgCQ==
35-
X-Amz-Cf-Pop:
36-
- DFW3-C1
30+
- Thu, 11 Feb 2021 20:09:25 GMT
31+
Server:
32+
- nginx/1.18.0
3733
X-Amzn-Trace-Id:
38-
- Root=1-5eb998a2-be6c915eeb0eee745d58beb2;Sampled=0
39-
X-Cache:
40-
- Miss from cloudfront
34+
- Root=1-60258ef5-e8d47a2a59f540b54f612e7e;Sampled=0
4135
x-amz-apigw-id:
42-
- MYTJZGfToAMFm4w=
36+
- amNGaEI7iYcFtdw=
4337
x-amzn-RequestId:
44-
- 9c5352f0-f3df-41f2-9618-07c095ff4f71
38+
- 35f3a24b-e9d9-4923-8442-a88722fbc5da
4539
status:
4640
code: 200
4741
message: OK

0 commit comments

Comments
 (0)