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

1""" 

2Sous-client pour les endpoints /v1/units. 

3 

4Classes: 

5- ``henrri_connect.units.synchro.SyncUnitsClient`` : Accès synchrone aux endpoints unités. 

6 

7Notes: 

8- Utiliser de préférence l'objet ``henrri_connect.SyncHenrriClient`` pour acceder aux endpoints. 

9""" 

10 

11from __future__ import annotations 

12 

13from typing import TYPE_CHECKING 

14 

15from ..models import ListResponse, Unit 

16 

17if TYPE_CHECKING: 

18 from ..connect import ( 

19 SyncHenrriClient, 

20 ) 

21 

22UNITS_ENDPOINT = "/v1/units" 

23 

24class SyncUnitsClient: 

25 """ 

26 Accès synchrone aux unités. 

27 

28 Arguments 

29 - client (SyncHenrriClient) : Client synchrone pour acceder aux endpoints. 

30 

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 """ 

36 

37 def __init__(self, client: SyncHenrriClient) -> None: 

38 self._c = client 

39 

40 def list_units(self) -> ListResponse[Unit]: 

41 """ 

42 Liste toutes les unités disponibles. 

43 

44 Arguments 

45 - ``None`` 

46 

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()) 

52 

53 def add(self, unit: Unit) -> Unit: 

54 """ 

55 Crée une nouvelle unité. 

56 

57 Arguments 

58 - ``unit: Unit`` : Unité à crée. 

59 

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()) 

69 

70 def get(self, unit_id: int) -> Unit: 

71 """ 

72 Récupère une unité par son identifiant. 

73 

74 Arguments 

75 - ``unit_id`` (int) : Identifiant de l'unité. 

76 

77 Returns 

78 - ``Unit`` : Unité. 

79 """ 

80 resp = self._c.request("GET", f"{UNITS_ENDPOINT}/{unit_id}") 

81 return Unit.model_validate(resp.json())