Class: Consul::Client::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/consul/client/http.rb

Overview

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

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, logger:) ⇒ HTTP

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of HTTP.



17
18
19
20
21
# File 'lib/consul/client/http.rb', line 17

def initialize(host:, port:, logger:)
  @host   = host
  @port   = port
  @logger = logger
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#get(request_uri) ⇒ Object

Get JSON data from an endpoint.

Parameters:

  • request_uri (String)

    portion of the HTTP path after the version base, such as /agent/self.

Returns:

  • (Object)

    parsed JSON response

Raises:



29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object

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

Examples:

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

Parameters:

  • request_uri (String)

    portion of the HTTP path after the version base, such as /agent/self.

Returns:

  • (Object)

    parsed JSON response

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

Put request to an endpoint. If data is provided, it is JSON encoded and sent in the request body.

Parameters:

  • request_uri (String)

    portion of the HTTP path after the version base, such as /agent/self.

  • data (Object) (defaults to: nil)

    body for request

Returns:

  • (Object)

    parsed JSON response

Raises:



83
84
85
86
87
88
89
90
91
92
# 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