Skip to content

Commit a4a15c8

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

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

rabbitmq_amqp_python_client/address_helper.py

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,57 @@
11
from .entities import BindingSpecification
22

33

4+
def binding_path_with_exchange_queue(bind_specification: BindingSpecification) -> str:
5+
binding_path_wth_exchange_queue_key = (
6+
"/bindings"
7+
+ "/"
8+
+ "src="
9+
+ bind_specification.source_exchange
10+
+ ";"
11+
+ "dstq="
12+
+ bind_specification.destination_queue
13+
+ ";key="
14+
+ bind_specification.binding_key
15+
+ ";args="
16+
)
17+
return binding_path_wth_exchange_queue_key
18+
19+
20+
def encode_path_segment(input: str) -> str:
21+
encoded_input = ""
22+
for character in input:
23+
if is_unreserved(character):
24+
encoded_input += character
25+
26+
else:
27+
# encoded_input += "%%%02X" % character
28+
encoded_input += hex(ord(character))
29+
30+
return encoded_input
31+
32+
433
def exchange_address(exchange_name: str, routing_key: str = "") -> str:
534
if routing_key == "":
6-
path = "/exchanges/" + exchange_name
35+
path = "/exchanges/" + encode_path_segment(exchange_name)
736
else:
8-
path = "/exchanges/" + exchange_name + "/" + routing_key
37+
path = (
38+
"/exchanges/"
39+
+ encode_path_segment(exchange_name)
40+
+ "/"
41+
+ encode_path_segment(routing_key)
42+
)
943

1044
return path
1145

1246

13-
def queue_address(name: str) -> str:
14-
path = "/queues/" + name
47+
def queue_address(queue_name: str) -> str:
48+
path = "/queues/" + encode_path_segment(queue_name)
49+
50+
return path
51+
52+
53+
def purge_queue_address(queue_name: str) -> str:
54+
path = "/queues/" + encode_path_segment(queue_name) + "/messages"
1555

1656
return path
1757

@@ -28,17 +68,29 @@ def path_address() -> str:
2868
return path
2969

3070

71+
def is_unreserved(input: str) -> bool:
72+
return (
73+
(input >= "A" and input <= "Z")
74+
or (input >= "a" and input <= "z")
75+
or (input >= "0" and input <= "9")
76+
or input == "-"
77+
or input == "."
78+
or input == "_"
79+
or input == "~"
80+
)
81+
82+
3183
def binding_path_with_exchange_queue(bind_specification: BindingSpecification) -> str:
3284
binding_path_wth_exchange_queue_key = (
3385
"/bindings"
3486
+ "/"
3587
+ "src="
36-
+ bind_specification.source_exchange
88+
+ encode_path_segment(bind_specification.source_exchange)
3789
+ ";"
3890
+ "dstq="
39-
+ bind_specification.destination_queue
91+
+ encode_path_segment(bind_specification.destination_queue)
4092
+ ";key="
41-
+ bind_specification.binding_key
93+
+ encode_path_segment(bind_specification.binding_key)
4294
+ ";args="
4395
)
4496
return binding_path_wth_exchange_queue_key

0 commit comments

Comments
 (0)