Class: RabbitMQ::HTTP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rabbitmq/http/client.rb,
lib/rabbitmq/http/client/version.rb

Constant Summary collapse

VERSION =
"2.2.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, options = {}) ⇒ Client

Returns a new instance of Client.



27
28
29
30
31
32
33
34
35
36
# File 'lib/rabbitmq/http/client.rb', line 27

def initialize(endpoint, options = {})
  @endpoint = endpoint
  @options  = options

  @request_helper = RequestHelper.new()
  @response_helper = ResponseHelper.new(self)
  @health = HealthChecks.new(self)

  initialize_connection(endpoint, options)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



21
22
23
# File 'lib/rabbitmq/http/client.rb', line 21

def connection
  @connection
end

#endpointObject (readonly)

API



20
21
22
# File 'lib/rabbitmq/http/client.rb', line 20

def endpoint
  @endpoint
end

#healthObject (readonly)

API



20
21
22
# File 'lib/rabbitmq/http/client.rb', line 20

def health
  @health
end

#request_helperObject (readonly)

Returns the value of attribute request_helper.



21
22
23
# File 'lib/rabbitmq/http/client.rb', line 21

def request_helper
  @request_helper
end

#response_helperObject (readonly)

Returns the value of attribute response_helper.



21
22
23
# File 'lib/rabbitmq/http/client.rb', line 21

def response_helper
  @response_helper
end

Class Method Details

.connect(endpoint, options = {}) ⇒ Object



23
24
25
# File 'lib/rabbitmq/http/client.rb', line 23

def self.connect(endpoint, options = {})
  new(endpoint, options)
end

Instance Method Details

#bind_exchange(vhost, destination_exchange, source_exchange, routing_key, arguments = []) ⇒ Object



241
242
243
244
245
246
247
# File 'lib/rabbitmq/http/client.rb', line 241

def bind_exchange(vhost, destination_exchange, source_exchange, routing_key, arguments = [])
  resp = @connection.post("bindings/#{encode_uri_path_segment(vhost)}/e/#{encode_uri_path_segment(source_exchange)}/e/#{encode_uri_path_segment(destination_exchange)}") do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = MultiJson.dump({:routing_key => routing_key, :arguments => arguments})
  end
  resp.headers['location']
end

#bind_queue(vhost, queue, exchange, routing_key, arguments = []) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/rabbitmq/http/client.rb', line 219

def bind_queue(vhost, queue, exchange, routing_key, arguments = [])
  resp = @connection.post("bindings/#{encode_uri_path_segment(vhost)}/e/#{encode_uri_path_segment(exchange)}/q/#{encode_uri_path_segment(queue)}") do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = MultiJson.dump({:routing_key => routing_key, :arguments => arguments})
  end
  resp.headers['location']
end

#channel_info(name) ⇒ Object



110
111
112
# File 'lib/rabbitmq/http/client.rb', line 110

def channel_info(name)
  decode_resource(@connection.get("channels/#{encode_uri_path_segment(name)}"))
end

#clear_parameters_of(component, vhost, name) ⇒ Object



434
435
436
# File 'lib/rabbitmq/http/client.rb', line 434

def clear_parameters_of(component, vhost, name)
  decode_resource(@connection.delete("parameters/#{encode_uri_path_segment(component)}/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}"))
end

#clear_permissions_of(vhost, user) ⇒ Object



298
299
300
# File 'lib/rabbitmq/http/client.rb', line 298

def clear_permissions_of(vhost, user)
  decode_resource(@connection.delete("permissions/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(user)}"))
end

#clear_policies_of(vhost, name) ⇒ Object



401
402
403
# File 'lib/rabbitmq/http/client.rb', line 401

def clear_policies_of(vhost, name)
  decode_resource(@connection.delete("policies/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}"))
end

#close_connection(name) ⇒ Object



102
103
104
# File 'lib/rabbitmq/http/client.rb', line 102

def close_connection(name)
  decode_resource(@connection.delete("connections/#{encode_uri_path_segment(name)}"))
end

#connection_info(name) ⇒ Object



98
99
100
# File 'lib/rabbitmq/http/client.rb', line 98

def connection_info(name)
  decode_resource(@connection.get("connections/#{encode_uri_path_segment(name)}"))
end

#create_vhost(name) ⇒ Object



