Skip to content

Commit

Permalink
Some more trial
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane610 committed Sep 22, 2023
1 parent 14b8b5b commit b242648
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 108 deletions.
3 changes: 0 additions & 3 deletions axis/vapix/interfaces/api_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
19 changes: 16 additions & 3 deletions axis/vapix/models/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand All @@ -70,7 +83,7 @@ class ApiRequest(ABC, Generic[ApiDataT]):
error_codes: dict[int, str] = field(init=False)

@abstractmethod
def process_raw(self, raw: str) -> ApiDataT:
def process_raw(self, bytes_data: bytes) -> ApiDataT:
"""Process raw data."""


Expand Down
8 changes: 4 additions & 4 deletions axis/vapix/models/api_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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"],
Expand Down
32 changes: 27 additions & 5 deletions axis/vapix/models/basic_device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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."""
Expand All @@ -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"],
Expand Down
62 changes: 31 additions & 31 deletions axis/vapix/models/light_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ def __post_init__(self) -> None:
"method": "getLightInformation",
}

def process_raw(self, raw: str) -> dict[str, LightInformation]:
def process_raw(self, bytes_data: bytes) -> dict[str, LightInformation]:
"""Prepare light information dictionary."""
data: GetLightInformationResponseT = orjson.loads(raw)
data: GetLightInformationResponseT = orjson.loads(bytes_data)
return LightInformation.from_list(data["data"]["items"])


Expand Down Expand Up @@ -316,9 +316,9 @@ def __post_init__(self) -> None:
"method": "getServiceCapabilities",
}

def process_raw(self, raw: str) -> ServiceCapabilities:
def process_raw(self, bytes_data: bytes) -> ServiceCapabilities:
"""Prepare light information dictionary."""
data: GetServiceCapabilitiesResponseT = orjson.loads(raw)
data: GetServiceCapabilitiesResponseT = orjson.loads(bytes_data)
return ServiceCapabilities.from_dict(data["data"])


Expand All @@ -345,7 +345,7 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id},
}

def process_raw(self, raw: str) -> None:
def process_raw(self, bytes_data: bytes) -> None:
"""No return data to process."""


Expand Down Expand Up @@ -417,9 +417,9 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id},
}

def process_raw(self, raw: str) -> bool:
def process_raw(self, bytes_data: bytes) -> bool:
"""If light is on or off."""
data: GetLightStatusResponseT = orjson.loads(raw)
data: GetLightStatusResponseT = orjson.loads(bytes_data)
return data["data"]["status"]


Expand Down Expand Up @@ -448,7 +448,7 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id, "enabled": self.enabled},
}

def process_raw(self, raw: str) -> None:
def process_raw(self, bytes_data: bytes) -> None:
"""No return data to process."""


Expand All @@ -475,9 +475,9 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id},
}

def process_raw(self, raw: str) -> Range:
def process_raw(self, bytes_data: bytes) -> Range:
"""If light is on or off."""
data: GetValidRangesResponseT = orjson.loads(raw)
data: GetValidRangesResponseT = orjson.loads(bytes_data)
return Range.from_dict(data["data"]["ranges"][0])


Expand Down Expand Up @@ -506,7 +506,7 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id, "intensity": self.intensity},
}

def process_raw(self, raw: str) -> None:
def process_raw(self, bytes_data: bytes) -> None:
"""No return data to process."""


Expand All @@ -533,9 +533,9 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id},
}

def process_raw(self, raw: str) -> int:
def process_raw(self, bytes_data: bytes) -> int:
"""If light is on or off."""
data: GetIntensityResponseT = orjson.loads(raw)
data: GetIntensityResponseT = orjson.loads(bytes_data)
return data["data"]["intensity"]


Expand Down Expand Up @@ -570,7 +570,7 @@ def __post_init__(self) -> None:
},
}

def process_raw(self, raw: str) -> None:
def process_raw(self, bytes_data: bytes) -> None:
"""No return data to process."""


Expand Down Expand Up @@ -599,9 +599,9 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id, "LEDID": self.led_id},
}

def process_raw(self, raw: str) -> int:
def process_raw(self, bytes_data: bytes) -> int:
"""Process light intensity."""
data: GetIntensityResponseT = orjson.loads(raw)
data: GetIntensityResponseT = orjson.loads(bytes_data)
return data["data"]["intensity"]


Expand All @@ -628,9 +628,9 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id},
}

def process_raw(self, raw: str) -> int:
def process_raw(self, bytes_data: bytes) -> int:
"""If light is on or off."""
data: GetIntensityResponseT = orjson.loads(raw)
data: GetIntensityResponseT = orjson.loads(bytes_data)
return data["data"]["intensity"]


Expand Down Expand Up @@ -659,7 +659,7 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id, "enabled": self.enabled},
}

def process_raw(self, raw: str) -> None:
def process_raw(self, bytes_data: bytes) -> None:
"""No return data to process."""


Expand All @@ -686,9 +686,9 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id},
}

def process_raw(self, raw: str) -> list[Range]:
def process_raw(self, bytes_data: bytes) -> list[Range]:
"""If light is on or off."""
data: GetValidRangesResponseT = orjson.loads(raw)
data: GetValidRangesResponseT = orjson.loads(bytes_data)
return Range.from_list(data["data"]["ranges"])


Expand Down Expand Up @@ -720,7 +720,7 @@ def __post_init__(self) -> None:
},
}

def process_raw(self, raw: str) -> None:
def process_raw(self, bytes_data: bytes) -> None:
"""No return data to process."""


Expand All @@ -747,9 +747,9 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id},
}

def process_raw(self, raw: str) -> int:
def process_raw(self, bytes_data: bytes) -> int:
"""Angle of illumination."""
data: GetAngleOfIlluminationResponseT = orjson.loads(raw)
data: GetAngleOfIlluminationResponseT = orjson.loads(bytes_data)
return data["data"]["angleOfIllumination"]


Expand All @@ -776,9 +776,9 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id},
}

def process_raw(self, raw: str) -> int:
def process_raw(self, bytes_data: bytes) -> int:
"""Angle of illumination."""
data: GetAngleOfIlluminationResponseT = orjson.loads(raw)
data: GetAngleOfIlluminationResponseT = orjson.loads(bytes_data)
return data["data"]["angleOfIllumination"]


Expand Down Expand Up @@ -807,7 +807,7 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id, "enabled": self.enabled},
}

def process_raw(self, raw: str) -> None:
def process_raw(self, bytes_data: bytes) -> None:
"""No return data to process."""


Expand All @@ -834,9 +834,9 @@ def __post_init__(self) -> None:
"params": {"lightID": self.light_id},
}

def process_raw(self, raw: str) -> bool:
def process_raw(self, bytes_data: bytes) -> bool:
"""If light is on or off."""
data: GetLightSynchronizationDayNightModeResponseT = orjson.loads(raw)
data: GetLightSynchronizationDayNightModeResponseT = orjson.loads(bytes_data)
return data["data"]["synchronize"]


Expand All @@ -858,7 +858,7 @@ def __post_init__(self) -> None:
"method": "getSupportedVersions",
}

def process_raw(self, raw: str) -> list[str]:
def process_raw(self, bytes_data: bytes) -> list[str]:
"""Process supported versions."""
data: GetSupportedVersionsResponseT = orjson.loads(raw)
data: GetSupportedVersionsResponseT = orjson.loads(bytes_data)
return data.get("data", {}).get("apiVersions", [])
14 changes: 7 additions & 7 deletions axis/vapix/models/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def __post_init__(self) -> None:
"params": self.client_config.to_dict(),
}

def process_raw(self, raw: str) -> None:
def process_raw(self, bytes_data: bytes) -> None:
"""Prepare view area dictionary."""


Expand All @@ -421,7 +421,7 @@ def __post_init__(self) -> None:
"method": "activateClient",
}

def process_raw(self, raw: str) -> None:
def process_raw(self, bytes_data: bytes) -> None:
"""Prepare view area dictionary."""


Expand Down Expand Up @@ -458,9 +458,9 @@ def __post_init__(self) -> None:
"method": "getClientStatus",
}

def process_raw(self, raw: str) -> ClientConfigStatus:
def process_raw(self, bytes_data: bytes) -> ClientConfigStatus:
"""Prepare view area dictionary."""
data: GetClientStatusResponseT = orjson.loads(raw)
data: GetClientStatusResponseT = orjson.loads(bytes_data)
return ClientConfigStatus.from_dict(data["data"])


Expand All @@ -484,9 +484,9 @@ def __post_init__(self) -> None:
"method": "getEventPublicationConfig",
}

def process_raw(self, raw: str) -> EventPublicationConfig:
def process_raw(self, bytes_data: bytes) -> EventPublicationConfig:
"""Prepare view area dictionary."""
data: GetEventPublicationConfigResponseT = orjson.loads(raw)
data: GetEventPublicationConfigResponseT = orjson.loads(bytes_data)
return EventPublicationConfig.from_dict(data["data"]["eventPublicationConfig"])


Expand All @@ -513,5 +513,5 @@ def __post_init__(self) -> None:
"params": self.config.to_dict(),
}

def process_raw(self, raw: str) -> None:
def process_raw(self, bytes_data: bytes) -> None:
"""Prepare view area dictionary."""
Loading

0 comments on commit b242648

Please sign in to comment.