Skip to content

Commit 7012030

Browse files
Merge pull request #43 from veeqo/add-query-support-for-collection-requests
Bypass query params to GET requests for collections
2 parents 752781a + 4984bcf commit 7012030

2 files changed

Lines changed: 65 additions & 46 deletions

File tree

lib/rabbitmq/http/client.rb

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ def protocol_ports
5757
reduce(Hash.new) { |acc, lnr| acc[lnr.protocol] = lnr.port; acc }
5858
end
5959

60-
def list_nodes
61-
decode_resource_collection(@connection.get("nodes"))
60+
def list_nodes(query = {})
61+
decode_resource_collection(@connection.get("nodes", query))
6262
end
6363

6464
def node_info(name)
6565
decode_resource(@connection.get("nodes/#{uri_encode(name)}"))
6666
end
6767

68-
def list_extensions
69-
decode_resource_collection(@connection.get("extensions"))
68+
def list_extensions(query = {})
69+
decode_resource_collection(@connection.get("extensions", query))
7070
end
7171

7272
def list_definitions
@@ -81,8 +81,8 @@ def upload_definitions(defs)
8181
response.success?
8282
end
8383

84-
def list_connections
85-
decode_resource_collection(@connection.get("connections"))
84+
def list_connections(query = {})
85+
decode_resource_collection(@connection.get("connections", query))
8686
end
8787

8888
def connection_info(name)
@@ -93,22 +93,22 @@ def close_connection(name)
9393
decode_resource(@connection.delete("connections/#{uri_encode(name)}"))
9494
end
9595

96-
def list_channels
97-
decode_resource_collection(@connection.get("channels"))
96+
def list_channels(query = {})
97+
decode_resource_collection(@connection.get("channels", query))
9898
end
9999

100100
def channel_info(name)
101101
decode_resource(@connection.get("channels/#{uri_encode(name)}"))
102102
end
103103

104-
def list_exchanges(vhost = nil)
104+
def list_exchanges(vhost = nil, query = {})
105105
path = if vhost.nil?
106106
"exchanges"
107107
else
108108
"exchanges/#{uri_encode(vhost)}"
109109
end
110110

111-
decode_resource_collection(@connection.get(path))
111+
decode_resource_collection(@connection.get(path, query))
112112
end
113113

114114
def declare_exchange(vhost, name, attributes = {})
@@ -137,22 +137,22 @@ def exchange_info(vhost, name)
137137
decode_resource(@connection.get("exchanges/#{uri_encode(vhost)}/#{uri_encode(name)}"))
138138
end
139139

140-
def list_bindings_by_source(vhost, exchange)
141-
decode_resource_collection(@connection.get("exchanges/#{uri_encode(vhost)}/#{uri_encode(exchange)}/bindings/source"))
140+
def list_bindings_by_source(vhost, exchange, query = {})
141+
decode_resource_collection(@connection.get("exchanges/#{uri_encode(vhost)}/#{uri_encode(exchange)}/bindings/source", query))
142142
end
143143

144-
def list_bindings_by_destination(vhost, exchange)
145-
decode_resource_collection(@connection.get("exchanges/#{uri_encode(vhost)}/#{uri_encode(exchange)}/bindings/destination"))
144+
def list_bindings_by_destination(vhost, exchange, query = {})
145+
decode_resource_collection(@connection.get("exchanges/#{uri_encode(vhost)}/#{uri_encode(exchange)}/bindings/destination", query))
146146
end
147147

148-
def list_queues(vhost = nil)
148+
def list_queues(vhost = nil, query = {})
149149
path = if vhost.nil?
150150
"queues"
151151
else
152152
"queues/#{uri_encode(vhost)}"
153153
end
154154

155-
decode_resource_collection(@connection.get(path))
155+
decode_resource_collection(@connection.get(path, query))
156156
end
157157

158158
def queue_info(vhost, name)
@@ -171,8 +171,8 @@ def delete_queue(vhost, name)
171171
decode_resource(@connection.delete("queues/#{uri_encode(vhost)}/#{uri_encode(name)}"))
172172
end
173173

