1+ from __future__ import annotations
2+
3+ from abc import ABC
4+ from dataclasses import asdict
15import datetime
26import json
37import logging
4- from abc import ABC
5- from dataclasses import asdict
6- from typing import Type , Optional , Any , TypeVar
8+ from typing import Any , Optional , Type , TypeVar
79
810import dacite
911import httpx
12+ from httpx ._types import HeaderTypes , QueryParamTypes
1013import tenacity
11- from httpx ._types import QueryParamTypes , HeaderTypes
1214
1315from saic_ismart_client_ng .api .schema import LoginResp
1416from saic_ismart_client_ng .crypto_utils import sha1_hex_digest
@@ -112,7 +114,7 @@ async def __execute_api_call(
112114 headers : Optional [HeaderTypes ] = None ,
113115 allow_null_body : bool = False ,
114116 ) -> Optional [T ]:
115- url = f"{ self .__configuration .base_uri } { path [ 1 :] if path . startswith ('/' ) else path } "
117+ url = f"{ self .__configuration .base_uri } { path . removeprefix ('/' )} "
116118 json_body = asdict (body ) if body else None
117119 req = httpx .Request (
118120 method , url , params = params , headers = headers , data = form_body , json = json_body
@@ -195,27 +197,24 @@ async def __deserialize(
195197 event_id = request_event_id ,
196198 return_code = return_code ,
197199 )
198- else :
199- logger .error (
200- f"API call return code is not acceptable: { return_code } : { response .text } . Headers: { response .headers } "
201- )
202- raise SaicApiException (error_message , return_code = return_code )
200+ logger .error (
201+ f"API call return code is not acceptable: { return_code } : { response .text } . Headers: { response .headers } "
202+ )
203+ raise SaicApiException (error_message , return_code = return_code )
203204
204205 if data_class is None :
205206 return None
206- elif "data" in json_data :
207+ if "data" in json_data :
207208 if data_class is str :
208209 return json .dumps (json_data ["data" ])
209- elif data_class is dict :
210+ if data_class is dict :
210211 return json_data ["data" ]
211- else :
212- return dacite .from_dict (data_class , json_data ["data" ])
213- elif allow_null_body :
212+ return dacite .from_dict (data_class , json_data ["data" ])
213+ if allow_null_body :
214214 return None
215- else :
216- raise SaicApiException (
217- f"Failed to deserialize response, missing 'data' field: { response .text } "
218- )
215+ raise SaicApiException (
216+ f"Failed to deserialize response, missing 'data' field: { response .text } "
217+ )
219218
220219 except SaicApiException as se :
221220 raise se
@@ -230,16 +229,14 @@ async def __deserialize(
230229 raise SaicLogoutException (
231230 response .text , response .status_code
232231 ) from e
233- else :
234- logger .error (
235- f"API call failed: { response .status_code } { response .text } " ,
236- exc_info = e ,
237- )
238- raise SaicApiException (response .text , response .status_code ) from e
239- else :
240- raise SaicApiException (
241- f"Failed to deserialize response: { e } . Original json was { response .text } "
242- ) from e
232+ logger .error (
233+ f"API call failed: { response .status_code } { response .text } " ,
234+ exc_info = e ,
235+ )
236+ raise SaicApiException (response .text , response .status_code ) from e
237+ raise SaicApiException (
238+ f"Failed to deserialize response: { e } . Original json was { response .text } "
239+ ) from e
243240
244241 def logout (self ):
245242 self .__api_client .user_token = ""
@@ -277,13 +274,12 @@ def saic_api_retry_policy(retry_state):
277274 if isinstance (wrapped_exception , SaicApiRetryException ):
278275 logger .debug ("Retrying since we got SaicApiRetryException" )
279276 return True
280- elif isinstance (wrapped_exception , SaicLogoutException ):
277+ if isinstance (wrapped_exception , SaicLogoutException ):
281278 logger .error ("Not retrying since we got logged out" )
282279 return False
283- elif isinstance (wrapped_exception , SaicApiException ):
280+ if isinstance (wrapped_exception , SaicApiException ):
284281 logger .error ("Not retrying since we got a generic exception" )
285282 return False
286- else :
287- logger .error (f"Not retrying { retry_state .args } { wrapped_exception } " )
288- return False
283+ logger .error (f"Not retrying { retry_state .args } { wrapped_exception } " )
284+ return False
289285 return False
0 commit comments