Skip to content

Commit ddb95f7

Browse files
DanielePalaiaDanielePalaia
andauthored
adding headers and custom ExchangeTypes (#47)
* adding headers and custom ExchangeTypes * adding a class for CustomExchangeSpec --------- Co-authored-by: DanielePalaia <daniele985@@gmail.com>
1 parent 2eb822c commit ddb95f7

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

rabbitmq_amqp_python_client/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .connection import Connection
77
from .consumer import Consumer
88
from .entities import (
9+
ExchangeCustomSpecification,
910
ExchangeSpecification,
1011
ExchangeToExchangeBindingSpecification,
1112
ExchangeToQueueBindingSpecification,
@@ -75,4 +76,5 @@
7576
"OffsetSpecification",
7677
"OutcomeState",
7778
"Environment",
79+
"ExchangeCustomSpecification",
7880
]

rabbitmq_amqp_python_client/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ExchangeType(enum.Enum):
2424
direct = "direct"
2525
topic = "topic"
2626
fanout = "fanout"
27+
headers = "headers"
2728

2829

2930
class QueueType(enum.Enum):

rabbitmq_amqp_python_client/entities.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ class ExchangeSpecification:
2020
is_durable: bool = True
2121

2222

23+
@dataclass
24+
class ExchangeCustomSpecification:
25+
name: str
26+
exchange_type: str
27+
arguments: dict[str, str] = field(default_factory=dict)
28+
is_auto_delete: bool = False
29+
is_internal: bool = False
30+
is_durable: bool = True
31+
32+
2333
@dataclass
2434
class QueueInfo:
2535
name: str

rabbitmq_amqp_python_client/management.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .address_helper import AddressHelper
66
from .common import CommonValues, QueueType
77
from .entities import (
8+
ExchangeCustomSpecification,
89
ExchangeSpecification,
910
ExchangeToExchangeBindingSpecification,
1011
ExchangeToQueueBindingSpecification,
@@ -98,15 +99,21 @@ def _request(
9899
return msg
99100

100101
def declare_exchange(
101-
self, exchange_specification: ExchangeSpecification
102-
) -> ExchangeSpecification:
102+
self,
103+
exchange_specification: Union[
104+
ExchangeSpecification, ExchangeCustomSpecification
105+
],
106+
) -> Union[ExchangeSpecification, ExchangeCustomSpecification]:
103107
logger.debug("declare_exchange operation called")
104-
body = {}
108+
body: dict[str, Any] = {}
105109
body["auto_delete"] = exchange_specification.is_auto_delete
106110
body["durable"] = exchange_specification.is_durable
107-
body["type"] = exchange_specification.exchange_type.value # type: ignore
111+
if isinstance(exchange_specification, ExchangeSpecification):
112+
body["type"] = exchange_specification.exchange_type.value
113+
elif isinstance(exchange_specification, ExchangeCustomSpecification):
114+
body["type"] = exchange_specification.exchange_type
108115
body["internal"] = exchange_specification.is_internal
109-
body["arguments"] = exchange_specification.arguments # type: ignore
116+
body["arguments"] = exchange_specification.arguments
110117

111118
path = AddressHelper.exchange_address(exchange_specification.name)
112119

tests/test_management.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ def test_declare_delete_exchange(management: Management) -> None:
2929
management.delete_exchange(exchange_name)
3030

3131

32+
def test_declare_delete_exchange_headers(management: Management) -> None:
33+
34+
exchange_name = "test-exchange"
35+
36+
exchange_info = management.declare_exchange(
37+
ExchangeSpecification(name=exchange_name, exchange_type=ExchangeType.headers)
38+
)
39+
40+
assert exchange_info.name == exchange_name
41+
42+
management.delete_exchange(exchange_name)
43+
44+
3245
def test_declare_delete_exchange_with_args(management: Management) -> None:
3346

3447
exchange_name = "test-exchange-with-args"

0 commit comments

Comments
 (0)