Coverage for faststream/nats/helpers/obj_storage_declarer.py: 90%

17 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1from typing import TYPE_CHECKING, Optional 

2 

3from nats.js.api import ObjectStoreConfig 

4 

5from .state import ConnectedState, ConnectionState, EmptyConnectionState 

6 

7if TYPE_CHECKING: 

8 from nats.js import JetStreamContext 

9 from nats.js.api import Placement, StorageType 

10 from nats.js.object_store import ObjectStore 

11 

12 

13class OSBucketDeclarer: 

14 __slots__ = ( 

15 "__state", 

16 "buckets", 

17 ) 

18 

19 buckets: dict[str, "ObjectStore"] 

20 

21 def __init__(self) -> None: 

22 self.buckets = {} 

23 

24 self.__state: ConnectionState[JetStreamContext] = EmptyConnectionState() 

25 

26 def connect(self, connection: "JetStreamContext") -> None: 

27 self.__state = ConnectedState(connection) 

28 

29 def disconnect(self) -> None: 

30 self.__state = EmptyConnectionState() 

31 

32 async def create_object_store( 

33 self, 

34 bucket: str, 

35 *, 

36 description: str | None = None, 

37 ttl: float | None = None, 

38 max_bytes: int | None = None, 

39 storage: Optional["StorageType"] = None, 

40 replicas: int = 1, 

41 placement: Optional["Placement"] = None, 

42 # custom 

43 declare: bool = True, 

44 ) -> "ObjectStore": 

45 if (object_store := self.buckets.get(bucket)) is None: 

46 if declare: 46 ↛ 60line 46 didn't jump to line 60 because the condition on line 46 was always true

47 object_store = await self.__state.connection.create_object_store( 

48 bucket=bucket, 

49 config=ObjectStoreConfig( 

50 bucket=bucket, 

51 description=description, 

52 ttl=ttl, 

53 max_bytes=max_bytes, 

54 storage=storage, 

55 replicas=replicas, 

56 placement=placement, 

57 ), 

58 ) 

59 else: 

60 object_store = await self.__state.connection.object_store(bucket) 

61 

62 self.buckets[bucket] = object_store 

63 

64 return object_store