class Consul::Client::Base
Public API Base
.
Public Class Methods
new(options = nil)
click to toggle source
Public: Constructor with options hash
Optional Parameters:
options[:data_center] - The consul data center. Default: 'dc1'. options[:api_host] - The api host to request against. Default: '127.0.0.1'. options[:api_port] - The api port the api host is listening to. Default: '8500'. options[:version] - The Consul API version to use. Default: 'v1'. options[:logger] - The default logging mechanism. Default: Logger.new(STDOUT).
Return: This instance
# File lib/consul/client/base.rb, line 22 def initialize(options = nil) options = {} if options.nil? raise TypeError, 'Options must be nil or a Hash' unless options.is_a?(Hash) @options = options.clone end
Public Instance Methods
Protected Instance Methods
_get(url, params = nil, json_only = true)
click to toggle source
Protected: Generic get request. Wraps error handling and url validation.
url - url endpoint to hit. params - Hash of key to value parameters json_only - Flag that annotates we should only be expecting json back. Default true, most consul endpoints return JSON.
Returns: Throws:
ArgumentError: the url is not valid. IOError: Unable to reach Consul Agent.
# File lib/consul/client/base.rb, line 45 def _get(url, params = nil, json_only = true) # Validation validate_url(url) opts = {} opts[:params] = params unless params.nil? opts[:accept] = :json if json_only begin resp = makeRequestWithProxySetting { RestClient.get url, opts } success = (resp.code == 200 or resp.code == 201) if success logger.debug("Successful GET at endpoint #{url}") else logger.warn("Unable to GET from endpoint #{url} returned code: #{resp.code}") end return resp rescue Exception => e # Unable to communicate with consul agent. logger.warn(e.message) raise IOError.new "Unable to complete get request: #{e}" end end
_put(url, value, params = nil)
click to toggle source
Protected: Generic put request. Wraps error and translates Rest response to success or failure.
url - The url endpoint for the put request. value - The value to put at the url endpoint.
Returns: true on success or false on failure and the body of the return message. Throws:
ArgumentError: the url is not valid. IOError: Unable to reach Consul Agent.
# File lib/consul/client/base.rb, line 81 def _put(url, value, params = nil) # Validation validate_url(url) # If possible, Convert value to json unless Consul::Utils.valid_json?(value) value = value.to_json if value.respond_to?(:to_json) end opts = {} opts[:params] = params unless params.nil? begin opts[:content_type] = :json if Consul::Utils.valid_json?(value) resp = makeRequestWithProxySetting { RestClient.put(url, value, opts) {|response, req, res| response } } success = (resp.code == 200 or resp.code == 201) if success logger.debug("Successful PUT #{value} at endpoint #{url}") else logger.warn("Unable to PUT #{value} at endpoint #{url} returned code: #{resp.code}") end return success, resp.body rescue Exception => e logger.error('RestClient.put Error: Unable to reach consul agent') raise IOError.new "Unable to complete put request: #{e}" end end
base_versioned_url()
click to toggle source
# File lib/consul/client/base.rb, line 151 def base_versioned_url "#{base_url}/#{version}" end
data_center()
click to toggle source
# File lib/consul/client/base.rb, line 127 def data_center @data_center ||= options[:data_center] || 'dc1' end
host()
click to toggle source
# File lib/consul/client/base.rb, line 131 def host @host ||= options[:api_host] || '127.0.0.1' end
https()
click to toggle source
# File lib/consul/client/base.rb, line 147 def https @https = false end
logger()
click to toggle source
# File lib/consul/client/base.rb, line 143 def logger @logger ||= options[:logger] || Logger.new(STDOUT) end
makeRequestWithProxySetting() { || ... }
click to toggle source
# File lib/consul/client/base.rb, line 112 def makeRequestWithProxySetting oldProxySetting = RestClient.proxy RestClient.proxy = @options[:proxy] response = yield RestClient.proxy = oldProxySetting response end
options()
click to toggle source
# File lib/consul/client/base.rb, line 123 def options @options ||= {} end
port()
click to toggle source
# File lib/consul/client/base.rb, line 135 def port @port ||= options[:api_port] || '8500' end
version()
click to toggle source
# File lib/consul/client/base.rb, line 139 def version @version ||= options[:version] || 'v1' end
Private Instance Methods
base_url()
click to toggle source
# File lib/consul/client/base.rb, line 157 def base_url "#{(https ? 'https': 'http')}://#{host}:#{port}" end
validate_url(url)
click to toggle source
Private: Validates the url
# File lib/consul/client/base.rb, line 162 def validate_url(url) raise ArgumentError.new 'URL cannot be blank' if url.to_s == '' end