263
264
265
266
267
268
# File 'lib/rabbitmq/http/client.rb', line 263

def create_vhost(name)
  response = @connection.put("vhosts/#{encode_uri_path_segment(name)}") do |req|
    req.headers['Content-Type'] = "application/json"
  end
  decode_resource(response)
end

#declare_exchange(vhost, name, attributes = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rabbitmq/http/client.rb', line 124

def declare_exchange(vhost, name, attributes = {})
  opts = {
    type: "direct",
    auto_delete: false,
    durable: true,
    arguments: {}
  }.merge(attributes)

  response = @connection.put("exchanges/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}") do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = MultiJson.dump(opts)
  end
  decode_resource(response)
end

#declare_queue(vhost, name, attributes) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/rabbitmq/http/client.rb', line 172

def declare_queue(vhost, name, attributes)
  response = @connection.put("queues/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = MultiJson.dump(attributes)
  end
  decode_resource(response)
end

#delete_exchange(vhost, name, if_unused = false) ⇒ Object



139
140
141
142
143
144
# File 'lib/rabbitmq/http/client.rb', line 139

def delete_exchange(vhost, name, if_unused = false)
  response = @connection.delete("exchanges/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}") do |req|
    req.params["if-unused"] = true if if_unused
  end
  decode_resource(response)
end

#delete_exchange_binding(vhost, destination_exchange, source_exchange, properties_key) ⇒ Object



249
250
251
252
# File 'lib/rabbitmq/http/client.rb', line 249

def delete_exchange_binding(vhost, destination_exchange, source_exchange, properties_key)
  resp = @connection.delete("bindings/#{encode_uri_path_segment(vhost)}/e/#{encode_uri_path_segment(source_exchange)}/e/#{encode_uri_path_segment(destination_exchange)}/#{encode_uri_path_segment(properties_key)}")
  resp.success?
end

#delete_queue(vhost, name) ⇒ Object



180
181
182
# File 'lib/rabbitmq/http/client.rb', line 180

def delete_queue(vhost, name)
  decode_resource(@connection.delete("queues/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}"))
end

#delete_queue_binding(vhost, queue, exchange, properties_key) ⇒ Object



227
228
229
230
# File 'lib/rabbitmq/http/client.rb', line 227

def delete_queue_binding(vhost, queue, exchange, properties_key)
  resp = @connection.delete("bindings/#{encode_uri_path_segment(vhost)}/e/#{encode_uri_path_segment(exchange)}/q/#{encode_uri_path_segment(queue)}/#{encode_uri_path_segment(properties_key)}")
  resp.success?
end

#delete_topic_permissions_of(vhost, user) ⇒ Object



326
327
328
# File 'lib/rabbitmq/http/client.rb', line 326

def delete_topic_permissions_of(vhost, user)
  decode_resource(@connection.delete("topic-permissions/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(user)}"))
end

#delete_user(name) ⇒ Object



360
361
362
# File 'lib/rabbitmq/http/client.rb', line 360

def delete_user(name)
  decode_resource(@connection.delete("users/#{encode_uri_path_segment(name)}"))
end

#delete_vhost(name) ⇒ Object



270
271
272
# File 'lib/rabbitmq/http/client.rb', line 270

def delete_vhost(name)
  decode_resource(@connection.delete("vhosts/#{encode_uri_path_segment(name)}"))
end

#enabled_protocolsArray<String>

Returns a list of messaging protocols supported by the node (or cluster).

Common values are:

  • amqp

  • amqp/ssl

  • mqtt

  • stomp

The exact value depends on RabbitMQ configuration and enabled plugins.

Returns:

  • (Array<String>)

    Enabled protocols



56
57
58
59
60
# File 'lib/rabbitmq/http/client.rb', line 56

def enabled_protocols
  self.overview.listeners.
    map { |lnr| lnr.protocol }.
    uniq
end

#exchange_binding_info(vhost, destination_exchange, source_exchange, properties_key) ⇒ Object



236
237
238
# File 'lib/rabbitmq/http/client.rb', line 236

def exchange_binding_info(vhost, destination_exchange, source_exchange, properties_key)
  decode_resource(@connection.get("bindings/#{encode_uri_path_segment(vhost)}/e/#{encode_uri_path_segment(source_exchange)}/e/#{encode_uri_path_segment(destination_exchange)}/#{encode_uri_path_segment(properties_key)}"))
end

#exchange_info(vhost, name) ⇒ Object



146
147
148
# File 'lib/rabbitmq/http/client.rb', line 146

def exchange_info(vhost, name)
  decode_resource(@connection.get("exchanges/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}"))
end

#get_messages(vhost, name, options) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/rabbitmq/http/client.rb', line 193

def get_messages(vhost, name, options)
  response = @connection.post("queues/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}/get") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = MultiJson.dump(options)
  end
  decode_resource_collection(response)
end

#list_bindings(vhost = nil, query = {}) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/rabbitmq/http/client.rb', line 201

def list_bindings(vhost = nil, query = {})
  path = if vhost.nil?
           "bindings"
         else
           "bindings/#{encode_uri_path_segment(vhost)}"
         end

  decode_resource_collection(@connection.get(path, query))
end

#list_bindings_between_exchanges(vhost, destination_exchange, source_exchange, query = {}) ⇒ Object



232
233
234
# File 'lib/rabbitmq/http/client.rb', line 232

def list_bindings_between_exchanges(vhost, destination_exchange, source_exchange, query = {})
  decode_resource_collection(@connection.get("bindings/#{encode_uri_path_segment(vhost)}/e/#{encode_uri_path_segment(source_exchange)}/e/#{encode_uri_path_segment(destination_exchange)}", query))
end

#list_bindings_between_queue_and_exchange(vhost, queue, exchange, query = {}) ⇒ Object



211
212
213
# File 'lib/rabbitmq/http/client.rb', line 211

def list_bindings_between_queue_and_exchange(vhost, queue, exchange, query = {})
  decode_resource_collection(@connection.get("bindings/#{encode_uri_path_segment(vhost)}/e/#{encode_uri_path_segment(exchange)}/q/#{encode_uri_path_segment(queue)}", query))
end

#list_bindings_by_destination(vhost, exchange, query = {}) ⇒ Object



154
155
156
# File 'lib/rabbitmq/http/client.rb', line 154

def list_bindings_by_destination(vhost, exchange, query = {})
  decode_resource_collection(@connection.get("exchanges/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(exchange)}/bindings/destination", query))
end

#list_bindings_by_source(vhost, exchange, query = {}) ⇒ Object



150
151
152
# File 'lib/rabbitmq/http/client.rb', line 150

def list_bindings_by_source(vhost, exchange, query = {})
  decode_resource_collection(@connection.get("exchanges/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(exchange)}/bindings/source", query))
end

#list_channels(query = {}) ⇒ Object



106
107
108
# File 'lib/rabbitmq/http/client.rb', line 106

def list_channels(query = {})
  decode_resource_collection(@connection.get("channels", query))
end

#list_connections(query = {}) ⇒ Object



94
95
96
# File 'lib/rabbitmq/http/client.rb', line 94

def list_connections(query = {})
  decode_resource_collection(@connection.get("connections", query))
end

#list_definitionsObject



82
83
84
# File 'lib/rabbitmq/http/client.rb', line 82

def list_definitions
  decode_resource(@connection.get("definitions"))
end

#list_exchanges(vhost = nil, query = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/rabbitmq/http/client.rb', line 114

def list_exchanges(vhost = nil, query = {})
  path = if vhost.nil?
           "exchanges"
         else
           "exchanges/#{encode_uri_path_segment(vhost)}"
         end

  decode_resource_collection(@connection.get(path, query))
end

#list_extensions(query = {}) ⇒ Object



78
79
80
# File 'lib/rabbitmq/http/client.rb', line 78

def list_extensions(query = {})
  decode_resource_collection(@connection.get("extensions", query))
end

#list_nodes(query = {}) ⇒ Object



70
71
72
# File 'lib/rabbitmq/http/client.rb', line 70

def list_nodes(query = {})
  decode_resource_collection(@connection.get("nodes", query))
end

#list_parameters(component = nil, query = {}) ⇒ Object



408
409
410
411
412
413
414
415
# File 'lib/rabbitmq/http/client.rb', line 408

def list_parameters(component = nil, query = {})
  path = if component
           "parameters/#{encode_uri_path_segment(component)}"
         else
           "parameters"
         end
  decode_resource_collection(@connection.get(path, query))
end

#list_parameters_of(component, vhost, name = nil, query = {}) ⇒ Object



417
418
419
420
421
422
423
424
# File 'lib/rabbitmq/http/client.rb', line 417

def list_parameters_of(component, vhost, name = nil, query = {})
  path = if name
           "parameters/#{encode_uri_path_segment(component)}/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}"
         else
           "parameters/#{encode_uri_path_segment(component)}/#{encode_uri_path_segment(vhost)}"
         end
  decode_resource_collection(@connection.get(path, query))
end

#list_permissions(vhost = nil, query = {}) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/rabbitmq/http/client.rb', line 276

def list_permissions(vhost = nil, query = {})
  path = if vhost
           "vhosts/#{encode_uri_path_segment(vhost)}/permissions"
         else
           "permissions"
         end

  decode_resource_collection(@connection.get(path, query))
end

#list_permissions_of(vhost, user) ⇒ Object



286
287
288
# File 'lib/rabbitmq/http/client.rb', line 286

def list_permissions_of(vhost, user)
  decode_resource(@connection.get("permissions/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(user)}"))
end

#list_policies(vhost = nil, query = {}) ⇒ Object



374
375
376
377
378
379
380
381
382
# File 'lib/rabbitmq/http/client.rb', line 374

def list_policies(vhost = nil, query = {})
  path = if vhost
           "policies/#{encode_uri_path_segment(vhost)}"
         else
           "policies"
         end

  decode_resource_collection(@connection.get(path, query))
end

#list_policies_of(vhost, name = nil, query = {}) ⇒ Object



384
385
386
387
388
389
390
391
# File 'lib/rabbitmq/http/client.rb', line 384

def list_policies_of(vhost, name = nil, query = {})
  path = if name
           "policies/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}"
         else
           "policies/#{encode_uri_path_segment(vhost)}"
         end
  decode_resource_collection(@connection.get(path, query))
end

#list_queue_bindings(vhost, queue, query = {}) ⇒ Object



184
185
186
# File 'lib/rabbitmq/http/client.rb', line 184

def list_queue_bindings(vhost, queue, query = {})
  decode_resource_collection(@connection.get("queues/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(queue)}/bindings", query))
end

#list_queues(vhost = nil, query = {}) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/rabbitmq/http/client.rb', line 158

def list_queues(vhost = nil, query = {})
  path = if vhost.nil?
           "queues"
         else
           "queues/#{encode_uri_path_segment(vhost)}"
         end

  decode_resource_collection(@connection.get(path, query))
end

#list_topic_permissions(vhost = nil, query = {}) ⇒ Object



302
303
304
305
306
307
308
309
310
# File 'lib/rabbitmq/http/client.rb', line 302

def list_topic_permissions(vhost = nil, query = {})
  path = if vhost
           "vhosts/#{encode_uri_path_segment(vhost)}/topic-permissions"
          else
            "topic-permissions"
          end

  decode_resource_collection(@connection.get(path, query))
end

#list_topic_permissions_of(vhost, user) ⇒ Object



312
313
314
315
# File 'lib/rabbitmq/http/client.rb', line 312

def list_topic_permissions_of(vhost, user)
  path = "topic-permissions/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(user)}"
  decode_resource_collection(@connection.get(path))
end

#list_users(query = {}) ⇒ Object



330
331
332
333
334
335
336
337
338
# File 'lib/rabbitmq/http/client.rb', line 330

def list_users(query = {})
  results = decode_resource_collection(@connection.get("users", query))

  # HTTP API will return tags as an array starting with RabbitMQ 3.9
  results.map do |u|
    u.tags = u.tags.split(",") if u.tags.is_a?(String)
    u
  end
end

#list_vhosts(query = {}) ⇒ Object



255
256
257
# File 'lib/rabbitmq/http/client.rb', line 255

def list_vhosts(query = {})
  decode_resource_collection(@connection.get("vhosts", query))
end

#node_info(name) ⇒ Object



74
75
76
# File 'lib/rabbitmq/http/client.rb', line 74

def node_info(name)
  decode_resource(@connection.get("nodes/#{encode_uri_path_segment(name)}"))
end

#overviewObject



38
39
40
# File 'lib/rabbitmq/http/client.rb', line 38

def overview
  decode_resource(@connection.get("overview"))
end

#protocol_portsHash<String, Integer>

Returns a hash of protocol => port.

Returns:

  • (Hash<String, Integer>)

    Hash of protocol => port



65
66
67
68
# File 'lib/rabbitmq/http/client.rb', line 65

def protocol_ports
  (self.overview.listeners || []).
    reduce(Hash.new) { |acc, lnr| acc[lnr.protocol] = lnr.port; acc }
end

#purge_queue(vhost, name) ⇒ Object



188
189
190
191
# File 'lib/rabbitmq/http/client.rb', line 188

def purge_queue(vhost, name)
  @connection.delete("queues/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}/contents")
  Hashie::Mash.new
end

#queue_binding_info(vhost, queue, exchange, properties_key) ⇒ Object



215
216
217
# File 'lib/rabbitmq/http/client.rb', line 215

def queue_binding_info(vhost, queue, exchange, properties_key)
  decode_resource(@connection.get("bindings/#{encode_uri_path_segment(vhost)}/e/#{encode_uri_path_segment(exchange)}/q/#{encode_uri_path_segment(queue)}/#{encode_uri_path_segment(properties_key)}"))
end

#queue_info(vhost, name) ⇒ Object



168
169
170
# File 'lib/rabbitmq/http/client.rb', line 168

def queue_info(vhost, name)
  decode_resource(@connection.get("queues/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}"))
end

#update_parameters_of(component, vhost, name, attributes) ⇒ Object



426
427
428
429
430
431
432
# File 'lib/rabbitmq/http/client.rb', line 426

def update_parameters_of(component, vhost, name, attributes)
  response = @connection.put("parameters/#{encode_uri_path_segment(component)}/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = MultiJson.dump(attributes)
  end
  decode_resource(response)
end

#update_permissions_of(vhost, user, attributes) ⇒ Object



290
291
292
293
294
295
296
# File 'lib/rabbitmq/http/client.rb', line 290

def update_permissions_of(vhost, user, attributes)
  response = @connection.put("permissions/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(user)}") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = MultiJson.dump(attributes)
  end
  decode_resource(response)
end

#update_policies_of(vhost, name, attributes) ⇒ Object



393
394
395
396
397
398
399
# File 'lib/rabbitmq/http/client.rb', line 393

def update_policies_of(vhost, name, attributes)
  response = @connection.put("policies/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(name)}") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = MultiJson.dump(attributes)
  end
  decode_resource(response)
end

#update_topic_permissions_of(vhost, user, attributes) ⇒ Object



317
318
319
320
321
322
323
324
# File 'lib/rabbitmq/http/client.rb', line 317

def update_topic_permissions_of(vhost, user, attributes)
  response = @connection.put("topic-permissions/#{encode_uri_path_segment(vhost)}/#{encode_uri_path_segment(user)}") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = MultiJson.dump(attributes)
  end

  nil
end

#update_user(name, attributes) ⇒ Object Also known as: create_user



349
350
351
352
353
354
355
356
357
# File 'lib/rabbitmq/http/client.rb', line 349

def update_user(name, attributes)
  attributes[:tags] ||= ""

  response = @connection.put("users/#{encode_uri_path_segment(name)}") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = MultiJson.dump(attributes)
  end
  decode_resource(response)
end

#upload_definitions(defs) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/rabbitmq/http/client.rb', line 86

def upload_definitions(defs)
  response = @connection.post("definitions") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = defs
  end
  response.success?
end

#user_info(name) ⇒ Object



340
341
342
343
344
345
346
347
# File 'lib/rabbitmq/http/client.rb', line 340

def (name)
  result = decode_resource(@connection.get("users/#{encode_uri_path_segment(name)}"))

  # HTTP API will return tags as an array starting with RabbitMQ 3.9
  result.tags = result.tags.split(",") if result.tags.is_a?(String)

  result
end

#user_permissions(name, query = {}) ⇒ Object



364
365
366
# File 'lib/rabbitmq/http/client.rb', line 364

def user_permissions(name, query = {})
  decode_resource_collection(@connection.get("users/#{encode_uri_path_segment(name)}/permissions", query))
end

#vhost_info(name) ⇒ Object



259
260
261
# File 'lib/rabbitmq/http/client.rb', line 259

def vhost_info(name)
  decode_resource(@connection.get("vhosts/#{encode_uri_path_segment(name)}"))
end

#whoamiObject



368
369
370
# File 'lib/rabbitmq/http/client.rb', line 368

def whoami
  decode_resource(@connection.get("whoami"))
end