Coverage for src/henrri_connect/units/asynchro.py: 88%
16 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-05 10:54 +0200
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-05 10:54 +0200
1"""
2Sous-client pour les endpoints /v1/units.
4Classes:
5- ``henrri_connect.units.asynchro.AsyncUnitsClient`` : Accès asynchrone aux endpoints unités.
7Notes:
8- Utiliser de préférence l'objet ``henrri_connect.AsyncHenrriClient`` pour acceder aux endpoints.
9"""
11from __future__ import annotations
13from typing import TYPE_CHECKING
15from ..models import ListResponse, Unit
17if TYPE_CHECKING:
18 from ..connect import AsyncHenrriClient
20UNITS_ENDPOINT = "/v1/units"
22class AsyncUnitsClient:
23 """
24 Accès asynchrone aux unités.
26 Arugments
27 - client (AsyncHenrriClient) : Client asynchrone pour acceder aux endpoints.
29 Methods
30 - list_units() : Liste toutes les unités disponibles.
31 - add(unit: Unit) : Crée une nouvelle unité.
32 - get(unit_id: int) : Récupère une unité par son identifiant.
33 """
35 def __init__(self, client: AsyncHenrriClient) -> None:
36 self._c = client
38 async def list_units(self) -> ListResponse[Unit]:
39 """
40 Liste toutes les unités disponibles.
42 Arguments
43 - ``None``
45 Returns
46 - ``ListResponse[Unit]`` : Liste de unités.
47 """
48 resp = await self._c.request("GET", UNITS_ENDPOINT)
49 return ListResponse[Unit].model_validate(resp.json())
51 async def add(self, unit: Unit) -> Unit:
52 """
53 Crée une nouvelle unité.
55 Arguments
56 - ``unit: Unit`` : Unité à crée.
58 Returns
59 - ``Unit`` : Unité crée.
60 """
61 resp = await self._c.request(
62 "POST",
63 UNITS_ENDPOINT,
64 json=unit.model_dump(by_alias=True, exclude_unset=True, exclude_none=True),
65 )
66 return Unit.model_validate(resp.json())
68 async def get(self, unit_id: int) -> Unit:
69 """
70 Récupère une unité par son identifiant.
72 Arguments
73 - ``unit_id`` (int) : Identifiant de l'unité.
75 Returns
76 - ``Unit`` : Unité.
77 """
78 resp = await self._c.request("GET", f"{UNITS_ENDPOINT}/{unit_id}")
79 return Unit.model_validate(resp.json())