From e6c416c1d80e72d84af60ae6f09a14d76eafebe3 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Fri, 22 Sep 2023 22:54:00 +0200 Subject: [PATCH] Some more trial --- axis/vapix/interfaces/api_handler.py | 3 --- axis/vapix/models/api.py | 17 ++++++++++++-- axis/vapix/models/api_discovery.py | 8 +++---- axis/vapix/models/basic_device_info.py | 32 ++++++++++++++++++++++---- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/axis/vapix/interfaces/api_handler.py b/axis/vapix/interfaces/api_handler.py index a4d5c808..3628b61d 100644 --- a/axis/vapix/interfaces/api_handler.py +++ b/axis/vapix/interfaces/api_handler.py @@ -105,7 +105,6 @@ def api_version(self) -> str | None: if ( discovery_item := self.vapix.api_discovery.get(self.api_id.value) ) is not None: - # if (discovery_item := self.vapix.api_discovery[self.api_id.value]) is not None: return discovery_item.version return self.default_api_version @@ -115,8 +114,6 @@ async def _api_request(self) -> dict[str, ApiItemT]: async def update(self) -> None: """Refresh data.""" - # if self.api_request is None: - # return self._items = await self._api_request() self.initialized = True diff --git a/axis/vapix/models/api.py b/axis/vapix/models/api.py index 9575f26f..36559c5c 100644 --- a/axis/vapix/models/api.py +++ b/axis/vapix/models/api.py @@ -26,8 +26,8 @@ class ApiResponseSupportDecode(ABC): @classmethod @abstractmethod - def decode(cls, raw: str) -> Self: - """Decode string to class object.""" + def decode(cls, bytes_data: bytes) -> Self: + """Decode data to class object.""" @dataclass @@ -58,6 +58,19 @@ def data(self) -> dict[str, Any]: """Request data.""" +@dataclass +class ApiRequest3(ABC): + """Create API request body.""" + + method: str = field(init=False) + path: str = field(init=False) + + @property + @abstractmethod + def data(self) -> dict[str, Any]: + """Request data.""" + + @dataclass class ApiRequest(ABC, Generic[ApiDataT]): """Create API request body.""" diff --git a/axis/vapix/models/api_discovery.py b/axis/vapix/models/api_discovery.py index 234e76e9..6dab6fb9 100644 --- a/axis/vapix/models/api_discovery.py +++ b/axis/vapix/models/api_discovery.py @@ -204,9 +204,9 @@ class GetAllApisResponse(ApiResponse[list[Api]]): # error: ErrorDataT | None = None @classmethod - def decode(cls, raw: str) -> Self: + def decode(cls, bytes_data: bytes) -> Self: """Prepare API description dictionary.""" - data: ListApisResponseT = orjson.loads(raw) + data: ListApisResponseT = orjson.loads(bytes_data) return cls( api_version=data["apiVersion"], context=data["context"], @@ -249,9 +249,9 @@ class GetSupportedVersionsResponse(ApiResponse[list[str]]): # error: ErrorDataT | None = None @classmethod - def decode(cls, raw: str) -> Self: + def decode(cls, bytes_data: bytes) -> Self: """Prepare API description dictionary.""" - data: GetSupportedVersionsResponseT = orjson.loads(raw) + data: GetSupportedVersionsResponseT = orjson.loads(bytes_data) return cls( api_version=data["apiVersion"], context=data["context"], diff --git a/axis/vapix/models/basic_device_info.py b/axis/vapix/models/basic_device_info.py index b8af7f39..0b95f452 100644 --- a/axis/vapix/models/basic_device_info.py +++ b/axis/vapix/models/basic_device_info.py @@ -6,7 +6,7 @@ import orjson from typing_extensions import NotRequired, Self, TypedDict -from .api import CONTEXT, ApiItem, ApiRequest2, ApiResponse +from .api import CONTEXT, ApiItem, ApiRequest2, ApiRequest3, ApiResponse API_VERSION = "1.1" @@ -136,9 +136,9 @@ class GetAllPropertiesResponse(ApiResponse[DeviceInformation]): # error: ErrorDataT | None = None @classmethod - def decode(cls, raw: str) -> Self: + def decode(cls, bytes_data: bytes) -> Self: """Prepare API description dictionary.""" - data: GetAllPropertiesResponseT = orjson.loads(raw) + data: GetAllPropertiesResponseT = orjson.loads(bytes_data) return cls( api_version=data["apiVersion"], context=data["context"], @@ -170,6 +170,28 @@ def data(self) -> dict[str, Any]: } +@dataclass +class GetAllPropertiesRequest2(ApiRequest3): + """Request object for basic device info.""" + + method = "post" + path = "/axis-cgi/basicdeviceinfo.cgi" + content_type = "application/json" + error_codes = error_codes + + api_version: str = API_VERSION + context: str = CONTEXT + + @property + def data(self) -> dict[str, Any]: + """Initialize request data.""" + return { + "apiVersion": self.api_version, + "context": self.context, + "method": "getAllProperties", + } + + @dataclass class GetSupportedVersionsResponse(ApiResponse[list[str]]): """Response object for supported versions.""" @@ -181,9 +203,9 @@ class GetSupportedVersionsResponse(ApiResponse[list[str]]): # error: ErrorDataT | None = None @classmethod - def decode(cls, raw: str) -> Self: + def decode(cls, bytes_data: bytes) -> Self: """Prepare API description dictionary.""" - data: GetSupportedVersionsResponseT = orjson.loads(raw) + data: GetSupportedVersionsResponseT = orjson.loads(bytes_data) return cls( api_version=data["apiVersion"], context=data["context"],