11import logging
22from typing import Literal , Optional , Union , cast
33
4+ from pika .exceptions import AMQPError
5+
46from .amqp_consumer_handler import AMQPMessagingHandler
5- from .entities import ConsumerOptions
7+ from .entities import (
8+ ConsumerOptions ,
9+ DirectReplyToConsumerOptions ,
10+ StreamConsumerOptions ,
11+ )
612from .options import (
713 ReceiverOptionUnsettled ,
814 ReceiverOptionUnsettledWithFilters ,
@@ -29,18 +35,17 @@ class Consumer:
2935 _conn (BlockingConnection): The underlying connection to RabbitMQ
3036 _addr (str): The address to consume from
3137 _handler (Optional[MessagingHandler]): Optional message handling callback
32- _stream_options (Optional[StreamConsumerOptions]): Configuration for stream consumption
38+ _consumer_options (Optional[StreamConsumerOptions]): Configuration for stream consumption
3339 _credit (Optional[int]): Flow control credit value
3440 """
3541
3642 def __init__ (
37- self ,
38- conn : BlockingConnection ,
39- addr : str ,
40- handler : Optional [AMQPMessagingHandler ] = None ,
41- stream_options : Optional [ConsumerOptions ] = None ,
42- credit : Optional [int ] = None ,
43- direct_reply_to : Optional [bool ] = None ,
43+ self ,
44+ conn : BlockingConnection ,
45+ addr : str ,
46+ handler : Optional [AMQPMessagingHandler ] = None ,
47+ consumer_options : Optional [ConsumerOptions ] = None ,
48+ credit : Optional [int ] = None ,
4449 ):
4550 """
4651 Initialize a new Consumer instance.
@@ -49,27 +54,38 @@ def __init__(
4954 conn: The blocking connection to use for consuming
5055 addr: The address to consume from
5156 handler: Optional message handler for processing received messages
52- stream_options : Optional configuration for stream-based consumption
57+ consumer_options : Optional configuration for stream-based consumption
5358 credit: Optional credit value for flow control
5459 """
5560 self ._receiver : Optional [BlockingReceiver ] = None
5661 self ._conn = conn
5762 self ._addr = addr
5863 self ._handler = handler
59- self ._stream_options = stream_options
64+ self ._consumer_options = consumer_options
6065 self ._credit = credit
6166 self ._consumers : list [Consumer ] = []
62- self ._direct_reply_to = direct_reply_to
6367 self ._open ()
6468
6569 def _open (self ) -> None :
6670 if self ._receiver is None :
6771 logger .debug ("Creating Receiver" )
6872 self ._receiver = self ._create_receiver (self ._addr )
6973
74+ def get_queue_address (self ) -> Optional [str ]:
75+ """
76+ Get the name of the queue from the address.
77+
78+ Returns:
79+ str: The name of the queue.
80+ """
81+ if self ._receiver is not None :
82+ return cast (Optional [str ], self ._receiver .link .remote_source .address )
83+ else :
84+ raise AMQPError ("Receiver is not initialized" )
85+
7086 def _update_connection (self , conn : BlockingConnection ) -> None :
7187 self ._conn = conn
72- if self ._stream_options is None :
88+ if self ._consumer_options is None :
7389 logger .debug ("creating new receiver without stream" )
7490 self ._receiver = self ._conn .create_receiver (
7591 self ._addr ,
@@ -78,11 +94,11 @@ def _update_connection(self, conn: BlockingConnection) -> None:
7894 )
7995 else :
8096 logger .debug ("creating new stream receiver" )
81- self ._stream_options .offset (self ._handler .offset - 1 ) # type: ignore
97+ self ._consumer_options .offset (self ._handler .offset - 1 ) # type: ignore
8298 self ._receiver = self ._conn .create_receiver (
8399 self ._addr ,
84100 options = ReceiverOptionUnsettledWithFilters (
85- self ._addr , self ._stream_options
101+ self ._addr , self ._consumer_options
86102 ),
87103 handler = self ._handler ,
88104 )
@@ -145,34 +161,44 @@ def stop(self) -> None:
145161 self ._receiver .container .stop ()
146162
147163 def _create_receiver (self , addr : str ) -> BlockingReceiver :
148- logger . debug ( "Creating the receiver" )
149- if self ._direct_reply_to is None :
150- self . _direct_reply_to = True
164+ credit = 100
165+ if self ._credit is not None :
166+ credit = self . _credit
151167
152- if self ._direct_reply_to :
153- x = self ._conn .create_dynamic_receiver ()
154- # print(x.link.remote_source.address)
155- return x
168+ if self ._consumer_options is not None :
169+ logger .debug (
170+ "Creating the receiver, with options: %s" ,
171+ type (self ._consumer_options ).__name__ ,
172+ )
173+ else :
174+ logger .debug ("Creating the receiver, without options" )
156175
157- if self ._stream_options is None :
158- receiver = self ._conn .create_receiver (
176+ if self ._consumer_options is None :
177+ return self ._conn .create_receiver (
159178 addr ,
160179 options = ReceiverOptionUnsettled (addr ),
161180 handler = self ._handler ,
162- dynamic = self . _direct_reply_to ,
181+ credit = credit ,
163182 )
164- else :
165- receiver = self ._conn .create_receiver (
183+
184+ if isinstance (self ._consumer_options , DirectReplyToConsumerOptions ):
185+ print ("Creating dynamic receiver for direct reply-to" )
186+ x = self ._conn .create_dynamic_receiver (100 , handler = self ._handler )
187+ x .credit = credit
188+ return x
189+
190+ if isinstance (self ._consumer_options , StreamConsumerOptions ):
191+ return self ._conn .create_receiver (
166192 addr ,
167- options = ReceiverOptionUnsettledWithFilters (addr , self ._stream_options ),
193+ options = ReceiverOptionUnsettledWithFilters (
194+ addr , self ._consumer_options
195+ ),
168196 handler = self ._handler ,
169- dynamic = self ._direct_reply_to ,
170197 )
171198
172- if self ._credit is not None :
173- receiver .credit = self ._credit
174-
175- return receiver
199+ raise AMQPError (
200+ "Receiver is not initialized. No valid consumer options provided."
201+ )
176202
177203 @property
178204 def address (self ) -> str :
0 commit comments