Coverage for faststream/specification/asyncapi/v3_0_0/schema/channels.py: 86%

20 statements  

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

1from pydantic import BaseModel 

2from typing_extensions import Self 

3 

4from faststream._internal._compat import PYDANTIC_V2 

5from faststream.specification.asyncapi.v3_0_0.schema.bindings import ChannelBinding 

6from faststream.specification.asyncapi.v3_0_0.schema.message import Message 

7from faststream.specification.schema import PublisherSpec, SubscriberSpec 

8 

9from .utils import Reference 

10 

11 

12class Channel(BaseModel): 

13 """A class to represent a channel. 

14 

15 Attributes: 

16 address: A string representation of this channel's address, absent 

17 where there is none to give. 

18 description : optional description of the channel 

19 servers : optional list of servers associated with the channel 

20 bindings : optional channel binding 

21 parameters : optional parameters associated with the channel 

22 

23 Configurations: 

24 model_config : configuration for the model (only applicable for Pydantic version 2) 

25 Config : configuration for the class (only applicable for Pydantic version 1) 

26 """ 

27 

28 address: str | None = None 

29 description: str | None = None 

30 servers: list[dict[str, str]] | None = None 

31 messages: dict[str, Message | Reference] 

32 bindings: ChannelBinding | None = None 

33 

34 # TODO: 

35 # parameters: Optional[Parameter] = None 

36 

37 if PYDANTIC_V2: 37 ↛ 42line 37 didn't jump to line 42 because the condition on line 37 was always true

38 model_config = {"extra": "allow"} 

39 

40 else: 

41 

42 class Config: 

43 extra = "allow" 

44 

45 @classmethod 

46 def from_sub( 

47 cls, 

48 subscriber: SubscriberSpec, 

49 servers: list[dict[str, str]] | None = None, 

50 ) -> Self: 

51 message = subscriber.operation.message 

52 assert message.title 

53 

54 *left, right = message.title.split(":") 

55 message.title = ":".join((*left, f"Subscribe{right}")) 

56 

57 return cls( 

58 description=subscriber.description, 

59 address=subscriber.address, 

60 messages={ 

61 "SubscribeMessage": Message.from_spec(message), 

62 }, 

63 bindings=ChannelBinding.from_sub(subscriber.bindings), 

64 servers=servers, 

65 ) 

66 

67 @classmethod 

68 def from_pub( 

69 cls, 

70 publisher: PublisherSpec, 

71 servers: list[dict[str, str]] | None = None, 

72 ) -> Self: 

73 return cls( 

74 description=publisher.description, 

75 address=publisher.address, 

76 messages={ 

77 "Message": Message.from_spec(publisher.operation.message), 

78 }, 

79 bindings=ChannelBinding.from_pub(publisher.bindings), 

80 servers=servers, 

81 )