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.11.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.



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

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

  initialize_connection(endpoint, options)
end

Instance Attribute Details

#endpointObject (readonly)

API



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

def endpoint
  @endpoint
end

Class Method Details

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



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

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

Instance Method Details

#aliveness_test(vhost) ⇒ Object



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

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

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



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

def bind_exchange(vhost, destination_exchange, source_exchange, routing_key, arguments = [])
  resp = @connection.post("bindings/#{uri_encode(vhost)}/e/#{uri_encode(source_exchange)}/e/#{uri_encode(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



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

def bind_queue(vhost, queue, exchange, routing_key, arguments = [])
  resp = @connection.post("bindings/#{uri_encode(vhost)}/e/#{uri_encode(exchange)}/q/#{uri_encode(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



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

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

#clear_parameters_of(component, vhost, name) ⇒ Object



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

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

#clear_permissions_of(vhost, user) ⇒ Object



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

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

#clear_policies_of(vhost, name) ⇒ Object



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

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

#close_connection(name) ⇒ Object



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

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

#connection_info(name) ⇒ Object



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

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

#create_vhost(name) ⇒ Object



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

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

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



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

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

  response = @connection.put("exchanges/#{uri_encode(vhost)}/#{uri_encode(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



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

def declare_queue(vhost, name, attributes)
  response = @connection.put("queues/#{uri_encode(vhost)}/#{uri_encode(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



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

def delete_exchange(vhost, name, if_unused = false)
  response = @connection.delete("exchanges/#{uri_encode(vhost)}/#{uri_encode(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



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

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

#delete_queue(vhost, name) ⇒ Object



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

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

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



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

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

#delete_user(name) ⇒ Object



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

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

#delete_vhost(name) ⇒ Object



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

def delete_vhost(name)
  decode_resource(@connection.delete("vhosts/#{uri_encode(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



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

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

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



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

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

#exchange_info(vhost, name) ⇒ Object



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

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

#get_messages(vhost, name, options) ⇒ Object



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

def get_messages(vhost, name, options)
  response = @connection.post("queues/#{uri_encode(vhost)}/#{uri_encode(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) ⇒ Object



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

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

  decode_resource_collection(@connection.get(path))
end

#list_bindings_between_exchanges(vhost, destination_exchange, source_exchange) ⇒ Object



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

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

#list_bindings_between_queue_and_exchange(vhost, queue, exchange) ⇒ Object



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

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

#list_bindings_by_destination(vhost, exchange) ⇒ Object



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

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

#list_bindings_by_source(vhost, exchange) ⇒ Object



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

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

#list_channelsObject



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

def list_channels
  decode_resource_collection(@connection.get("channels"))
end

#list_connectionsObject



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

def list_connections
  decode_resource_collection(@connection.get("connections"))
end

#list_definitionsObject



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

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

#list_exchanges(vhost = nil) ⇒ Object



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

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

  decode_resource_collection(@connection.get(path))
end

#list_extensionsObject



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

def list_extensions
  decode_resource_collection(@connection.get("extensions"))
end

#list_nodesObject



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

def list_nodes
  decode_resource_collection(@connection.get("nodes"))
end

#list_parameters(component = nil) ⇒ Object



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

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

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



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

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

#list_permissions(vhost = nil) ⇒ Object



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

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

  decode_resource_collection(@connection.get(path))
end

#list_permissions_of(vhost, user) ⇒ Object



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

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

#list_policies(vhost = nil) ⇒ Object



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

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

  decode_resource_collection(@connection.get(path))
end

#list_policies_of(vhost, name = nil) ⇒ Object



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

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

#list_queue_bindings(vhost, queue) ⇒ Object



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

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

#list_queues(vhost = nil) ⇒ Object



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

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

  decode_resource_collection(@connection.get(path))
end

#list_usersObject



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

def list_users
  decode_resource_collection(@connection.get("users"))
end

#list_vhostsObject



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

def list_vhosts
  decode_resource_collection(@connection.get("vhosts"))
end

#node_info(name) ⇒ Object



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

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

#overviewObject



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

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



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

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

#purge_queue(vhost, name) ⇒ Object



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

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

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



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

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

#queue_info(vhost, name) ⇒ Object



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

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

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



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

def update_parameters_of(component, vhost, name, attributes)
  response = @connection.put("parameters/#{uri_encode(component)}/#{uri_encode(vhost)}/#{uri_encode(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



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

def update_permissions_of(vhost, user, attributes)
  response = @connection.put("permissions/#{uri_encode(vhost)}/#{uri_encode(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



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

def update_policies_of(vhost, name, attributes)
  response = @connection.put("policies/#{uri_encode(vhost)}/#{uri_encode(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



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

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

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

#upload_definitions(defs) ⇒ Object



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

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



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

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

#user_permissions(name) ⇒ Object



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

def user_permissions(name)
  decode_resource_collection(@connection.get("users/#{uri_encode(name)}/permissions"))
end

#vhost_info(name) ⇒ Object



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

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

#whoamiObject



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

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