Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/APIClientRegen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Regenerating the API Client

1. Download the latest API specification from https://api.infuse-iot.com/docs
2. Delete the previous API client: `rm -r ./src/infuse-iot/api_client`
3. Generate API client into the root directory: `openapi-python-client generate --path ./infuse-api.yaml`
4. Move API client to desired directory: `mv infuse-api-client/infuse_api_client/ ./src/infuse_iot/api_client/`
5. Move README: `mv infuse-api-client/README.md ./src/infuse_iot/api_client/`
6. Remove extraneous files: `rm -r infuse-api-client`
7. Manually fixup `README.md` for naming

Some of these steps can possibly be automated with the `openapi-python-client` `--config` parameter in the future.
9 changes: 7 additions & 2 deletions scripts/apn_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class APNSetter:
def __init__(self, args: argparse.Namespace):
self.app_id = args.app
self.apn = args.apn
self.family = args.family
self.client = LocalClient(default_multicast_address(), 1.0)
self.decoder = TDF()
self.state = "Scanning"
Expand Down Expand Up @@ -65,8 +66,7 @@ def announce_observed(self, live: Live, infuse_id: int, pkt: readings.announce):
with self.client.connection(infuse_id, GatewayRequestConnectionRequest.DataType.COMMAND) as mtu:
rpc_client = RpcClient(self.client, mtu, infuse_id)

ipv4 = 0
family_bytes = ipv4.to_bytes(1, "little")
family_bytes = self.family.to_bytes(1, "little")
apn_bytes = self.apn.encode("utf-8") + b"\x00"
val_bytes = family_bytes + len(apn_bytes).to_bytes(1, "little") + apn_bytes

Expand Down Expand Up @@ -102,6 +102,11 @@ def run(self):
addr_group = parser.add_mutually_exclusive_group(required=True)
addr_group.add_argument("--app", type=lambda x: int(x, 0), help="Application ID to configure")
parser.add_argument("--apn", type=str, required=True, help="LTE APN value")
family_group = parser.add_mutually_exclusive_group()
family_group.add_argument("--ipv4", dest="family", action="store_const", const=0, help="IPv4")
family_group.add_argument("--ipv6", dest="family", action="store_const", const=1, help="IPv6")
family_group.add_argument("--ipv4v6", dest="family", action="store_const", default=2, const=2, help="IPv4v6")
family_group.add_argument("--nonip", dest="family", action="store_const", const=3, help="NonIP")

args = parser.parse_args()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
from http import HTTPStatus
from typing import Any, Optional, Union, cast

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.device_logger_state import DeviceLoggerState
from ...types import Response


def _get_kwargs(
device_id: str,
index: int,
) -> dict[str, Any]:
_kwargs: dict[str, Any] = {
"method": "get",
"url": f"/device/deviceId/{device_id}/loggerState/{index}",
}

return _kwargs


def _parse_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Optional[Union[Any, DeviceLoggerState]]:
if response.status_code == 200:
response_200 = DeviceLoggerState.from_dict(response.json())

return response_200
if response.status_code == 404:
response_404 = cast(Any, None)
return response_404
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None


def _build_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Response[Union[Any, DeviceLoggerState]]:
return Response(
status_code=HTTPStatus(response.status_code),
content=response.content,
headers=response.headers,
parsed=_parse_response(client=client, response=response),
)


def sync_detailed(
device_id: str,
index: int,
*,
client: Union[AuthenticatedClient, Client],
) -> Response[Union[Any, DeviceLoggerState]]:
"""Get logger state by DeviceID and index

Args:
device_id (str):
index (int):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[Union[Any, DeviceLoggerState]]
"""

kwargs = _get_kwargs(
device_id=device_id,
index=index,
)

response = client.get_httpx_client().request(
**kwargs,
)

return _build_response(client=client, response=response)


def sync(
device_id: str,
index: int,
*,
client: Union[AuthenticatedClient, Client],
) -> Optional[Union[Any, DeviceLoggerState]]:
"""Get logger state by DeviceID and index

Args:
device_id (str):
index (int):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Union[Any, DeviceLoggerState]
"""

return sync_detailed(
device_id=device_id,
index=index,
client=client,
).parsed


