Skip to content

Commit

Permalink
Rework API request (#238)
Browse files Browse the repository at this point in the history
* Test

* Rework basic_device_info

* Make basic device info pass mypy and tests

* Adapt api discovery

* Some more trial

* Combine ApiRequest2/3
Change data to content

* Skip being too smart for now, api handler classes will need to combine request response parsing

* Fix pir sensor

* Fix stream profiles

* Fix view areas

* Fix mqtt

* Fix light control

* Remove 2 from ApiHandler2 and ApiRequest2
  • Loading branch information
Kane610 authored Oct 6, 2023
1 parent bfc2669 commit e5c78b2
Show file tree
Hide file tree
Showing 23 changed files with 1,448 additions and 702 deletions.
18 changes: 14 additions & 4 deletions axis/vapix/interfaces/api_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
"""

from ..models.api_discovery import (
API_VERSION,
Api,
ApiId,
GetAllApisResponse,
GetSupportedVersionsRequest,
GetSupportedVersionsResponse,
ListApisRequest,
ListApisT,
)
Expand All @@ -18,13 +21,20 @@ class ApiDiscoveryHandler(ApiHandler[Api]):
"""API Discovery for Axis devices."""

api_id = ApiId.API_DISCOVERY
api_request = ListApisRequest()
default_api_version = API_VERSION

async def _api_request(self) -> dict[str, Api]:
"""Get default data of API discovery."""
return await self.get_api_list()

async def get_api_list(self) -> ListApisT:
"""List all APIs registered on API Discovery service."""
discovery_item = self[self.api_id.value]
return await self.vapix.request2(ListApisRequest(discovery_item.version))
bytes_data = await self.vapix.new_request(ListApisRequest())
response = GetAllApisResponse.decode(bytes_data)
return {api.id: api for api in response.data}

async def get_supported_versions(self) -> list[str]:
"""List supported API versions."""
return await self.vapix.request2(GetSupportedVersionsRequest())
bytes_data = await self.vapix.new_request(GetSupportedVersionsRequest())
response = GetSupportedVersionsResponse.decode(bytes_data)
return response.data
18 changes: 11 additions & 7 deletions axis/vapix/interfaces/api_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""API handler class and base class for an API endpoint."""

from abc import ABC
from abc import ABC, abstractmethod
from typing import (
TYPE_CHECKING,
Any,
Expand All @@ -15,14 +15,13 @@
from ..models.api_discovery import ApiId
from ..vapix import Vapix

from ..models.api import ApiItemT, ApiRequest
from ..models.api import ApiItemT


class ApiHandler(ABC, Generic[ApiItemT]):
"""Base class for a map of API Items."""

api_id: "ApiId"
api_request: ApiRequest[dict[str, ApiItemT]] | None
default_api_version: str | None = None

def __init__(self, vapix: "Vapix") -> None:
Expand All @@ -35,18 +34,23 @@ def supported(self) -> bool:
"""Is API supported by the device."""
return self.api_id.value in self.vapix.api_discovery

@property
def api_version(self) -> str | None:
"""Latest API version supported."""
if (discovery_item := self.vapix.api_discovery[self.api_id.value]) is not None:
if (
discovery_item := self.vapix.api_discovery.get(self.api_id.value)
) is not None:
return discovery_item.version
return self.default_api_version

@abstractmethod
async def _api_request(self) -> dict[str, ApiItemT]:
"""Get API data method defined by subsclass."""

async def update(self) -> None:
"""Refresh data."""
if self.api_request is None:
return
self._items = await self._api_request()
self.initialized = True
self._items = await self.vapix.request2(self.api_request)

def items(self) -> ItemsView[str, ApiItemT]:
"""Return items."""
Expand Down
17 changes: 14 additions & 3 deletions axis/vapix/interfaces/basic_device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

from ..models.api_discovery import ApiId
from ..models.basic_device_info import (
API_VERSION,
DeviceInformation,
GetAllPropertiesRequest,
GetAllPropertiesResponse,
GetAllPropertiesT,
GetSupportedVersionsRequest,
GetSupportedVersionsResponse,
)
from .api_handler import ApiHandler

Expand All @@ -19,18 +22,26 @@ class BasicDeviceInfoHandler(ApiHandler[DeviceInformation]):
"""Basic device information for Axis devices."""

api_id = ApiId.BASIC_DEVICE_INFO
api_request = GetAllPropertiesRequest()
default_api_version = API_VERSION

async def _api_request(self) -> dict[str, DeviceInformation]:
"""Get default data of basic device information."""
return await self.get_all_properties()

async def get_all_properties(self) -> GetAllPropertiesT:
"""List all properties of basic device info."""
discovery_item = self.vapix.api_discovery[self.api_id.value]
return await self.vapix.request2(
bytes_data = await self.vapix.new_request(
GetAllPropertiesRequest(discovery_item.version)
)
response = GetAllPropertiesResponse.decode(bytes_data)
return {"0": response.data}

async def get_supported_versions(self) -> list[str]:
"""List supported API versions."""
return await self.vapix.request2(GetSupportedVersionsRequest())
bytes_data = await self.vapix.new_request(GetSupportedVersionsRequest())
response = GetSupportedVersionsResponse.decode(bytes_data)
return response.data

@property
def architecture(self) -> str:
Expand Down
Loading

0 comments on commit e5c78b2

Please sign in to comment.