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

1""" 

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

3 

4Classes: 

5- ``henrri_connect.units.asynchro.AsyncUnitsClient`` : Accès asynchrone aux endpoints unités. 

6 

7Notes: 

8- Utiliser de préférence l'objet ``henrri_connect.AsyncHenrriClient`` 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 AsyncHenrriClient 

19 

20UNITS_ENDPOINT = "/v1/units" 

21 

22class AsyncUnitsClient: 

23 """ 

24 Accès asynchrone aux unités. 

25 

26 Arugments 

27 - client (AsyncHenrriClient) : Client asynchrone pour acceder aux endpoints. 

28 

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

34 

35 def __init__(self, client: AsyncHenrriClient) -> None: 

36 self._c = client 

37 

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

39 """ 

40 Liste toutes les unités disponibles. 

41 

42 Arguments 

43 - ``None`` 

44 

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

50 

51 async def add(self, unit: Unit) -> Unit: 

52 """ 

53 Crée une nouvelle unité. 

54 

55 Arguments 

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

57 

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

67 

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

69 """ 

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

71 

72 Arguments 

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

74 

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