Coverage for src/henrri_connect/units/synchro.py: 100%
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.synchro.SyncUnitsClient`` : Accès synchrone aux endpoints unités.
7Notes:
8- Utiliser de préférence l'objet ``henrri_connect.SyncHenrriClient`` 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 (
19 SyncHenrriClient,
20 )
22UNITS_ENDPOINT = "/v1/units"
24class SyncUnitsClient:
25 """
26 Accès synchrone aux unités.
28 Arguments
29 - client (SyncHenrriClient) : Client synchrone pour acceder aux endpoints.
31 Methods
32 - list_units() : Liste toutes les unités disponibles.
33 - add(unit: Unit) : Crée une nouvelle unité.
34 - get(unit_id: int) : Récupère une unité par son identifiant.
35 """
37 def __init__(self, client: SyncHenrriClient) -> None:
38 self._c = client
40 def list_units(self) -> ListResponse[Unit]:
41 """
42 Liste toutes les unités disponibles.
44 Arguments
45 - ``None``
47 Returns
48 - ``ListResponse[Unit]`` : Liste de unités.
49 """
50 resp = self._c.request("GET", UNITS_ENDPOINT)
51 return ListResponse[Unit].model_validate(resp.json())
53 def add(self, unit: Unit) -> Unit:
54 """
55 Crée une nouvelle unité.
57 Arguments
58 - ``unit: Unit`` : Unité à crée.
60 Returns
61 - ``Unit`` : Unité crée.
62 """
63 resp = self._c.request(
64 "POST",
65 UNITS_ENDPOINT,
66 json=unit.model_dump(by_alias=True, exclude_unset=True, exclude_none=True),
67 )
68 return Unit.model_validate(resp.json())
70 def get(self, unit_id: int) -> Unit:
71 """
72 Récupère une unité par son identifiant.
74 Arguments
75 - ``unit_id`` (int) : Identifiant de l'unité.
77 Returns
78 - ``Unit`` : Unité.
79 """
80 resp = self._c.request("GET", f"{UNITS_ENDPOINT}/{unit_id}")
81 return Unit.model_validate(resp.json())