Skip to content

Commit 4a953c7

Browse files
author
DanielePalaia
committed
improving url helper
1 parent b01834d commit 4a953c7

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed
Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,48 @@
11
from .entities import BindingSpecification
22

33

4+
def is_unreserved(char: str) -> bool:
5+
# According to RFC 3986, unreserved characters are A-Z, a-z, 0-9, '-', '.', '_', and '~'
6+
return char.isalnum() or char in "-._~"
7+
8+
9+
def encode_path_segment(input_string: str) -> str:
10+
encoded = []
11+
12+
# Iterate over each character in the input string
13+
for char in input_string:
14+
# Check if the character is an unreserved character
15+
if is_unreserved(char):
16+
encoded.append(char) # Append as is
17+
else:
18+
# Encode character to %HH format
19+
encoded.append(f"%{ord(char):02X}")
20+
21+
return "".join(encoded)
22+
23+
424
def exchange_address(exchange_name: str, routing_key: str = "") -> str:
525
if routing_key == "":
6-
path = "/exchanges/" + exchange_name
26+
path = "/exchanges/" + encode_path_segment(exchange_name)
727
else:
8-
path = "/exchanges/" + exchange_name + "/" + routing_key
28+
path = (
29+
"/exchanges/"
30+
+ encode_path_segment(exchange_name)
31+
+ "/"
32+
+ encode_path_segment(routing_key)
33+
)
934

1035
return path
1136

1237

13-
def queue_address(name: str) -> str:
14-
path = "/queues/" + name
38+
def queue_address(queue_name: str) -> str:
39+
path = "/queues/" + encode_path_segment(queue_name)
1540

1641
return path
1742

1843

19-
def purge_queue_address(name: str) -> str:
20-
path = "/queues/" + name + "/messages"
44+
def purge_queue_address(queue_name: str) -> str:
45+
path = "/queues/" + encode_path_segment(queue_name) + "/messages"
2146

2247
return path
2348

@@ -33,12 +58,12 @@ def binding_path_with_exchange_queue(bind_specification: BindingSpecification) -
3358
"/bindings"
3459
+ "/"
3560
+ "src="
36-
+ bind_specification.source_exchange
61+
+ encode_path_segment(bind_specification.source_exchange)
3762
+ ";"
3863
+ "dstq="
39-
+ bind_specification.destination_queue
64+
+ encode_path_segment(bind_specification.destination_queue)
4065
+ ";key="
41-
+ bind_specification.binding_key
66+
+ encode_path_segment(bind_specification.binding_key)
4267
+ ";args="
4368
)
4469
return binding_path_wth_exchange_queue_key

rabbitmq_amqp_python_client/management.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def unbind(self, binding_exchange_queue_path: str) -> None:
230230
# def queue_info(self, queue_name:str):
231231

232232
# TODO
233-
def purge_queue(self, queue_name: str):
233+
def purge_queue(self, queue_name: str) -> None:
234234
logger.debug("purge_queue operation called")
235235
path = purge_queue_address(queue_name)
236236

tests/test_address_helper.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1-
"""
2-
from rabbitmq_amqp_python_client import queue_address
1+
from rabbitmq_amqp_python_client import (
2+
exchange_address,
3+
queue_address,
4+
)
35

4-
def test_encoding_simple() -> None:
6+
7+
def test_encoding_queue_simple() -> None:
58
queue = "my_queue"
69

710
address = queue_address(queue)
811

912
assert address == "/queues/my_queue"
1013

11-
def test_encoding_hex() -> None:
14+
15+
def test_encoding_queue_hex() -> None:
1216
queue = "my_queue>"
1317

1418
address = queue_address(queue)
1519

1620
assert address == "/queues/my_queue%3E"
1721

18-
"""
22+
23+
def test_encoding_exchange_hex() -> None:
24+
queue = "my_exchange/()"
25+
26+
address = exchange_address(queue)
27+
28+
assert address == "/exchanges/my_exchange%2F%28%29"

0 commit comments

Comments
 (0)