class Consul::Client::HTTP

Low-level wrapper around consul HTTP api. Do not instantiate this class directly, instead use the appropriate factory methods.

@see Consul::Client::V1#http

Attributes

logger[RW]

Public Class Methods

new(host:, port:, logger:) click to toggle source

@api private

# File lib/consul/client/http.rb, line 17
def initialize(host:, port:, logger:)
  @host   = host
  @port   = port
  @logger = logger
end

Public Instance Methods

get(request_uri) click to toggle source

Get JSON data from an endpoint.

@param request_uri [String] portion of the HTTP path after the version

base, such as +/agent/self+.

@return [Object] parsed JSON response @raise [ResponseException] if non-200 response is received.

# File lib/consul/client/http.rb, line 29
def get(request_uri)
  url = base_uri + request_uri
  logger.debug("GET #{url}")

  uri = URI.parse(url)

  response = http_request(:get, uri)

  parse_body(response)
end
get_while(request_uri, &block) click to toggle source

Watch an endpoint until the value returned causes the block to evaluate false.

@param request_uri [String] portion of the HTTP path after the version

base, such as +/agent/self+.

@return [Object] parsed JSON response @raise [ResponseException] if non-200 response is received. @example blocks until there are at least 3 passing nodes for the web service.

http.get_while("/health/service/web?passing") do |data|
  data.size <= 2
end
# File lib/consul/client/http.rb, line 51
def get_while(request_uri, &block)
  url = base_uri + request_uri
  index = 0
  json = nil

  check = ->{
    uri = URI.parse(url)
    uri.query ||= ""
    uri.query += "&index=#{index}&wait=60s"
    logger.debug("GET #{uri}")

    response = http_request(:get, uri)
    index = response['x-consul-index'].to_i

    json = parse_body(response)

    block.(json)
  }

  while check.()
  end

  json
end
put(request_uri, data = nil) click to toggle source

Put request to an endpoint. If data is provided, it is JSON encoded and sent in the request body. @param request_uri [String] portion of the HTTP path after the version

base, such as +/agent/self+.

@param data [Object] body for request @return [Object] parsed JSON response @raise [ResponseException] if non-200 response is received.

# File lib/consul/client/http.rb, line 83
def put(request_uri, data = nil)
  url = base_uri + request_uri
  logger.debug("PUT #{url}")

  uri = URI.parse(url)

  response = http_request(:put, uri, data)

  parse_body(response)
end

Protected Instance Methods

base_uri() click to toggle source
# File lib/consul/client/http.rb, line 98
def base_uri
  "http://#{@host}:#{@port}/v1"
end
http_request(method, uri, data = nil) click to toggle source
# File lib/consul/client/http.rb, line 106
def http_request(method, uri, data = nil)
  method = {
    get: Net::HTTP::Get,
    put: Net::HTTP::Put,
  }.fetch(method)

  http     = Net::HTTP.new(uri.host, uri.port)
  request  = method.new(uri.request_uri)
  request.body = data.to_json if data
  response = http.request(request)

  if response.code.to_i >= 400
    raise ResponseException, "#{response.code} on #{uri}"
  end

  response
end
parse_body(response) click to toggle source
# File lib/consul/client/http.rb, line 102
def parse_body(response)
  JSON.parse("[#{response.body}]")[0]
end