11import os
2- from typing import Optional , Tuple
2+ from typing import Optional , Tuple , Union
33from urllib .parse import urljoin
44
55import requests
6+ from aws_requests_auth .aws_auth import AWSRequestsAuth
67from cuenca_validations .typing import (
78 ClientRequestParams ,
89 DictStrAny ,
1314from ..exc import CuencaResponseException
1415from ..version import API_VERSION , CLIENT_VERSION
1516
16- API_URL = 'https://api.cuenca.com'
17- SANDBOX_URL = 'https://sandbox.cuenca.com'
17+ API_HOST = 'api.cuenca.com'
18+ SANDBOX_HOST = 'sandbox.cuenca.com'
19+ AWS_DEFAULT_REGION = 'us-east-1'
20+ AWS_SERVICE = 'execute-api'
1821
1922
2023class Session :
2124
22- base_url : str
23- auth : Tuple [str , str ]
24- webhook_secret : Optional [str ]
25+ host : str = API_HOST
26+ basic_auth : Tuple [str , str ]
27+ iam_auth : Optional [AWSRequestsAuth ] = None
2528 session : requests .Session
2629
2730 def __init__ (self ):
@@ -32,30 +35,72 @@ def __init__(self):
3235 'User-Agent' : f'cuenca-python/{ CLIENT_VERSION } ' ,
3336 }
3437 )
35- self .base_url = API_URL
38+
39+ # basic auth
3640 api_key = os .getenv ('CUENCA_API_KEY' , '' )
3741 api_secret = os .getenv ('CUENCA_API_SECRET' , '' )
38- self .webhook_secret = os .getenv ('CUENCA_WEBHOOK_SECRET' )
39- self .auth = (api_key , api_secret )
42+ self .basic_auth = (api_key , api_secret )
43+
44+ # IAM auth
45+ aws_access_key = os .getenv ('AWS_ACCESS_KEY_ID' , '' )
46+ aws_secret_access_key = os .getenv ('AWS_SECRET_ACCESS_KEY' , '' )
47+ aws_region = os .getenv ('AWS_DEFAULT_REGION' , AWS_DEFAULT_REGION )
48+ if aws_access_key and aws_secret_access_key :
49+ self .iam_auth = AWSRequestsAuth (
50+ aws_access_key = aws_access_key ,
51+ aws_secret_access_key = aws_secret_access_key ,
52+ aws_host = self .host ,
53+ aws_region = aws_region ,
54+ aws_service = AWS_SERVICE ,
55+ )
56+
57+ @property
58+ def auth (self ) -> Union [AWSRequestsAuth , Tuple [str , str ]]:
59+ # preference to basic auth
60+ return self .basic_auth if all (self .basic_auth ) else self .iam_auth
4061
4162 def configure (
4263 self ,
4364 api_key : Optional [str ] = None ,
4465 api_secret : Optional [str ] = None ,
45- webhook_secret : Optional [str ] = None ,
66+ aws_access_key : Optional [str ] = None ,
67+ aws_secret_access_key : Optional [str ] = None ,
68+ aws_region : str = AWS_DEFAULT_REGION ,
4669 sandbox : Optional [bool ] = None ,
4770 ):
4871 """
4972 This allows us to instantiate the http client when importing the
5073 client library and configure it later. It's also useful when rolling
5174 the api key
5275 """
53- self .auth = (api_key or self .auth [0 ], api_secret or self .auth [1 ])
54- self .webhook_secret = webhook_secret or self .webhook_secret
5576 if sandbox is False :
56- self .base_url = API_URL
77+ self .host = API_HOST
5778 elif sandbox is True :
58- self .base_url = SANDBOX_URL
79+ self .host = SANDBOX_HOST
80+
81+ # basic auth
82+ self .basic_auth = (
83+ api_key or self .basic_auth [0 ],
84+ api_secret or self .basic_auth [1 ],
85+ )
86+
87+ # IAM auth
88+ if self .iam_auth is not None :
89+ self .iam_auth .aws_access_key = (
90+ aws_access_key or self .iam_auth .aws_access_key
91+ )
92+ self .iam_auth .aws_secret_access_key = (
93+ aws_secret_access_key or self .iam_auth .aws_secret_access_key
94+ )
95+ self .iam_auth .aws_region = aws_region or self .iam_auth .aws_region
96+ elif aws_access_key and aws_secret_access_key :
97+ self .iam_auth = AWSRequestsAuth (
98+ aws_access_key = aws_access_key ,
99+ aws_secret_access_key = aws_secret_access_key ,
100+ aws_host = self .host ,
101+ aws_region = aws_region ,
102+ aws_service = AWS_SERVICE ,
103+ )
59104
60105 def get (
61106 self , endpoint : str , params : ClientRequestParams = None ,
@@ -65,6 +110,9 @@ def get(
65110 def post (self , endpoint : str , data : DictStrAny ) -> DictStrAny :
66111 return self .request ('post' , endpoint , data = data )
67112
113+ def patch (self , endpoint : str , data : DictStrAny ) -> DictStrAny :
114+ return self .request ('patch' , endpoint , data = data )
115+
68116 def delete (self , endpoint : str , data : OptionalDict = None ) -> DictStrAny :
69117 return self .request ('delete' , endpoint , data = data )
70118
@@ -78,7 +126,7 @@ def request(
78126 ) -> DictStrAny :
79127 resp = self .session .request (
80128 method = method ,
81- url = self .base_url + urljoin ('/' , endpoint ),
129+ url = 'https://' + self .host + urljoin ('/' , endpoint ),
82130 auth = self .auth ,
83131 json = data ,
84132 params = params ,
0 commit comments