Class: Consul::Client::HTTP
- Inherits:
-
Object
- Object
- Consul::Client::HTTP
- 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.
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
-
#get(request_uri) ⇒ Object
Get JSON data from an endpoint.
-
#get_while(request_uri, &block) ⇒ Object
Watch an endpoint until the value returned causes the block to evaluate
false. -
#initialize(host:, port:, logger:) ⇒ HTTP
constructor
private
A new instance of HTTP.
-
#put(request_uri, data = nil) ⇒ Object
Put request to an endpoint.
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
#logger ⇒ Object
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.
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.
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.
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 |