|
| 1 | +from .entities import BindingSpecification |
| 2 | + |
| 3 | + |
| 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 | + |
| 24 | +def exchange_address(exchange_name: str, routing_key: str = "") -> str: |
| 25 | + if routing_key == "": |
| 26 | + path = "/exchanges/" + encode_path_segment(exchange_name) |
| 27 | + else: |
| 28 | + path = ( |
| 29 | + "/exchanges/" |
| 30 | + + encode_path_segment(exchange_name) |
| 31 | + + "/" |
| 32 | + + encode_path_segment(routing_key) |
| 33 | + ) |
| 34 | + |
| 35 | + return path |
| 36 | + |
| 37 | + |
| 38 | +def queue_address(queue_name: str) -> str: |
| 39 | + path = "/queues/" + encode_path_segment(queue_name) |
| 40 | + |
| 41 | + return path |
| 42 | + |
| 43 | + |
| 44 | +def purge_queue_address(queue_name: str) -> str: |
| 45 | + path = "/queues/" + encode_path_segment(queue_name) + "/messages" |
| 46 | + |
| 47 | + return path |
| 48 | + |
| 49 | + |
| 50 | +def path_address() -> str: |
| 51 | + path = "/bindings" |
| 52 | + |
| 53 | + return path |
| 54 | + |
| 55 | + |
| 56 | +def binding_path_with_exchange_queue(bind_specification: BindingSpecification) -> str: |
| 57 | + binding_path_wth_exchange_queue_key = ( |
| 58 | + "/bindings" |
| 59 | + + "/" |
| 60 | + + "src=" |
| 61 | + + encode_path_segment(bind_specification.source_exchange) |
| 62 | + + ";" |
| 63 | + + "dstq=" |
| 64 | + + encode_path_segment(bind_specification.destination_queue) |
| 65 | + + ";key=" |
| 66 | + + encode_path_segment(bind_specification.binding_key) |
| 67 | + + ";args=" |
| 68 | + ) |
| 69 | + return binding_path_wth_exchange_queue_key |
0 commit comments