Class: Consul::Client::HTTP
- Inherits:
-
Object
- Object
- Consul::Client::HTTP
- Defined in:
- lib/consul/client/http.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #delete(request_uri) ⇒ Object
- #exists?(request_uri) ⇒ Boolean
- #get(request_uri, recurse: false, raw: false) ⇒ Object
-
#initialize(host:, port:, logger:) ⇒ HTTP
constructor
A new instance of HTTP.
- #put(request_uri, data = nil) ⇒ Object
- #watch(request_uri, index: 0, wait: '60s', recurse: false) ⇒ Object
Constructor Details
#initialize(host:, port:, logger:) ⇒ HTTP
Returns a new instance of HTTP.
15 16 17 18 19 |
# File 'lib/consul/client/http.rb', line 15 def initialize(host:, port:, logger:) @host = host @port = port @logger = logger end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
111 112 113 |
# File 'lib/consul/client/http.rb', line 111 def logger @logger end |
Instance Method Details
#delete(request_uri) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/consul/client/http.rb', line 95 def delete(request_uri) url = base_uri + request_uri logger.debug("DELETE #{url}") uri = URI.parse(url) response = http_request(:delete, uri, request_uri.match('^/kv/') ? data : data.to_json) begin response = http_request(:get, uri) parse_body(response) rescue NotFoundException nil end end |
#exists?(request_uri) ⇒ Boolean
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/consul/client/http.rb', line 35 def exists?(request_uri) url = base_uri + request_uri logger.debug("GET #{url}") uri = URI.parse(url) begin response = http_request(:get, uri) rescue NotFoundException return false end return true end |
#get(request_uri, recurse: false, raw: false) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/consul/client/http.rb', line 21 def get(request_uri, recurse: false, raw: false) url = base_uri + request_uri logger.debug("GET #{url}") uri = URI.parse(url) uri.query ||= "" uri.query += "&recurse" if recurse uri.query += "&raw" if raw response = http_request(:get, uri) raw ? response.body : parse_body(response) end |
#put(request_uri, data = nil) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/consul/client/http.rb', line 84 def put(request_uri, data = nil) url = base_uri + request_uri logger.debug("PUT #{url}") uri = URI.parse(url) response = http_request(:put, uri, request_uri.match('^/kv/') ? data : data.to_json) parse_body(response) end |
#watch(request_uri, index: 0, wait: '60s', recurse: false) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/consul/client/http.rb', line 49 def watch(request_uri, index: 0, wait: '60s', recurse: false) url = base_uri + request_uri uri = URI.parse(url) uri.query ||= "" uri.query += "&index=#{index}&wait=#{wait}" uri.query += "&recurse" if recurse wait_timeout = 0 case wait when /^(.+)s$/ wait_timeout = $1.to_i + 5 when /^(.+)m$/ wait_timeout = ($1.to_i * 60) + 5 else raise InavlidArgument end while true do begin logger.debug("GET #{uri}") response = http_request(:get, uri, nil, wait_timeout) unless response['x-consul-index'].to_i == index return { index: response['x-consul-index'].to_i, body: parse_body(response) } end rescue RpcErrorException logger.warn("Got 500 while watching #{uri}") sleep 5 rescue TimeoutException logger.warn("Got timeout while watching #{uri}") end end end |