174-
def list_queue_bindings(vhost, queue)
175-
decode_resource_collection(@connection.get("queues/#{uri_encode(vhost)}/#{uri_encode(queue)}/bindings"))
174+
def list_queue_bindings(vhost, queue, query = {})
175+
decode_resource_collection(@connection.get("queues/#{uri_encode(vhost)}/#{uri_encode(queue)}/bindings", query))
176176
end
177177

178178
def purge_queue(vhost, name)
@@ -188,18 +188,18 @@ def get_messages(vhost, name, options)
188188
decode_resource_collection(response)
189189
end
190190

191-
def list_bindings(vhost = nil)
191+
def list_bindings(vhost = nil, query = {})
192192
path = if vhost.nil?
193193
"bindings"
194194
else
195195
"bindings/#{uri_encode(vhost)}"
196196
end
197197

198-
decode_resource_collection(@connection.get(path))
198+
decode_resource_collection(@connection.get(path, query))
199199
end
200200

201-
def list_bindings_between_queue_and_exchange(vhost, queue, exchange)
202-
decode_resource_collection(@connection.get("bindings/#{uri_encode(vhost)}/e/#{uri_encode(exchange)}/q/#{uri_encode(queue)}"))
201+
def list_bindings_between_queue_and_exchange(vhost, queue, exchange, query = {})
202+
decode_resource_collection(@connection.get("bindings/#{uri_encode(vhost)}/e/#{uri_encode(exchange)}/q/#{uri_encode(queue)}", query))
203203
end
204204

205205
def queue_binding_info(vhost, queue, exchange, properties_key)
@@ -219,8 +219,8 @@ def delete_queue_binding(vhost, queue, exchange, properties_key)
219219
resp.success?
220220
end
221221

222-
def list_bindings_between_exchanges(vhost, destination_exchange, source_exchange)
223-
decode_resource_collection(@connection.get("bindings/#{uri_encode(vhost)}/e/#{uri_encode(source_exchange)}/e/#{uri_encode(destination_exchange)}"))
222+
def list_bindings_between_exchanges(vhost, destination_exchange, source_exchange, query = {})
223+
decode_resource_collection(@connection.get("bindings/#{uri_encode(vhost)}/e/#{uri_encode(source_exchange)}/e/#{uri_encode(destination_exchange)}", query))
224224
end
225225

