Skip to content

Commit a2eab9b

Browse files
committed
Add execute_api_call_with_optional_result and disallow the usage of allow_null_body in execute_api_call
1 parent b2a0995 commit a2eab9b

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/saic_ismart_client_ng/api/base.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ async def execute_api_call(
106106
out_type: type[T],
107107
params: QueryParamTypes | None = None,
108108
headers: HeaderTypes | None = None,
109-
allow_null_body: bool = False,
110109
) -> T:
111110
result = await self.__execute_api_call(
112111
method,
@@ -116,13 +115,35 @@ async def execute_api_call(
116115
out_type=out_type,
117116
params=params,
118117
headers=headers,
119-
allow_null_body=allow_null_body,
118+
allow_null_body=False,
120119
)
121-
if result is None and not allow_null_body:
120+
if result is None:
122121
msg = f"Failed to execute api call {method} {path}, was expecting a result of type {out_type} got None instead"
123122
raise SaicApiException(msg)
124123
return result
125124

125+
async def execute_api_call_with_optional_result(
126+
self,
127+
method: str,
128+
path: str,
129+
*,
130+
body: Any | None = None,
131+
form_body: Any | None = None,
132+
out_type: type[T],
133+
params: QueryParamTypes | None = None,
134+
headers: HeaderTypes | None = None,
135+
) -> T | None:
136+
return await self.__execute_api_call(
137+
method,
138+
path,
139+
body=body,
140+
form_body=form_body,
141+
out_type=out_type,
142+
params=params,
143+
headers=headers,
144+
allow_null_body=True,
145+
)
146+
126147
async def execute_api_call_no_result(
127148
self,
128149
method: str,
@@ -132,7 +153,6 @@ async def execute_api_call_no_result(
132153
form_body: Any | None = None,
133154
params: QueryParamTypes | None = None,
134155
headers: HeaderTypes | None = None,
135-
allow_null_body: bool = False,
136156
) -> None:
137157
await self.__execute_api_call(
138158
method,
@@ -141,7 +161,7 @@ async def execute_api_call_no_result(
141161
form_body=form_body,
142162
params=params,
143163
headers=headers,
144-
allow_null_body=allow_null_body,
164+
allow_null_body=True,
145165
)
146166

147167
async def __execute_api_call(

src/saic_ismart_client_ng/api/message/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async def get_news_list(
3939
async def get_message_list(
4040
self, *, page_num: int, page_size: int, message_group: str
4141
) -> MessageResp | None:
42-
return await self.execute_api_call(
42+
return await self.execute_api_call_with_optional_result(
4343
"GET",
4444
"/message/list",
4545
params={
@@ -48,7 +48,6 @@ async def get_message_list(
4848
"messageGroup": message_group,
4949
},
5050
out_type=MessageResp,
51-
allow_null_body=True,
5251
)
5352

5453
async def delete_all_alarms(self) -> None:
@@ -83,6 +82,6 @@ async def update_message_status(self, data: UpateMessageRequest) -> None:
8382
)
8483

8584
async def get_unread_messages_count(self) -> MessageResp | None:
86-
return await self.execute_api_call(
87-
"GET", "/message/unreadCount", out_type=MessageResp, allow_null_body=True
85+
return await self.execute_api_call_with_optional_result(
86+
"GET", "/message/unreadCount", out_type=MessageResp
8887
)

0 commit comments

Comments
 (0)