66import httpx
77
88from obelisk .exceptions import AuthenticationError
9- from obelisk .strategies .retry import RetryStrategy , \
10- NoRetryStrategy
9+ from obelisk .strategies .retry import RetryStrategy , NoRetryStrategy
1110from obelisk .types import ObeliskKind
1211
1312
@@ -32,27 +31,33 @@ class BaseClient:
3231
3332 log : logging .Logger
3433
35- def __init__ (self , client : str , secret : str ,
36- retry_strategy : RetryStrategy = NoRetryStrategy (),
37- kind : ObeliskKind = ObeliskKind .CLASSIC ) -> None :
34+ def __init__ (
35+ self ,
36+ client : str ,
37+ secret : str ,
38+ retry_strategy : RetryStrategy = NoRetryStrategy (),
39+ kind : ObeliskKind = ObeliskKind .CLASSIC ,
40+ ) -> None :
3841 self ._client = client
3942 self ._secret = secret
4043 self .retry_strategy = retry_strategy
4144 self .kind = kind
4245
43- self .log = logging .getLogger (' obelisk' )
46+ self .log = logging .getLogger (" obelisk" )
4447
4548 async def _get_token (self ):
46- auth_string = str (base64 .b64encode (
47- f'{ self ._client } :{ self ._secret } ' .encode ('utf-8' )), 'utf-8' )
49+ auth_string = str (
50+ base64 .b64encode (f"{ self ._client } :{ self ._secret } " .encode ("utf-8" )), "utf-8"
51+ )
4852 headers = {
49- ' Authorization' : f' Basic { auth_string } ' ,
50- ' Content-Type' : ('application/json'
51- if self . kind . use_json_auth else ' application/x-www-form-urlencoded' )
52- }
53- payload = {
54- 'grant_type' : 'client_credentials'
53+ " Authorization" : f" Basic { auth_string } " ,
54+ " Content-Type" : (
55+ " application/json"
56+ if self . kind . use_json_auth
57+ else "application/x-www-form-urlencoded"
58+ ),
5559 }
60+ payload = {"grant_type" : "client_credentials" }
5661
5762 async with httpx .AsyncClient () as client :
5863 response = None
@@ -64,7 +69,8 @@ async def _get_token(self):
6469 self .kind .token_url ,
6570 json = payload if self .kind .use_json_auth else None ,
6671 data = payload if not self .kind .use_json_auth else None ,
67- headers = headers )
72+ headers = headers ,
73+ )
6874
6975 response = request .json ()
7076 except Exception as e :
@@ -76,29 +82,33 @@ async def _get_token(self):
7682 raise last_error
7783
7884 if request .status_code != 200 :
79- if ' error' in response :
85+ if " error" in response :
8086 self .log .warning (f"Could not authenticate, { response ['error' ]} " )
8187 raise AuthenticationError
8288
83- self ._token = response ['access_token' ]
84- self ._token_expires = (datetime .now ()
85- + timedelta (seconds = response ['expires_in' ]))
89+ self ._token = response ["access_token" ]
90+ self ._token_expires = datetime .now () + timedelta (
91+ seconds = response ["expires_in" ]
92+ )
8693
8794 async def _verify_token (self ):
88- if (self ._token is None
89- or self ._token_expires < (datetime .now () - self .grace_period )):
95+ if self ._token is None or self ._token_expires < (
96+ datetime .now () - self .grace_period
97+ ):
9098 retry = self .retry_strategy .make ()
9199 first = True
92100 while first or await retry .should_retry ():
93101 first = False
94102 try :
95103 await self ._get_token ()
96104 return
97- except :
105+ except : # noqa: E722
106+ self .log .info ("excepted, Retrying token fetch" )
98107 continue
99108
100- async def http_post (self , url : str , data : Any = None ,
101- params : Optional [dict ] = None ) -> httpx .Response :
109+ async def http_post (
110+ self , url : str , data : Any = None , params : Optional [dict ] = None
111+ ) -> httpx .Response :
102112 """
103113 Send an HTTP POST request to Obelisk,
104114 with proper auth.
@@ -113,8 +123,8 @@ async def http_post(self, url: str, data: Any = None,
113123 await self ._verify_token ()
114124
115125 headers = {
116- ' Authorization' : f' Bearer { self ._token } ' ,
117- ' Content-Type' : ' application/json'
126+ " Authorization" : f" Bearer { self ._token } " ,
127+ " Content-Type" : " application/json" ,
118128 }
119129 if params is None :
120130 params = {}
@@ -127,11 +137,12 @@ async def http_post(self, url: str, data: Any = None,
127137 self .log .debug (f"Retrying, last response: { response .status_code } " )
128138
129139 try :
130- response = await client .post (url ,
131- json = data ,
132- params = {k : v for k , v in params .items () if
133- v is not None },
134- headers = headers )
140+ response = await client .post (
141+ url ,
142+ json = data ,
143+ params = {k : v for k , v in params .items () if v is not None },
144+ headers = headers ,
145+ )
135146
136147 if response .status_code // 100 == 2 :
137148 return response
@@ -144,7 +155,6 @@ async def http_post(self, url: str, data: Any = None,
144155 raise last_error
145156 return response
146157
147-
148158 async def http_get (self , url : str , params : Optional [dict ] = None ) -> httpx .Response :
149159 """
150160 Send an HTTP GET request to Obelisk,
@@ -160,8 +170,8 @@ async def http_get(self, url: str, params: Optional[dict] = None) -> httpx.Respo
160170 await self ._verify_token ()
161171
162172 headers = {
163- ' Authorization' : f' Bearer { self ._token } ' ,
164- ' Content-Type' : ' application/json'
173+ " Authorization" : f" Bearer { self ._token } " ,
174+ " Content-Type" : " application/json" ,
165175 }
166176 if params is None :
167177 params = {}
@@ -174,10 +184,11 @@ async def http_get(self, url: str, params: Optional[dict] = None) -> httpx.Respo
174184 self .log .debug (f"Retrying, last response: { response .status_code } " )
175185
176186 try :
177- response = await client .get (url ,
178- params = {k : v for k , v in params .items () if
179- v is not None },
180- headers = headers )
187+ response = await client .get (
188+ url ,
189+ params = {k : v for k , v in params .items () if v is not None },
190+ headers = headers ,
191+ )
181192
182193 if response .status_code // 100 == 2 :
183194 return response
0 commit comments