1
1
from .entities import BindingSpecification
2
2
3
3
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
+
4
17
def exchange_address (exchange_name : str , routing_key : str = "" ) -> str :
5
18
if routing_key == "" :
6
- path = "/exchanges/" + exchange_name
19
+ path = "/exchanges/" + encode_path_segment ( exchange_name )
7
20
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
+ )
9
27
10
28
return path
11
29
12
30
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 )
15
33
16
34
return path
17
35
18
36
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"
21
39
22
40
return path
23
41
@@ -28,17 +46,29 @@ def path_address() -> str:
28
46
return path
29
47
30
48
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
+
31
61
def binding_path_with_exchange_queue (bind_specification : BindingSpecification ) -> str :
32
62
binding_path_wth_exchange_queue_key = (
33
63
"/bindings"
34
64
+ "/"
35
65
+ "src="
36
- + bind_specification .source_exchange
66
+ + encode_path_segment ( bind_specification .source_exchange )
37
67
+ ";"
38
68
+ "dstq="
39
- + bind_specification .destination_queue
69
+ + encode_path_segment ( bind_specification .destination_queue )
40
70
+ ";key="
41
- + bind_specification .binding_key
71
+ + encode_path_segment ( bind_specification .binding_key )
42
72
+ ";args="
43
73
)
44
74
return binding_path_wth_exchange_queue_key
0 commit comments