async def asyncio_detailed(
device_id: str,
index: int,
*,
client: Union[AuthenticatedClient, Client],
) -> Response[Union[Any, DeviceLoggerState]]:
"""Get logger state by DeviceID and index

Args:
device_id (str):
index (int):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[Union[Any, DeviceLoggerState]]
"""

kwargs = _get_kwargs(
device_id=device_id,
index=index,
)

response = await client.get_async_httpx_client().request(**kwargs)

return _build_response(client=client, response=response)


async def asyncio(
device_id: str,
index: int,
*,
client: Union[AuthenticatedClient, Client],
) -> Optional[Union[Any, DeviceLoggerState]]:
"""Get logger state by DeviceID and index

Args:
device_id (str):
index (int):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Union[Any, DeviceLoggerState]
"""

return (
await asyncio_detailed(
device_id=device_id,
index=index,
client=client,
)
).parsed
177 changes: 177 additions & 0 deletions src/infuse_iot/api_client/api/key/derive_device_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
from http import HTTPStatus
from typing import Any, Optional, Union

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.derive_device_key_body import DeriveDeviceKeyBody
from ...models.error import Error
from ...models.key import Key
from ...types import Response


def _get_kwargs(
*,
body: DeriveDeviceKeyBody,
) -> dict[str, Any]:
headers: dict[str, Any] = {}

_kwargs: dict[str, Any] = {
"method": "post",
"url": "/key/derived/device",
}

_body = body.to_dict()

_kwargs["json"] = _body
headers["Content-Type"] = "application/json"

_kwargs["headers"] = headers
return _kwargs


def _parse_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Optional[Union[Error, Key]]:
if response.status_code == 200:
response_200 = Key.from_dict(response.json())

return response_200
if response.status_code == 400:
response_400 = Error.from_dict(response.json())

return response_400
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None


def _build_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Response[Union[Error, Key]]:
return Response(
status_code=HTTPStatus(response.status_code),
content=response.content,
headers=response.headers,
parsed=_parse_response(client=client, response=response),
)


def sync_detailed(
*,
client: Union[AuthenticatedClient, Client],
body: DeriveDeviceKeyBody,
) -> Response[Union[Error, Key]]:
"""Derive a device key for encryption

Generate a derived key to use for device level encrpytion, if security state is provided, it will be
used to derive the key, otherwise the last stored security state will be used.

Args:
body (DeriveDeviceKeyBody):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[Union[Error, Key]]
"""

kwargs = _get_kwargs(
body=body,
)

response = client.get_httpx_client().request(
**kwargs,
)

return _build_response(client=client, response=response)


def sync(
*,
client: Union[AuthenticatedClient, Client],
body: DeriveDeviceKeyBody,
) -> Optional[Union[Error, Key]]:
"""Derive a device key for encryption

Generate a derived key to use for device level encrpytion, if security state is provided, it will be
used to derive the key, otherwise the last stored security state will be used.

Args:
body (DeriveDeviceKeyBody):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Union[Error, Key]
"""

return sync_detailed(
client=client,
body=body,
).parsed


async def asyncio_detailed(
*,
client: Union[AuthenticatedClient, Client],
body: DeriveDeviceKeyBody,
) -> Response[Union[Error, Key]]:
"""Derive a device key for encryption

Generate a derived key to use for device level encrpytion, if security state is provided, it will be
used to derive the key, otherwise the last stored security state will be used.

Args:
body (DeriveDeviceKeyBody):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[Union[Error, Key]]
"""

kwargs = _get_kwargs(
body=body,
)

response = await client.get_async_httpx_client().request(**kwargs)

return _build_response(client=client, response=response)


async def asyncio(
*,
client: Union[AuthenticatedClient, Client],
body: DeriveDeviceKeyBody,
) -> Optional[Union[Error, Key]]:
"""Derive a device key for encryption

Generate a derived key to use for device level encrpytion, if security state is provided, it will be
used to derive the key, otherwise the last stored security state will be used.

Args:
body (DeriveDeviceKeyBody):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Union[Error, Key]
"""

return (
await asyncio_detailed(
client=client,
body=body,
)
).parsed
Loading