|
| 1 | +using Amqp.Types; |
| 2 | + |
| 3 | +namespace RabbitMQ.AMQP.Client.Impl; |
| 4 | + |
| 5 | +public abstract class BindingSpecificationBase |
| 6 | +{ |
| 7 | + protected string Source = ""; |
| 8 | + protected string Destination = ""; |
| 9 | + protected string RoutingKey = ""; |
| 10 | + protected bool ToQueue = true; |
| 11 | + protected Dictionary<string, object> _arguments = new(); |
| 12 | + |
| 13 | + protected Map ArgsToMap() |
| 14 | + { |
| 15 | + Map argMap = new(); |
| 16 | + |
| 17 | + foreach ((string key, object value) in _arguments) |
| 18 | + { |
| 19 | + argMap[key] = value; |
| 20 | + } |
| 21 | + |
| 22 | + return argMap; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +public class AmqpBindingSpecification(AmqpManagement management) : BindingSpecificationBase, IBindingSpecification |
| 27 | +{ |
| 28 | + private AmqpManagement Management { get; } = management; |
| 29 | + |
| 30 | + public async Task Bind() |
| 31 | + { |
| 32 | + var kv = new Map |
| 33 | + { |
| 34 | + { "source", Source }, |
| 35 | + { "binding_key", RoutingKey }, |
| 36 | + { "arguments", ArgsToMap() }, |
| 37 | + { ToQueue ? "destination_queue" : "destination_exchange", Destination } |
| 38 | + }; |
| 39 | + |
| 40 | + await Management.Request(kv, $"/{Consts.Bindings}", |
| 41 | + AmqpManagement.Post, |
| 42 | + [ |
| 43 | + AmqpManagement.Code204, |
| 44 | + ]).ConfigureAwait(false); |
| 45 | + } |
| 46 | + |
| 47 | + public async Task Unbind() |
| 48 | + { |
| 49 | + string destinationCharacter = ToQueue ? "dstq" : "dste"; |
| 50 | + if (_arguments.Count == 0) |
| 51 | + { |
| 52 | + string target = |
| 53 | + $"/{Consts.Bindings}/src={Utils.EncodePathSegment(Source)};" + |
| 54 | + $"{($"{destinationCharacter}={Utils.EncodePathSegment(Destination)};" + |
| 55 | + $"key={Utils.EncodePathSegment(RoutingKey)};args=")}"; |
| 56 | + |
| 57 | + await Management.Request( |
| 58 | + null, target, |
| 59 | + AmqpManagement.Delete, new[] { AmqpManagement.Code204 }).ConfigureAwait(false); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + string path = BindingsTarget(destinationCharacter, Source, Destination, RoutingKey); |
| 64 | + List<Map> bindings = await GetBindings(path).ConfigureAwait(false); |
| 65 | + string? uri = MatchBinding(bindings, RoutingKey, ArgsToMap()); |
| 66 | + if (uri != null) |
| 67 | + { |
| 68 | + await Management.Request( |
| 69 | + null, uri, |
| 70 | + AmqpManagement.Delete, new[] { AmqpManagement.Code204 }).ConfigureAwait(false); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public IBindingSpecification SourceExchange(string exchange) |
| 76 | + { |
| 77 | + ToQueue = false; |
| 78 | + Source = exchange; |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + public IBindingSpecification DestinationQueue(string queue) |
| 83 | + { |
| 84 | + ToQueue = true; |
| 85 | + Destination = queue; |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + public IBindingSpecification DestinationExchange(string exchange) |
| 90 | + { |
| 91 | + Destination = exchange; |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + public IBindingSpecification Key(string key) |
| 96 | + { |
| 97 | + RoutingKey = key; |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + public IBindingSpecification Argument(string key, object value) |
| 102 | + { |
| 103 | + _arguments[key] = value; |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + public IBindingSpecification Arguments(Dictionary<string, object> arguments) |
| 108 | + { |
| 109 | + _arguments = arguments; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + private string BindingsTarget( |
| 114 | + string destinationField, string source, string destination, string key) |
| 115 | + { |
| 116 | + return "/bindings?src=" |
| 117 | + + Utils.EncodeHttpParameter(source) |
| 118 | + + "&" |
| 119 | + + destinationField |
| 120 | + + "=" |
| 121 | + + Utils.EncodeHttpParameter(destination) |
| 122 | + + "&key=" |
| 123 | + + Utils.EncodeHttpParameter(key); |
| 124 | + } |
| 125 | + |
| 126 | + private async Task<List<Map>> GetBindings(string path) |
| 127 | + { |
| 128 | + var result = await Management.Request( |
| 129 | + null, path, |
| 130 | + AmqpManagement.Get, new[] { AmqpManagement.Code200 }).ConfigureAwait(false); |
| 131 | + |
| 132 | + if (result.Body is not List list) |
| 133 | + { |
| 134 | + return []; |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + var l = new List<Map>() { }; |
| 139 | + foreach (object o in list) |
| 140 | + { |
| 141 | + if (o is Map item) |
| 142 | + { |
| 143 | + l.Add(item); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return l; |
| 148 | + } |
| 149 | + |
| 150 | + private string? MatchBinding(List<Map> bindings, string key, Map arguments) |
| 151 | + { |
| 152 | + string? uri = null; |
| 153 | + foreach (Map binding in bindings) |
| 154 | + { |
| 155 | + string bindingKey = (string)binding["binding_key"]; |
| 156 | + Map bindingArguments = (Map)binding["arguments"]; |
| 157 | + if ((key == null && bindingKey == null) || (key != null && key.Equals(bindingKey))) |
| 158 | + { |
| 159 | + if ((arguments == null && bindingArguments == null) || |
| 160 | + (arguments != null && Utils.CompareMap(arguments, bindingArguments))) |
| 161 | + { |
| 162 | + uri = binding["location"].ToString(); |
| 163 | + break; |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return uri; |
| 169 | + } |
| 170 | +} |
0 commit comments