3333 from saic_ismart_client_ng .listener import SaicApiListener
3434 from saic_ismart_client_ng .model import SaicApiConfiguration
3535
36-
3736 class IsDataclass (Protocol ):
3837 # as already noted in comments, checking for this attribute is currently
3938 # the most reliable way to ascertain that something is a dataclass
4039 __dataclass_fields__ : ClassVar [dict [str , Any ]]
4140
42-
4341 T = TypeVar ("T" , bound = IsDataclass )
4442
4543logger = logging .getLogger (__name__ )
4644
4745
4846class AbstractSaicApi :
4947 def __init__ (
50- self ,
51- configuration : SaicApiConfiguration ,
52- listener : SaicApiListener | None = None ,
48+ self ,
49+ configuration : SaicApiConfiguration ,
50+ listener : SaicApiListener | None = None ,
5351 ) -> None :
5452 self .__configuration = configuration
5553 self .__api_client = SaicApiClient (configuration , listener = listener )
@@ -61,7 +59,10 @@ async def login(self) -> LoginResp:
6159 "Accept" : "application/json" ,
6260 "Authorization" : "Basic c3dvcmQ6c3dvcmRfc2VjcmV0" ,
6361 }
64- firebase_device_id = "simulator*********************************************" + str (int (datetime .datetime .now ().timestamp ()))
62+ firebase_device_id = (
63+ "simulator*********************************************"
64+ + str (int (datetime .datetime .now ().timestamp ()))
65+ )
6566 form_body = {
6667 "grant_type" : "password" ,
6768 "username" : self .__configuration .username ,
@@ -73,14 +74,18 @@ async def login(self) -> LoginResp:
7374 }
7475
7576 if self .__configuration .username_is_email :
76- form_body .update ({
77- "loginType" : "2"
78- })
77+ form_body .update ({"loginType" : "2" })
78+ elif self .__configuration .phone_country_code is not None :
79+ form_body .update (
80+ {
81+ "loginType" : "1" ,
82+ "countryCode" : self .__configuration .phone_country_code ,
83+ }
84+ )
7985 else :
80- form_body .update ({
81- "loginType" : "1" ,
82- "countryCode" : self .__configuration .phone_country_code ,
83- })
86+ raise SaicApiException (
87+ "Username is not an email but no phone number country code has been provided."
88+ )
8489
8590 result = await self .execute_api_call (
8691 "POST" ,
@@ -91,7 +96,7 @@ async def login(self) -> LoginResp:
9196 )
9297 # Update the user token
9398 if not (access_token := result .access_token ) or not (
94- expiration := result .expires_in
99+ expiration := result .expires_in
95100 ):
96101 raise SaicApiException (
97102 "Failed to get an access token, please check your credentials"
@@ -104,15 +109,15 @@ async def login(self) -> LoginResp:
104109 return result
105110
106111 async def execute_api_call (
107- self ,
108- method : str ,
109- path : str ,
110- * ,
111- body : Any | None = None ,
112- form_body : Any | None = None ,
113- out_type : type [T ],
114- params : QueryParamTypes | None = None ,
115- headers : HeaderTypes | None = None ,
112+ self ,
113+ method : str ,
114+ path : str ,
115+ * ,
116+ body : Any | None = None ,
117+ form_body : Any | None = None ,
118+ out_type : type [T ],
119+ params : QueryParamTypes | None = None ,
120+ headers : HeaderTypes | None = None ,
116121 ) -> T :
117122 result = await self .__execute_api_call (
118123 method ,
@@ -130,15 +135,15 @@ async def execute_api_call(
130135 return result
131136
132137 async def execute_api_call_with_optional_result (
133- self ,
134- method : str ,
135- path : str ,
136- * ,
137- body : Any | None = None ,
138- form_body : Any | None = None ,
139- out_type : type [T ],
140- params : QueryParamTypes | None = None ,
141- headers : HeaderTypes | None = None ,
138+ self ,
139+ method : str ,
140+ path : str ,
141+ * ,
142+ body : Any | None = None ,
143+ form_body : Any | None = None ,
144+ out_type : type [T ],
145+ params : QueryParamTypes | None = None ,
146+ headers : HeaderTypes | None = None ,
142147 ) -> T | None :
143148 return await self .__execute_api_call (
144149 method ,
@@ -152,14 +157,14 @@ async def execute_api_call_with_optional_result(
152157 )
153158
154159 async def execute_api_call_no_result (
155- self ,
156- method : str ,
157- path : str ,
158- * ,
159- body : Any | None = None ,
160- form_body : Any | None = None ,
161- params : QueryParamTypes | None = None ,
162- headers : HeaderTypes | None = None ,
160+ self ,
161+ method : str ,
162+ path : str ,
163+ * ,
164+ body : Any | None = None ,
165+ form_body : Any | None = None ,
166+ params : QueryParamTypes | None = None ,
167+ headers : HeaderTypes | None = None ,
163168 ) -> None :
164169 await self .__execute_api_call (
165170 method ,
@@ -172,16 +177,16 @@ async def execute_api_call_no_result(
172177 )
173178
174179 async def __execute_api_call (
175- self ,
176- method : str ,
177- path : str ,
178- * ,
179- body : Any | None = None ,
180- form_body : Any | None = None ,
181- out_type : type [T ] | None = None ,
182- params : QueryParamTypes | None = None ,
183- headers : HeaderTypes | None = None ,
184- allow_null_body : bool = False ,
180+ self ,
181+ method : str ,
182+ path : str ,
183+ * ,
184+ body : Any | None = None ,
185+ form_body : Any | None = None ,
186+ out_type : type [T ] | None = None ,
187+ params : QueryParamTypes | None = None ,
188+ headers : HeaderTypes | None = None ,
189+ allow_null_body : bool = False ,
185190 ) -> T | None :
186191 try :
187192 url = f"{ self .__configuration .base_uri } { path .removeprefix ('/' )} "
@@ -203,15 +208,15 @@ async def __execute_api_call(
203208 raise SaicApiException (msg , return_code = 500 ) from e
204209
205210 async def execute_api_call_with_event_id (
206- self ,
207- method : str ,
208- path : str ,
209- * ,
210- body : Any | None = None ,
211- out_type : type [T ],
212- params : QueryParamTypes | None = None ,
213- headers : MutableMapping [str , str ] | None = None ,
214- delay : tenacity .wait .WaitBaseT | None = None ,
211+ self ,
212+ method : str ,
213+ path : str ,
214+ * ,
215+ body : Any | None = None ,
216+ out_type : type [T ],
217+ params : QueryParamTypes | None = None ,
218+ headers : MutableMapping [str , str ] | None = None ,
219+ delay : tenacity .wait .WaitBaseT | None = None ,
215220 ) -> T :
216221 result = await self .__execute_api_call_with_event_id (
217222 method ,
@@ -228,14 +233,14 @@ async def execute_api_call_with_event_id(
228233 return result
229234
230235 async def execute_api_call_with_event_id_no_result (
231- self ,
232- method : str ,
233- path : str ,
234- * ,
235- body : Any | None = None ,
236- params : QueryParamTypes | None = None ,
237- headers : MutableMapping [str , str ] | None = None ,
238- delay : tenacity .wait .WaitBaseT | None = None ,
236+ self ,
237+ method : str ,
238+ path : str ,
239+ * ,
240+ body : Any | None = None ,
241+ params : QueryParamTypes | None = None ,
242+ headers : MutableMapping [str , str ] | None = None ,
243+ delay : tenacity .wait .WaitBaseT | None = None ,
239244 ) -> None :
240245 await self .__execute_api_call_with_event_id (
241246 method ,
@@ -247,15 +252,15 @@ async def execute_api_call_with_event_id_no_result(
247252 )
248253
249254 async def __execute_api_call_with_event_id (
250- self ,
251- method : str ,
252- path : str ,
253- * ,
254- body : Any | None = None ,
255- out_type : type [T ] | None = None ,
256- params : QueryParamTypes | None = None ,
257- headers : MutableMapping [str , str ] | None = None ,
258- delay : tenacity .wait .WaitBaseT | None = None ,
255+ self ,
256+ method : str ,
257+ path : str ,
258+ * ,
259+ body : Any | None = None ,
260+ out_type : type [T ] | None = None ,
261+ params : QueryParamTypes | None = None ,
262+ headers : MutableMapping [str , str ] | None = None ,
263+ delay : tenacity .wait .WaitBaseT | None = None ,
259264 ) -> T | None :
260265 @tenacity .retry (
261266 stop = tenacity .stop_after_delay (30 ),
@@ -280,11 +285,11 @@ async def execute_api_call_with_event_id_inner(*, event_id: str) -> T | None:
280285
281286 # pylint: disable=too-many-branches
282287 async def __deserialize (
283- self ,
284- request : httpx .Request ,
285- response : httpx .Response ,
286- data_class : type [T ] | None ,
287- allow_null_body : bool ,
288+ self ,
289+ request : httpx .Request ,
290+ response : httpx .Response ,
291+ data_class : type [T ] | None ,
292+ allow_null_body : bool ,
288293 ) -> T | None :
289294 try :
290295 request_event_id = request .headers .get ("event-id" )
@@ -377,9 +382,9 @@ def logout(self) -> None:
377382 @property
378383 def is_logged_in (self ) -> bool :
379384 return (
380- self .__api_client .user_token is not None
381- and self .__token_expiration is not None
382- and self .__token_expiration > datetime .datetime .now ()
385+ self .__api_client .user_token is not None
386+ and self .__token_expiration is not None
387+ and self .__token_expiration > datetime .datetime .now ()
383388 )
384389
385390 @property
0 commit comments