Skip to content

Commit 3c520ad

Browse files
committed
Refactor API request methods to streamline response handling and reduce code duplication
1 parent 0f295c2 commit 3c520ad

File tree

1 file changed

+16
-35
lines changed

1 file changed

+16
-35
lines changed

pyoverkiz/client.py

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import backoff
1313
from aiohttp import (
1414
ClientConnectorError,
15+
ClientResponse,
1516
ClientSession,
1617
ServerDisconnectedError,
1718
)
@@ -478,23 +479,10 @@ async def _execute_action_group_direct(
478479
The executed action group does not have to be persisted on the server before use.
479480
Per-session rate-limit : 1 calls per 28min 48s period for all operations of the same category (exec)
480481
"""
481-
# Build a logical (snake_case) payload using model helpers and convert it
482-
# to the exact JSON schema expected by the API (camelCase + small fixes).
483482
payload = {"label": label, "actions": [a.to_payload() for a in actions]}
483+
url = f"exec/apply/{mode.value}" if mode else "exec/apply"
484484

485-
# Prepare final payload with camelCase keys and special abbreviation handling
486-
final_payload = prepare_payload(payload)
487-
488-
if mode == CommandMode.GEOLOCATED:
489-
url = "exec/apply/geolocated"
490-
elif mode == CommandMode.INTERNAL:
491-
url = "exec/apply/internal"
492-
elif mode == CommandMode.HIGH_PRIORITY:
493-
url = "exec/apply/highPriority"
494-
else:
495-
url = "exec/apply"
496-
497-
response: dict = await self._post(url, final_payload)
485+
response: dict = await self._post(url, prepare_payload(payload))
498486

499487
return cast(str, response["execId"])
500488

@@ -710,55 +698,48 @@ async def get_reference_ui_widgets(self) -> list[str]:
710698
async def _get(self, path: str) -> Any:
711699
"""Make a GET request to the OverKiz API."""
712700
await self._refresh_token_if_expired()
713-
headers = dict(self._auth.auth_headers(path))
714701

715702
async with self.session.get(
716703
f"{self.server_config.endpoint}{path}",
717-
headers=headers,
704+
headers=self._auth.auth_headers(path),
718705
ssl=self._ssl,
719706
) as response:
720-
await check_response(response)
721-
722-
# 204 has no body.
723-
if response.status == 204:
724-
return None
725-
726-
return await response.json()
707+
return await self._parse_response(response)
727708

728709
async def _post(
729710
self, path: str, payload: JSON | None = None, data: JSON | None = None
730711
) -> Any:
731712
"""Make a POST request to the OverKiz API."""
732713
await self._refresh_token_if_expired()
733-
headers = dict(self._auth.auth_headers(path))
734714

735715
async with self.session.post(
736716
f"{self.server_config.endpoint}{path}",
737717
data=data,
738718
json=payload,
739-
headers=headers,
719+
headers=self._auth.auth_headers(path),
740720
ssl=self._ssl,
741721
) as response:
742-
await check_response(response)
743-
744-
# 204 has no body.
745-
if response.status == 204:
746-
return None
747-
748-
return await response.json()
722+
return await self._parse_response(response)
749723

750724
async def _delete(self, path: str) -> None:
751725
"""Make a DELETE request to the OverKiz API."""
752726
await self._refresh_token_if_expired()
753-
headers = dict(self._auth.auth_headers(path))
754727

755728
async with self.session.delete(
756729
f"{self.server_config.endpoint}{path}",
757-
headers=headers,
730+
headers=self._auth.auth_headers(path),
758731
ssl=self._ssl,
759732
) as response:
760733
await check_response(response)
761734

735+
@staticmethod
736+
async def _parse_response(response: ClientResponse) -> Any:
737+
"""Check response status and parse JSON body (returns None for 204)."""
738+
await check_response(response)
739+
if response.status == 204:
740+
return None
741+
return await response.json()
742+
762743
async def _refresh_token_if_expired(self) -> None:
763744
"""Check if token is expired and request a new one."""
764745
refreshed = await self._auth.refresh_if_needed()

0 commit comments

Comments
 (0)