Skip to content

Commit 250af71

Browse files
author
DanielePalaia
committed
fixes for code review
1 parent 515e7fe commit 250af71

File tree

9 files changed

+55
-94
lines changed

9 files changed

+55
-94
lines changed

examples/getting_started/getting_started.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
from rabbitmq_amqp_python_client import ( # SSlConfigurationContext,; SslConfigurationContext,; ClientCert,
55
AddressHelper,
66
AMQPMessagingHandler,
7-
BindingSpecification,
87
Connection,
98
Environment,
109
Event,
1110
ExchangeSpecification,
11+
ExchangeToQueueBindingSpecification,
1212
Message,
1313
OutcomeState,
1414
QuorumQueueSpecification,
@@ -102,7 +102,7 @@ def main() -> None:
102102

103103
print("binding queue to exchange")
104104
bind_name = management.bind(
105-
BindingSpecification(
105+
ExchangeToQueueBindingSpecification(
106106
source_exchange=exchange_name,
107107
destination_queue=queue_name,
108108
binding_key=routing_key,

examples/reconnection/reconnection_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
from rabbitmq_amqp_python_client import (
99
AddressHelper,
1010
AMQPMessagingHandler,
11-
BindingSpecification,
1211
Connection,
1312
ConnectionClosed,
1413
Consumer,
1514
Environment,
1615
Event,
1716
ExchangeSpecification,
17+
ExchangeToQueueBindingSpecification,
1818
Management,
1919
Message,
2020
Publisher,
@@ -160,7 +160,7 @@ def main() -> None:
160160

161161
print("binding queue to exchange")
162162
bind_name = connection_configuration.management.bind(
163-
BindingSpecification(
163+
ExchangeToQueueBindingSpecification(
164164
source_exchange=exchange_name,
165165
destination_queue=queue_name,
166166
binding_key=routing_key,

examples/tls/tls_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
from rabbitmq_amqp_python_client import ( # SSlConfigurationContext,; SslConfigurationContext,; ClientCert,
55
AddressHelper,
66
AMQPMessagingHandler,
7-
BindingSpecification,
87
ClientCert,
98
Connection,
109
Environment,
1110
Event,
1211
ExchangeSpecification,
12+
ExchangeToQueueBindingSpecification,
1313
Message,
1414
QuorumQueueSpecification,
1515
SslConfigurationContext,
@@ -102,7 +102,7 @@ def main() -> None:
102102

103103
print("binding queue to exchange")
104104
bind_name = management.bind(
105-
BindingSpecification(
105+
ExchangeToQueueBindingSpecification(
106106
source_exchange=exchange_name,
107107
destination_queue=queue_name,
108108
binding_key=routing_key,

rabbitmq_amqp_python_client/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
from .connection import Connection
77
from .consumer import Consumer
88
from .entities import (
9-
BindingSpecification,
109
ExchangeSpecification,
10+
ExchangeToExchangeBindingSpecification,
11+
ExchangeToQueueBindingSpecification,
1112
OffsetSpecification,
1213
StreamOptions,
1314
)
1415
from .environment import Environment
1516
from .exceptions import (
16-
AmqpValidationException,
1717
ArgumentOutOfRangeException,
1818
ValidationCodeException,
1919
)
@@ -53,7 +53,8 @@
5353
"QuorumQueueSpecification",
5454
"ClassicQueueSpecification",
5555
"StreamSpecification",
56-
"BindingSpecification",
56+
"ExchangeToQueueBindingSpecification",
57+
"ExchangeToExchangeBindingSpecification",
5758
"QueueType",
5859
"Publisher",
5960
"Message",
@@ -74,5 +75,4 @@
7475
"OffsetSpecification",
7576
"OutcomeState",
7677
"Environment",
77-
"AmqpValidationException",
7878
]

rabbitmq_amqp_python_client/address_helper.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from typing import Optional
22

3-
from .entities import BindingSpecification
3+
from .entities import (
4+
ExchangeToExchangeBindingSpecification,
5+
ExchangeToQueueBindingSpecification,
6+
)
47
from .qpid.proton._message import Message
58

69

@@ -63,7 +66,7 @@ def path_address() -> str:
6366

6467
@staticmethod
6568
def binding_path_with_exchange_queue(
66-
bind_specification: BindingSpecification,
69+
bind_specification: ExchangeToQueueBindingSpecification,
6770
) -> str:
6871
if bind_specification.binding_key is not None:
6972
key = ";key=" + encode_path_segment(bind_specification.binding_key)
@@ -85,7 +88,7 @@ def binding_path_with_exchange_queue(
8588

8689
@staticmethod
8790
def binding_path_with_exchange_exchange(
88-
bind_specification: BindingSpecification,
91+
bind_specification: ExchangeToExchangeBindingSpecification,
8992
) -> str:
9093
binding_path_wth_exchange_exchange_key = (
9194
"/bindings"

rabbitmq_amqp_python_client/entities.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ class OffsetSpecification(Enum):
4141

4242

4343
@dataclass
44-
class BindingSpecification:
44+
class ExchangeToQueueBindingSpecification:
4545
source_exchange: str
46+
destination_queue: str
47+
binding_key: Optional[str] = None
48+
49+
50+
@dataclass
51+
class ExchangeToExchangeBindingSpecification:
52+
source_exchange: str
53+
destination_exchange: str
4654
binding_key: Optional[str] = None
47-
destination_exchange: Optional[str] = None
48-
destination_queue: Optional[str] = None
4955

5056

5157
class StreamOptions:

rabbitmq_amqp_python_client/exceptions.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,3 @@ def __init__(self, msg: str):
1414

1515
def __str__(self) -> str:
1616
return repr(self.msg)
17-
18-
19-
class AmqpValidationException(BaseException):
20-
# Constructor or Initializer
21-
def __init__(self, msg: str):
22-
self.msg = msg
23-
24-
def __str__(self) -> str:
25-
return repr(self.msg)

rabbitmq_amqp_python_client/management.py

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
from .address_helper import AddressHelper
66
from .common import CommonValues, QueueType
77
from .entities import (
8-
BindingSpecification,
98
ExchangeSpecification,
9+
ExchangeToExchangeBindingSpecification,
10+
ExchangeToQueueBindingSpecification,
1011
QueueInfo,
1112
)
12-
from .exceptions import (
13-
AmqpValidationException,
14-
ValidationCodeException,
15-
)
13+
from .exceptions import ValidationCodeException
1614
from .options import ReceiverOption, SenderOption
1715
from .qpid.proton._message import Message
1816
from .qpid.proton.utils import (
@@ -304,19 +302,23 @@ def _validate_reponse_code(
304302
"wrong response code received: " + str(response_code)
305303
)
306304

307-
def bind(self, bind_specification: BindingSpecification) -> str:
305+
def bind(
306+
self,
307+
bind_specification: Union[
308+
ExchangeToQueueBindingSpecification, ExchangeToExchangeBindingSpecification
309+
],
310+
) -> str:
308311
logger.debug("Bind Operation called")
309-
self._validate_binding(bind_specification)
310312

311313
body = {}
312314
if bind_specification.binding_key is not None:
313315
body["binding_key"] = bind_specification.binding_key
314316
else:
315317
body["binding_key"] = ""
316318
body["source"] = bind_specification.source_exchange
317-
if bind_specification.destination_queue is not None:
319+
if isinstance(bind_specification, ExchangeToQueueBindingSpecification):
318320
body["destination_queue"] = bind_specification.destination_queue
319-
elif bind_specification.destination_exchange is not None:
321+
elif isinstance(bind_specification, ExchangeToExchangeBindingSpecification):
320322
body["destination_exchange"] = bind_specification.destination_exchange
321323

322324
body["arguments"] = {} # type: ignore
@@ -332,47 +334,34 @@ def bind(self, bind_specification: BindingSpecification) -> str:
332334
],
333335
)
334336

335-
if bind_specification.destination_queue is not None:
337+
if isinstance(bind_specification, ExchangeToQueueBindingSpecification):
336338
binding_path = AddressHelper.binding_path_with_exchange_queue(
337339
bind_specification
338340
)
339-
elif bind_specification.destination_exchange is not None:
341+
elif isinstance(bind_specification, ExchangeToExchangeBindingSpecification):
340342
binding_path = AddressHelper.binding_path_with_exchange_exchange(
341343
bind_specification
342344
)
343345

344346
return binding_path
345347

346-
def _validate_binding(self, bind_specification: BindingSpecification) -> None:
347-
if (
348-
bind_specification.destination_queue is not None
349-
and bind_specification.destination_exchange is not None
350-
):
351-
raise AmqpValidationException(
352-
"just one of destination_queue and destination_exchange of BindingSpecification must be set "
353-
"for a binding operation"
354-
)
355-
356-
if (
357-
bind_specification.destination_queue is None
358-
and bind_specification.destination_exchange is None
359-
):
360-
raise AmqpValidationException(
361-
"at least one of destination_queue and destination_exchange of BindingSpecification must be set "
362-
"for a binding operation"
363-
)
364-
365-
def unbind(self, bind_specification: Union[BindingSpecification, str]) -> None:
348+
def unbind(
349+
self,
350+
bind_specification: Union[
351+
str,
352+
ExchangeToQueueBindingSpecification,
353+
ExchangeToExchangeBindingSpecification,
354+
],
355+
) -> None:
366356
logger.debug("UnBind Operation called")
367357
if isinstance(bind_specification, str):
368358
binding_name = bind_specification
369359
else:
370-
self._validate_binding(bind_specification)
371-
if bind_specification.destination_queue is not None:
360+
if isinstance(bind_specification, ExchangeToQueueBindingSpecification):
372361
binding_name = AddressHelper.binding_path_with_exchange_queue(
373362
bind_specification
374363
)
375-
elif bind_specification.destination_exchange is not None:
364+
elif isinstance(bind_specification, ExchangeToExchangeBindingSpecification):
376365
binding_name = AddressHelper.binding_path_with_exchange_exchange(
377366
bind_specification
378367
)

tests/test_management.py

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from datetime import timedelta
22

33
from rabbitmq_amqp_python_client import (
4-
AmqpValidationException,
5-
BindingSpecification,
64
ClassicQueueSpecification,
75
ExchangeSpecification,
6+
ExchangeToExchangeBindingSpecification,
7+
ExchangeToQueueBindingSpecification,
88
ExchangeType,
99
Management,
1010
QueueType,
@@ -74,7 +74,7 @@ def test_bind_exchange_to_queue(management: Management) -> None:
7474
management.declare_queue(QuorumQueueSpecification(name=queue_name))
7575

7676
binding_exchange_queue_path = management.bind(
77-
BindingSpecification(
77+
ExchangeToQueueBindingSpecification(
7878
source_exchange=exchange_name,
7979
destination_queue=queue_name,
8080
binding_key=routing_key,
@@ -99,34 +99,6 @@ def test_bind_exchange_to_queue(management: Management) -> None:
9999
management.unbind(binding_exchange_queue_path)
100100

101101

102-
def test_bind_no_destination(management: Management) -> None:
103-
104-
exchange_name = "test-bind-exchange-to-queue-exchange"
105-
queue_name = "test-bind-exchange-to-queue-queue"
106-
routing_key = "routing-key"
107-
raised = False
108-
109-
management.declare_exchange(ExchangeSpecification(name=exchange_name))
110-
111-
management.declare_queue(QuorumQueueSpecification(name=queue_name))
112-
113-
try:
114-
management.bind(
115-
BindingSpecification(
116-
source_exchange=exchange_name,
117-
binding_key=routing_key,
118-
)
119-
)
120-
except AmqpValidationException:
121-
raised = True
122-
123-
assert raised is True
124-
125-
management.delete_exchange(exchange_name)
126-
127-
management.delete_queue(queue_name)
128-
129-
130102
def test_bind_exchange_to_queue_without_key(management: Management) -> None:
131103

132104
exchange_name = "test-bind-exchange-to-queue-exchange"
@@ -137,7 +109,7 @@ def test_bind_exchange_to_queue_without_key(management: Management) -> None:
137109
management.declare_queue(QuorumQueueSpecification(name=queue_name))
138110

139111
binding_exchange_queue_path = management.bind(
140-
BindingSpecification(
112+
ExchangeToQueueBindingSpecification(
141113
source_exchange=exchange_name,
142114
destination_queue=queue_name,
143115
)
@@ -165,14 +137,14 @@ def test_bind_unbind_by_binding_spec(management: Management) -> None:
165137
management.declare_queue(QuorumQueueSpecification(name=queue_name))
166138

167139
management.bind(
168-
BindingSpecification(
140+
ExchangeToQueueBindingSpecification(
169141
source_exchange=exchange_name,
170142
destination_queue=queue_name,
171143
)
172144
)
173145

174146
management.unbind(
175-
BindingSpecification(
147+
ExchangeToQueueBindingSpecification(
176148
source_exchange=exchange_name,
177149
destination_queue=queue_name,
178150
)
@@ -194,7 +166,7 @@ def test_bind_exchange_to_exchange(management: Management) -> None:
194166
management.declare_exchange(ExchangeSpecification(name=destination_exchange_name))
195167

196168
binding_exchange_exchange_path = management.bind(
197-
BindingSpecification(
169+
ExchangeToExchangeBindingSpecification(
198170
source_exchange=source_exchange_name,
199171
destination_exchange=destination_exchange_name,
200172
binding_key=routing_key,

0 commit comments

Comments
 (0)