Skip to content

Commit 17fe614

Browse files
author
DanielePalaia
committed
improving url helper
1 parent b01834d commit 17fe614

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed
Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
11
from .entities import BindingSpecification
22

33

4+
def encode_path_segment(input: str) -> str:
5+
encoded_input = ""
6+
for character in input:
7+
if is_unreserved(character):
8+
encoded_input += character
9+
10+
else:
11+
# encoded_input += "%%%02X" % character
12+
encoded_input += hex((ord(character)))
13+
14+
return encoded_input
15+
16+
417
def exchange_address(exchange_name: str, routing_key: str = "") -> str:
518
if routing_key == "":
6-
path = "/exchanges/" + exchange_name
19+
path = "/exchanges/" + encode_path_segment(exchange_name)
720
else:
8-
path = "/exchanges/" + exchange_name + "/" + routing_key
21+
path = (
22+
"/exchanges/"
23+
+ encode_path_segment(exchange_name)
24+
+ "/"
25+
+ encode_path_segment(routing_key)
26+
)
927

1028
return path
1129

1230

13-
def queue_address(name: str) -> str:
14-
path = "/queues/" + name
31+
def queue_address(queue_name: str) -> str:
32+
path = "/queues/" + encode_path_segment(queue_name)
1533

1634
return path
1735

1836

19-
def purge_queue_address(name: str) -> str:
20-
path = "/queues/" + name + "/messages"
37+
def purge_queue_address(queue_name: str) -> str:
38+
path = "/queues/" + encode_path_segment(queue_name) + "/messages"
2139

2240
return path
2341

@@ -28,17 +46,29 @@ def path_address() -> str:
2846
return path
2947

3048

49+
def is_unreserved(input: str) -> bool:
50+
return (
51+
(input >= "A" and input <= "Z")
52+
or (input >= "a" and input <= "z")
53+
or (input >= "0" and input <= "9")
54+
or input == "-"
55+
or input == "."
56+
or input == "_"
57+
or input == "~"
58+
)
59+
60+
3161
def binding_path_with_exchange_queue(bind_specification: BindingSpecification) -> str:
3262
binding_path_wth_exchange_queue_key = (
3363
"/bindings"
3464
+ "/"
3565
+ "src="
36-
+ bind_specification.source_exchange
66+
+ encode_path_segment(bind_specification.source_exchange)
3767
+ ";"
3868
+ "dstq="
39-
+ bind_specification.destination_queue
69+
+ encode_path_segment(bind_specification.destination_queue)
4070
+ ";key="
41-
+ bind_specification.binding_key
71+
+ encode_path_segment(bind_specification.binding_key)
4272
+ ";args="
4373
)
4474
return binding_path_wth_exchange_queue_key

tests/test_address_helper.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
"""
21
from rabbitmq_amqp_python_client import queue_address
32

3+
44
def test_encoding_simple() -> None:
55
queue = "my_queue"
66

77
address = queue_address(queue)
88

99
assert address == "/queues/my_queue"
1010

11+
1112
def test_encoding_hex() -> None:
1213
queue = "my_queue>"
1314

1415
address = queue_address(queue)
1516

1617
assert address == "/queues/my_queue%3E"
17-
18-
"""

0 commit comments

Comments
 (0)