226226
def exchange_binding_info(vhost, destination_exchange, source_exchange, properties_key)
@@ -242,8 +242,8 @@ def delete_exchange_binding(vhost, destination_exchange, source_exchange, proper
242242
end
243243

244244

245-
def list_vhosts
246-
decode_resource_collection(@connection.get("vhosts"))
245+
def list_vhosts(query = {})
246+
decode_resource_collection(@connection.get("vhosts", query))
247247
end
248248

249249
def vhost_info(name)
@@ -263,14 +263,14 @@ def delete_vhost(name)
263263

264264

265265

266-
def list_permissions(vhost = nil)
266+
def list_permissions(vhost = nil, query = {})
267267
path = if vhost
268268
"vhosts/#{uri_encode(vhost)}/permissions"
269269
else
270270
"permissions"
271271
end
272272

273-
decode_resource_collection(@connection.get(path))
273+
decode_resource_collection(@connection.get(path, query))
274274
end
275275

276276
def list_permissions_of(vhost, user)
@@ -291,8 +291,8 @@ def clear_permissions_of(vhost, user)
291291

292292

293293

294-
def list_users
295-
decode_resource_collection(@connection.get("users"))
294+
def list_users(query = {})
295+
decode_resource_collection(@connection.get("users", query))
296296
end
297297

298298
def user_info(name)
@@ -314,8 +314,8 @@ def delete_user(name)
314314
decode_resource(@connection.delete("users/#{uri_encode(name)}"))
315315
end
316316

317-
def user_permissions(name)
318-
decode_resource_collection(@connection.get("users/#{uri_encode(name)}/permissions"))
317+
def user_permissions(name, query = {})
318+
decode_resource_collection(@connection.get("users/#{uri_encode(name)}/permissions", query))
319319
end
320320

321321
def whoami
@@ -324,23 +324,23 @@ def whoami
324324

325325

326326

327-
def list_policies(vhost = nil)
327+
def list_policies(vhost = nil, query = {})
328328
path = if vhost
329329
"policies/#{uri_encode(vhost)}"
330330
else
331331
"policies"
332332
end
333333

334-
decode_resource_collection(@connection.get(path))
334+
decode_resource_collection(@connection.get(path, query))
335335
end
336336

337-
def list_policies_of(vhost, name = nil)
337+
def list_policies_of(vhost, name = nil, query = {})
338338
path = if name
339339
"policies/#{uri_encode(vhost)}/#{uri_encode(name)}"
340340
else
341341
"policies/#{uri_encode(vhost)}"
342342
end
343-
decode_resource_collection(@connection.get(path))
343+
decode_resource_collection(@connection.get(path, query))
344344
end
345345

346346
def update_policies_of(vhost, name, attributes)
@@ -358,22 +358,22 @@ def clear_policies_of(vhost, name)
358358

359359

360360

361-
def list_parameters(component = nil)
361+
def list_parameters(component = nil, query = {})
362362
path = if component
363363
"parameters/#{uri_encode(component)}"
364364
else
365365
"parameters"
366366
end
367-
decode_resource_collection(@connection.get(path))
367+
decode_resource_collection(@connection.get(path, query))
368368
end
369369

370-
def list_parameters_of(component, vhost, name = nil)
370+
def list_parameters_of(component, vhost, name = nil, query = {})
371371
path = if name
372372
"parameters/#{uri_encode(component)}/#{uri_encode(vhost)}/#{uri_encode(name)}"
373373
else
374374
"parameters/#{uri_encode(component)}/#{uri_encode(vhost)}"
375375
end
376-
decode_resource_collection(@connection.get(path))
376+
decode_resource_collection(@connection.get(path, query))
377377
end
378378

379379
def update_parameters_of(component, vhost, name, attributes)
@@ -436,7 +436,9 @@ def decode_resource(response)
436436
end
437437

438438
def decode_resource_collection(response)
439-
response.body.map { |i| Hashie::Mash.new(i) }
439+
collection = response.body.is_a?(Array) ? response.body : response.body.fetch('items')
440+
441+
collection.map { |i| Hashie::Mash.new(i) }
440442
end
441443
end # Client
442444
end # HTTP

spec/integration/client_spec.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,30 @@ def await_event_propagation
399399
@channel.close
400400
end
401401

402-
it "returns a list of all queues" do
403-
q = @channel.queue("", durable: false)
402+
context "when no params given" do
403+
it "returns a list of all queues" do
404+
q1 = @channel.queue("", durable: false)
405+
q2 = @channel.queue("", durable: false)
404406

405-
xs = subject.list_queues
406-
expect(xs.detect { |x| x.name == q.name }).to_not be_empty
407+
xs = subject.list_queues
408+
expect(xs.select { |x| [q1.name, q2.name].include?(x.name) }.count).to eq 2
407409

408-
subject.delete_queue("/", q.name)
410+
subject.delete_queue("/", q1.name)
411+
subject.delete_queue("/", q2.name)
412+
end
413+
end
414+
415+
context "when pagination params given" do
416+
it "returns a paginated list of queues" do
417+
q1 = @channel.queue("", durable: false)
418+
q2 = @channel.queue("", durable: false)
419+
420+
xs = subject.list_queues(nil, page: 1, page_size: 1)
421+
expect(xs.count).to eq 1
422+
423+
subject.delete_queue("/", q1.name)
424+
subject.delete_queue("/", q2.name)
425+
end
409426
end
410427
end
411428

0 commit comments

Comments
 (0)