|
12 | 12 | import backoff |
13 | 13 | from aiohttp import ( |
14 | 14 | ClientConnectorError, |
| 15 | + ClientResponse, |
15 | 16 | ClientSession, |
16 | 17 | ServerDisconnectedError, |
17 | 18 | ) |
@@ -478,23 +479,10 @@ async def _execute_action_group_direct( |
478 | 479 | The executed action group does not have to be persisted on the server before use. |
479 | 480 | Per-session rate-limit : 1 calls per 28min 48s period for all operations of the same category (exec) |
480 | 481 | """ |
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). |
483 | 482 | payload = {"label": label, "actions": [a.to_payload() for a in actions]} |
| 483 | + url = f"exec/apply/{mode.value}" if mode else "exec/apply" |
484 | 484 |
|
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)) |
498 | 486 |
|
499 | 487 | return cast(str, response["execId"]) |
500 | 488 |
|
@@ -710,55 +698,48 @@ async def get_reference_ui_widgets(self) -> list[str]: |
710 | 698 | async def _get(self, path: str) -> Any: |
711 | 699 | """Make a GET request to the OverKiz API.""" |
712 | 700 | await self._refresh_token_if_expired() |
713 | | - headers = dict(self._auth.auth_headers(path)) |
714 | 701 |
|
715 | 702 | async with self.session.get( |
716 | 703 | f"{self.server_config.endpoint}{path}", |
717 | | - headers=headers, |
| 704 | + headers=self._auth.auth_headers(path), |
718 | 705 | ssl=self._ssl, |
719 | 706 | ) 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) |
727 | 708 |
|
728 | 709 | async def _post( |
729 | 710 | self, path: str, payload: JSON | None = None, data: JSON | None = None |
730 | 711 | ) -> Any: |
731 | 712 | """Make a POST request to the OverKiz API.""" |
732 | 713 | await self._refresh_token_if_expired() |
733 | | - headers = dict(self._auth.auth_headers(path)) |
734 | 714 |
|
735 | 715 | async with self.session.post( |
736 | 716 | f"{self.server_config.endpoint}{path}", |
737 | 717 | data=data, |
738 | 718 | json=payload, |
739 | | - headers=headers, |
| 719 | + headers=self._auth.auth_headers(path), |
740 | 720 | ssl=self._ssl, |
741 | 721 | ) 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) |
749 | 723 |
|
750 | 724 | async def _delete(self, path: str) -> None: |
751 | 725 | """Make a DELETE request to the OverKiz API.""" |
752 | 726 | await self._refresh_token_if_expired() |
753 | | - headers = dict(self._auth.auth_headers(path)) |
754 | 727 |
|
755 | 728 | async with self.session.delete( |
756 | 729 | f"{self.server_config.endpoint}{path}", |
757 | | - headers=headers, |
| 730 | + headers=self._auth.auth_headers(path), |
758 | 731 | ssl=self._ssl, |
759 | 732 | ) as response: |
760 | 733 | await check_response(response) |
761 | 734 |
|
| 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 | + |
762 | 743 | async def _refresh_token_if_expired(self) -> None: |
763 | 744 | """Check if token is expired and request a new one.""" |
764 | 745 | refreshed = await self._auth.refresh_if_needed() |
|
0 commit comments