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 =
"1.15.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.



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

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

  initialize_connection(endpoint, options)
end

Instance Attribute Details

#endpointObject (readonly)

API



16
17
18
# File 'lib/rabbitmq/http/client.rb', line 16

def endpoint
  @endpoint
end

Class Method Details

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



18
19
20
# File 'lib/rabbitmq/http/client.rb', line 18

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

Instance Method Details

#aliveness_test(vhost) ⇒ Object



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

def aliveness_test(vhost)
  r = @connection.get("aliveness-test/#{encode_uri_path_segment(vhost)}")
  r.body["status"] == "ok"
end

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



232
233
234
235
236
237
238
# File 'lib/rabbitmq/http/client.rb', line 232

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



210
211
212
213
214
215
216
# File 'lib/rabbitmq/http/client.rb', line 210

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



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

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

#clear_parameters_of(component, vhost, name) ⇒ Object



388
389
390
# File 'lib/rabbitmq/http/client.rb', line 388

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



289
290
291
# File 'lib/rabbitmq/http/client.rb', line 289

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



355
356
357
# File 'lib/rabbitmq/http/client.rb', line 355

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



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

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

#connection_info(name) ⇒ Object



89
90
91
# File 'lib/rabbitmq/http/client.rb', line 89

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

#create_vhost(name) ⇒ Object



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

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rabbitmq/http/client.rb', line 115

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



163
164
165
166
167
168
169
# File 'lib/rabbitmq/http/client.rb', line 163

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



130
131
132
133
134
135
# File 'lib/rabbitmq/http/client.rb', line 130

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



240
241
242
243
# File 'lib/rabbitmq/http/client.rb', line 240

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



171
172
173
# File 'lib/rabbitmq/http/client.rb', line 171

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



218
219
220
221
# File 'lib/rabbitmq/http/client.rb', line 218

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_user(name) ⇒ Object



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

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

#delete_vhost(name) ⇒ Object



261
262
263
# File 'lib/rabbitmq/http/client.rb', line 261

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



47
48
49
50
51
# File 'lib/rabbitmq/http/client.rb', line 47

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

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



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

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



137
138
139
# File 'lib/rabbitmq/http/client.rb', line 137

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



184
185
186
187
188
189
190
# File 'lib/rabbitmq/http/client.rb', line 184

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



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

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



223
224
225
# File 'lib/rabbitmq/http/client.rb', line 223

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



202
203
204
# File 'lib/rabbitmq/http/client.rb', line 202

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



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

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



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

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



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

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

#list_connections(query = {}) ⇒ Object



85
86
87
# File 'lib/rabbitmq/http/client.rb', line 85

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

#list_definitionsObject



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

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

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



105
106
107
108
109
110
111
112
113
# File 'lib/rabbitmq/http/client.rb', line 105

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



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

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

#list_nodes(query = {}) ⇒ Object



61
62
63
# File 'lib/rabbitmq/http/client.rb', line 61

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

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



362
363
364
365
366
367
368
369
# File 'lib/rabbitmq/http/client.rb', line 362

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



371
372
373
374
375
376
377
378
# File 'lib/rabbitmq/http/client.rb', line 371

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



267
268
269
270
271
272
273
274
275
# File 'lib/rabbitmq/http/client.rb', line 267

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



277
278
279
# File 'lib/rabbitmq/http/client.rb', line 277

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



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

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



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

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



175
176
177
# File 'lib/rabbitmq/http/client.rb', line 175

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



149
150
151
152
153
154
155
156
157
# File 'lib/rabbitmq/http/client.rb', line 149

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_users(query = {}) ⇒ Object



295
296
297
# File 'lib/rabbitmq/http/client.rb', line 295

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

#list_vhosts(query = {}) ⇒ Object



246
247
248
# File 'lib/rabbitmq/http/client.rb', line 246

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

#node_info(name) ⇒ Object



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

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

#overviewObject



29
30
31
# File 'lib/rabbitmq/http/client.rb', line 29

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



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

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

#purge_queue(vhost, name) ⇒ Object



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

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



206
207
208
# File 'lib/rabbitmq/http/client.rb', line 206

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



159
160
161
# File 'lib/rabbitmq/http/client.rb', line 159

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



380
381
382
383
384
385
386
# File 'lib/rabbitmq/http/client.rb', line 380

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



281
282
283
284
285
286
287
# File 'lib/rabbitmq/http/client.rb', line 281

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



347
348
349
350
351
352
353
# File 'lib/rabbitmq/http/client.rb', line 347

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_user(name, attributes) ⇒ Object Also known as: create_user



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

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



77
78
79
80
81
82
83
# File 'lib/rabbitmq/http/client.rb', line 77

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



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

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

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



318
319
320
# File 'lib/rabbitmq/http/client.rb', line 318

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

#vhost_info(name) ⇒ Object



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

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

#whoamiObject



322
323
324
# File 'lib/rabbitmq/http/client.rb', line 322

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