class RabbitMQ::HTTP::Client

Constants

VERSION

Attributes

connection[R]
endpoint[R]

API

health[R]

API

request_helper[R]
response_helper[R]

Public Class Methods

connect(endpoint, options = {}) click to toggle source
# File lib/rabbitmq/http/client.rb, line 23
def self.connect(endpoint, options = {})
  new(endpoint, options)
end
new(endpoint, options = {}) click to toggle source
# 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

Public Instance Methods

bind_exchange(vhost, destination_exchange, source_exchange, routing_key, arguments = []) click to toggle source
# 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 = []) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/rabbitmq/http/client.rb, line 408
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) click to toggle source
# 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) click to toggle source
# File lib/rabbitmq/http/client.rb, line 375
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) click to toggle source
# 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) click to toggle source
# File lib/rabbitmq/http/client.rb, line 98
def connection_info(name)
  decode_resource(@connection.get("connections/#{encode_uri_path_segment(name)}"))
end
create_user(name, attributes)
Alias for: update_user
create_vhost(name) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_user(name) click to toggle source
# File lib/rabbitmq/http/client.rb, line 334
def delete_user(name)
  decode_resource(@connection.delete("users/#{encode_uri_path_segment(name)}"))
end
delete_vhost(name) click to toggle source
# File lib/rabbitmq/http/client.rb, line 270
def delete_vhost(name)
  decode_resource(@connection.delete("vhosts/#{encode_uri_path_segment(name)}"))
end
enabled_protocols() click to toggle source

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.

@return [Array<String>] Enabled protocols

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/rabbitmq/http/client.rb, line 106
def list_channels(query = {})
  decode_resource_collection(@connection.get("channels", query))
end
list_connections(query = {}) click to toggle source
# File lib/rabbitmq/http/client.rb, line 94
def list_connections(query = {})
  decode_resource_collection(@connection.get("connections", query))
end
list_definitions() click to toggle source
# File lib/rabbitmq/http/client.rb, line 82
def list_definitions
  decode_resource(@connection.get("definitions"))
end
list_exchanges(vhost = nil, query = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/rabbitmq/http/client.rb, line 78
def list_extensions(query = {})
  decode_resource_collection(@connection.get("extensions", query))
end
list_nodes(query = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/rabbitmq/http/client.rb, line 382
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 = {}) click to toggle source
# File lib/rabbitmq/http/client.rb, line 391
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 = {}) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# File lib/rabbitmq/http/client.rb, line 348
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 = {}) click to toggle source
# File lib/rabbitmq/http/client.rb, line 358
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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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_users(query = {}) click to toggle source
# File lib/rabbitmq/http/client.rb, line 304
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 = {}) click to toggle source
# File lib/rabbitmq/http/client.rb, line 255
def list_vhosts(query = {})
  decode_resource_collection(@connection.get("vhosts", query))
end
node_info(name) click to toggle source
# File lib/rabbitmq/http/client.rb, line 74
def node_info(name)
  decode_resource(@connection.get("nodes/#{encode_uri_path_segment(name)}"))
end
overview() click to toggle source
# File lib/rabbitmq/http/client.rb, line 38
def overview
  decode_resource(@connection.get("overview"))
end
protocol_ports() click to toggle source

Returns a hash of protocol => port.

@return [Hash<String, Integer>] Hash of protocol => port

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/rabbitmq/http/client.rb, line 400
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) click to toggle source
# 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) click to toggle source
# File lib/rabbitmq/http/client.rb, line 367
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) click to toggle source
# File lib/rabbitmq/http/client.rb, line 323
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
Also aliased as: create_user
upload_definitions(defs) click to toggle source
# 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) click to toggle source
# File lib/rabbitmq/http/client.rb, line 314
def user_info(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 = {}) click to toggle source
# File lib/rabbitmq/http/client.rb, line 338
def user_permissions(name, query = {})
  decode_resource_collection(@connection.get("users/#{encode_uri_path_segment(name)}/permissions", query))
end
vhost_info(name) click to toggle source
# File lib/rabbitmq/http/client.rb, line 259
def vhost_info(name)
  decode_resource(@connection.get("vhosts/#{encode_uri_path_segment(name)}"))
end
whoami() click to toggle source
# File lib/rabbitmq/http/client.rb, line 342
def whoami
  decode_resource(@connection.get("whoami"))
end

Protected Instance Methods

decode_resource(response) click to toggle source
# File lib/rabbitmq/http/client.rb, line 436
def decode_resource(response)
  @response_helper.decode_resource(response)
end
decode_resource_collection(response) click to toggle source
# File lib/rabbitmq/http/client.rb, line 444
def decode_resource_collection(response)
  @response_helper.decode_resource_collection(response)
end
decode_response_body(body) click to toggle source
# File lib/rabbitmq/http/client.rb, line 440
def decode_response_body(body)
  @response_helper.decode_response_body(body)
end
encode_uri_path_segment(segment) click to toggle source
# File lib/rabbitmq/http/client.rb, line 432
def encode_uri_path_segment(segment)
  @request_helper.encode_uri_path_segment(segment)
end
initialize_connection(endpoint, options = {}) click to toggle source
# File lib/rabbitmq/http/client.rb, line 414
def initialize_connection(endpoint, options = {})
  uri     = URI.parse(endpoint)
  uri.path = "/api" if ["","/"].include?(uri.path)
  user     = uri.user     || options.delete(:username) || "guest"
  password = uri.password || options.delete(:password) || "guest"
  options = options.merge(:url => uri.to_s)
  adapter = options.delete(:adapter) || Faraday.default_adapter

  @connection = Faraday.new(options) do |conn|
    conn.basic_auth user, password
    conn.use        FaradayMiddleware::FollowRedirects, :limit => 3
    conn.use        Faraday::Response::RaiseError
    conn.response   :json, :content_type => /\bjson$/

    conn.adapter    adapter
  end
end