Coverage for faststream/response/response.py: 97%

34 statements  

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

1from collections.abc import Sequence 

2from typing import Any 

3 

4from typing_extensions import Self 

5 

6from .publish_type import PublishType 

7 

8 

9class Response: 

10 def __init__( 

11 self, 

12 body: Any, 

13 *, 

14 headers: dict[str, Any] | None = None, 

15 correlation_id: str | None = None, 

16 ) -> None: 

17 """Initialize a handler.""" 

18 self.body = body 

19 self.headers = headers or {} 

20 self.correlation_id = correlation_id 

21 

22 def as_publish_command(self) -> "PublishCommand": 

23 """Method to transform handlers' Response result to DTO for publishers.""" 

24 return PublishCommand( 

25 body=self.body, 

26 headers=self.headers, 

27 correlation_id=self.correlation_id, 

28 _publish_type=PublishType.PUBLISH, 

29 ) 

30 

31 def get_publish_key(self) -> Any | None: # ruff: ignore[no-self-use] 

32 """Get the key for publishing this message. 

33 

34 Override this method in subclasses to provide broker-specific keys. 

35 Default implementation returns None (no key). 

36 

37 Returns: 

38 The key for publishing, or None if this Response type doesn't use keys. 

39 """ 

40 return None 

41 

42 

43class PublishCommand(Response): 

44 def __init__( 

45 self, 

46 body: Any, 

47 *, 

48 _publish_type: PublishType, 

49 reply_to: str = "", 

50 destination: str = "", 

51 correlation_id: str | None = None, 

52 headers: dict[str, Any] | None = None, 

53 ) -> None: 

54 super().__init__( 

55 body, 

56 headers=headers, 

57 correlation_id=correlation_id, 

58 ) 

59 

60 self.destination = destination 

61 self.reply_to = reply_to 

62 

63 self.publish_type = _publish_type 

64 

65 @property 

66 def batch_bodies(self) -> tuple["Any", ...]: 

67 if self.body is not None: 

68 return (self.body,) 

69 return () 

70 

71 def add_headers( 

72 self, 

73 headers: dict[str, Any], 

74 *, 

75 override: bool = True, 

76 ) -> None: 

77 if override: 

78 self.headers |= headers 

79 else: 

80 self.headers = headers | self.headers 

81 

82 @classmethod 

83 def from_cmd(cls, cmd: Self) -> Self: 

84 raise NotImplementedError 

85 

86 

87class BatchPublishCommand(PublishCommand): 

88 def __init__( 

89 self, 

90 body: Any, 

91 /, 

92 *bodies: Any, 

93 _publish_type: PublishType, 

94 reply_to: str = "", 

95 destination: str = "", 

96 correlation_id: str | None = None, 

97 headers: dict[str, Any] | None = None, 

98 ) -> None: 

99 super().__init__( 

100 body, 

101 headers=headers, 

102 correlation_id=correlation_id, 

103 destination=destination, 

104 reply_to=reply_to, 

105 _publish_type=_publish_type, 

106 ) 

107 self.extra_bodies = bodies 

108 

109 @property 

110 def batch_bodies(self) -> tuple["Any", ...]: 

111 return (*super().batch_bodies, *self.extra_bodies) 

112 

113 @batch_bodies.setter 

114 def batch_bodies(self, value: Sequence["Any"]) -> None: 

115 if len(value) == 0: 

116 self.body = None 

117 self.extra_bodies = () 

118 else: 

119 self.body = value[0] 

120 self.extra_bodies = tuple(value[1:]) 

121 

122 @classmethod 

123 def from_cmd( 

124 cls, 

125 cmd: "PublishCommand", 

126 *, 

127 batch: bool = False, 

128 ) -> "BatchPublishCommand": 

129 raise NotImplementedError 

130 

131 @staticmethod 

132 def _parse_bodies(body: Any, *, batch: bool = False) -> tuple[Any, tuple[Any, ...]]: 

133 extra_bodies = [] 

134 if batch and isinstance(body, Sequence) and not isinstance(body, (str, bytes)): 

135 if body: 

136 body, extra_bodies = body[0], body[1:] 

137 else: 

138 body = None 

139 return body, tuple(extra_bodies)