Skip to content

Commit

Permalink
Merge pull request #188 from loopj/cleanup-requests
Browse files Browse the repository at this point in the history
Use PascalCase for sending requests by default, clean up request logic
  • Loading branch information
loopj authored Jan 26, 2025
2 parents 8197917 + 5463703 commit 2fbac50
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 94 deletions.
24 changes: 18 additions & 6 deletions src/aiovantage/config_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import logging
from ssl import SSLContext
from types import TracebackType
from typing import Protocol, TypeVar
from xml.etree import ElementTree as ET

from typing_extensions import Self
Expand All @@ -33,10 +34,20 @@
from aiovantage.connection import BaseConnection
from aiovantage.errors import ClientResponseError, LoginFailedError, LoginRequiredError

from .interfaces import Call, Method, Return
from .interfaces.introspection import GetSysInfo
from .interfaces.login import Login

T = TypeVar("T")
U = TypeVar("U")


class Method(Protocol[T, U]):
"""Duck typing for config client methods."""

interface: str
call: T | None
result: U | None


class ConfigConnection(BaseConnection):
"""Connection to a Vantage ACI server."""
Expand Down Expand Up @@ -82,6 +93,7 @@ def __init__(
# Configure the request serializer
self._serializer = XmlSerializer(
config=SerializerConfig(xml_declaration=False),
context=xml_context,
)

# Configure the response parser
Expand Down Expand Up @@ -146,10 +158,10 @@ async def raw_request(

async def request(
self,
method_cls: type[Method[Call, Return]],
params: Call | None = None,
method_cls: type[Method[T, U]],
params: T | None = None,
connection: ConfigConnection | None = None,
) -> Return | None:
) -> U | None:
"""Marshall a request, send it to the ACI service, and return a parsed object.
Args:
Expand Down Expand Up @@ -194,10 +206,10 @@ async def request(

# Parse the method element with xsdata
method = self._parser.parse(method_el, method_cls)
if method.return_value is None:
if method.result is None:
return None

return method.return_value
return method.result

async def get_connection(self) -> ConfigConnection:
"""Get a connection to the ACI service."""
Expand Down
14 changes: 0 additions & 14 deletions src/aiovantage/config_client/interfaces/__init__.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
__all__ = [
"CloseFilter",
"GetFilterResults",
"OpenFilter",
"GetObject",
"OpenFilter",
]
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ class CloseFilter:

interface = "IConfiguration"

call: int | None = field(default=None)
return_value: bool | None = field(
call: int | None = field(
default=None,
metadata={
"name": "call",
},
)

result: bool | None = field(
default=None,
metadata={
"name": "return",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,22 @@ class GetFilterResults:
class Params:
"""Method parameters."""

h_filter: int = field(
metadata={
"name": "hFilter",
}
)

count: int = field(
default=50,
metadata={
"name": "Count",
},
)

whole_object: bool = field(
default=True,
metadata={
"name": "WholeObject",
},
)

call: Params | None = field(default=None)
return_value: list[ObjectChoice] | None = field(
h_filter: int = field(metadata={"name": "hFilter"})
count: int = 50
whole_object: bool = True

call: Params | None = field(
default=None,
metadata={
"name": "call",
},
)

result: list[ObjectChoice] | None = field(
default=None,
metadata={
"name": "Object",
"wrapper": "return",
"name": "Object",
"type": "Element",
},
)
23 changes: 11 additions & 12 deletions src/aiovantage/config_client/interfaces/configuration/get_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@ class GetObject:

interface = "IConfiguration"

@dataclass
class Params:
"""Method parameters."""

vids: list[int] = field(
metadata={
"name": "VID",
}
)
call: list[int] | None = field(
default=None,
metadata={
"wrapper": "call",
"name": "VID",
"type": "Element",
},
)

call: Params | None = field(default=None)
return_value: list[ObjectChoice] | None = field(
result: list[ObjectChoice] | None = field(
default=None,
metadata={
"name": "Object",
"wrapper": "return",
"name": "Object",
"type": "Element",
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class Params:
object_types: list[str] | None = field(
default=None,
metadata={
"name": "ObjectType",
"wrapper": "Objects",
"name": "ObjectType",
"type": "Element",
},
)

Expand All @@ -28,8 +29,14 @@ class Params:
},
)

call: Params | None = field(default=None)
return_value: int | None = field(
call: Params | None = field(
default=None,
metadata={
"name": "call",
},
)

result: int | None = field(
default=None,
metadata={
"name": "return",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ class GetInterfaces:
class Interface:
"""Object interface definition."""

name: str = field(metadata={"name": "Name"})
version: str = field(metadata={"name": "Version"})
id: int = field(metadata={"name": "IID"})
name: str
version: str
iid: int = field(metadata={"name": "IID"})

call = None
return_value: list[Interface] | None = field(
default=None,

result: list[Interface] | None = field(
default_factory=list,
metadata={
"name": "Interface",
"wrapper": "return",
"name": "Interface",
"type": "Element",
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ class GetSysInfo:
class SysInfo:
"""SysInfo class."""

master_number: int = field(metadata={"name": "MasterNumber"})
serial_number: int = field(metadata={"name": "SerialNumber"})
master_number: int
serial_number: int

@dataclass
class Return:
"""Method return value."""

sys_info: "GetSysInfo.SysInfo" = field(metadata={"name": "SysInfo"})
sys_info: "GetSysInfo.SysInfo"

call = None
return_value: Return | None = field(

result: Return | None = field(
default=None,
metadata={
"name": "return",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ class GetTypes:
class Type:
"""Object type definition."""

name: str = field(metadata={"name": "Name"})
version: str = field(metadata={"name": "Version"})
name: str
version: str

call = None
return_value: list[Type] | None = field(

result: list[Type] | None = field(
default=None,
metadata={
"name": "Type",
"wrapper": "return",
"name": "Type",
"type": "Element",
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ class GetVersion:
class Return:
"""Method return value."""

kernel: str | None = field(default=None)
rootfs: str | None = field(default=None)
app: str | None = field(default=None)
kernel: str | None = field(default=None, metadata={"name": "kernel"})
rootfs: str | None = field(default=None, metadata={"name": "rootfs"})
app: str | None = field(default=None, metadata={"name": "app"})

call = None
return_value: Return | None = field(

result: Return | None = field(
default=None,
metadata={
"name": "return",
Expand Down
25 changes: 11 additions & 14 deletions src/aiovantage/config_client/interfaces/login/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@ class Login:
class Params:
"""Method parameters."""

user: str = field(
metadata={
"name": "User",
}
)

password: str = field(
metadata={
"name": "Password",
}
)

call: Params | None = field(default=None)
return_value: bool | None = field(
user: str
password: str

call: Params | None = field(
default=None,
metadata={
"name": "call",
},
)

result: bool | None = field(
default=None,
metadata={
"name": "return",
Expand Down
2 changes: 1 addition & 1 deletion src/aiovantage/config_client/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async def get_objects_by_id(
The objects of the specified ids
"""
# Open the filter
response = await client.request(GetObject, GetObject.Params(vids=list(vids)))
response = await client.request(GetObject, list(vids))
if not response:
return

Expand Down

0 comments on commit 2fbac50

Please sign in to comment.