Class: Hawkular::BaseClient
- Inherits:
-
Object
- Object
- Hawkular::BaseClient
- Includes:
- ClientUtils
- Defined in:
- lib/hawkular/base_client.rb
Overview
This is the base functionality for all the clients, that inherit from it. You should not directly use it, but through the more specialized clients.
Direct Known Subclasses
Alerts::Client, Inventory::Client, Metrics::Client, Operations::Client, Prometheus::Alerter, Prometheus::Client, Token::Client
Constant Summary collapse
- HawkularException =
Hawkular::Exception
- HawkularConnectionException =
Hawkular::ConnectionException
Instance Attribute Summary collapse
-
#tenants ⇒ Tenants
readonly
Access tenants API.
Instance Method Summary collapse
- #admin_header ⇒ Object
-
#base_64_credentials(credentials = {}) ⇒ String
Encode the passed credentials (username/password) into a base64 representation that can be used to generate a Http-Authentication header.
-
#generate_query_params(params = {}) ⇒ String
Generate a query string from the passed hash, starting with ‘?’ Values may be an array, in which case the array values are joined together by ‘,`.
- #http_delete(suburl, headers = {}) ⇒ Object
- #http_get(suburl, headers = {}) ⇒ Object
- #http_post(suburl, hash, headers = {}) ⇒ Object
- #http_put(suburl, hash, headers = {}) ⇒ Object
-
#initialize(entrypoint = nil, credentials = {}, options = {}) ⇒ BaseClient
constructor
A new instance of BaseClient.
-
#normalize_entrypoint_url(entrypoint, suffix_path) ⇒ String
Generate a new url with the passed sufix path if the path is not already added also, this function always remove the slash at the end of the URL, so if your entrypoint is localhost/hawkular/inventory/ this function will return localhost/hawkular/inventory to the URL.
-
#now ⇒ Integer
timestamp of current time.
- #url(url_format, *params) ⇒ Object
-
#url_with_websocket_scheme(url) ⇒ String
Generate a new url using the websocket scheme.
Methods included from ClientUtils
Constructor Details
#initialize(entrypoint = nil, credentials = {}, options = {}) ⇒ BaseClient
Returns a new instance of BaseClient.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/hawkular/base_client.rb', line 23 def initialize(entrypoint = nil, credentials = {}, = {}) @entrypoint = entrypoint @credentials = { username: nil, password: nil, token: nil }.merge(credentials) @options = { verify_ssl: OpenSSL::SSL::VERIFY_PEER, headers: {} }.merge() @tenant = @options.delete(:tenant) @admin_token = @options.delete(:admin_token) @logger = Hawkular::Logger.new fail Hawkular::ArgumentError, 'You need to provide an entrypoint' if entrypoint.nil? end |
Instance Attribute Details
#tenants ⇒ Tenants (readonly)
Returns access tenants API.
21 22 23 |
# File 'lib/hawkular/base_client.rb', line 21 def tenants @tenants end |
Instance Method Details
#admin_header ⇒ Object
177 178 179 180 181 |
# File 'lib/hawkular/base_client.rb', line 177 def admin_header headers = {} headers[:'Hawkular-Admin-Token'] = @admin_token unless @admin_token.nil? headers end |
#base_64_credentials(credentials = {}) ⇒ String
Encode the passed credentials (username/password) into a base64 representation that can be used to generate a Http-Authentication header
122 123 124 125 126 127 |
# File 'lib/hawkular/base_client.rb', line 122 def base_64_credentials(credentials = {}) creds = credentials.empty? ? @credentials : credentials encoded = Base64.encode64(creds[:username] + ':' + creds[:password]) encoded.rstrip! end |
#generate_query_params(params = {}) ⇒ String
Generate a query string from the passed hash, starting with ‘?’ Values may be an array, in which case the array values are joined together by ‘,`.
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/hawkular/base_client.rb', line 133 def generate_query_params(params = {}) params = params.reject { |_k, v| v.nil? || ((v.instance_of? Array) && v.empty?) } return '' if params.empty? params.inject('?') do |ret, (k, v)| ret += '&' unless ret == '?' part = v.instance_of?(Array) ? "#{k}=#{v.join(',')}" : "#{k}=#{v}" ret + hawk_escape(part) end end |
#http_delete(suburl, headers = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/hawkular/base_client.rb', line 80 def http_delete(suburl, headers = {}) res = rest_client(suburl).delete(http_headers(headers)) logger.log(res) res.empty? ? {} : JSON.parse(res) rescue handle_fault $ERROR_INFO end |
#http_get(suburl, headers = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/hawkular/base_client.rb', line 48 def http_get(suburl, headers = {}) res = rest_client(suburl).get(http_headers(headers)) logger.log(res) res.empty? ? {} : JSON.parse(res) rescue handle_fault $ERROR_INFO end |
#http_post(suburl, hash, headers = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/hawkular/base_client.rb', line 58 def http_post(suburl, hash, headers = {}) body = JSON.generate(hash) res = rest_client(suburl).post(body, http_headers(headers)) logger.log(res) res.empty? ? {} : JSON.parse(res) rescue handle_fault $ERROR_INFO end |
#http_put(suburl, hash, headers = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/hawkular/base_client.rb', line 69 def http_put(suburl, hash, headers = {}) body = JSON.generate(hash) res = rest_client(suburl).put(body, http_headers(headers)) logger.log(res) res.empty? ? {} : JSON.parse(res) rescue handle_fault $ERROR_INFO end |
#normalize_entrypoint_url(entrypoint, suffix_path) ⇒ String
Generate a new url with the passed sufix path if the path is not already added also, this function always remove the slash at the end of the URL, so if your entrypoint is localhost/hawkular/inventory/ this function will return localhost/hawkular/inventory to the URL
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/hawkular/base_client.rb', line 151 def normalize_entrypoint_url(entrypoint, suffix_path) fail Hawkular::ArgumentError, 'suffix_path must not be empty' if suffix_path.empty? strip_path = suffix_path.gsub(%r{/$}, '') strip_path.nil? || suffix_path = strip_path strip_path = suffix_path.gsub(%r{^/}, '') strip_path.nil? || suffix_path = strip_path entrypoint = entrypoint.to_s strip_entrypoint = entrypoint.gsub(%r{/$}, '') strip_path.nil? && strip_entrypoint = entrypoint relative_path_rgx = Regexp.new("\/#{Regexp.quote(suffix_path)}(\/)*$") if relative_path_rgx.match(entrypoint) strip_entrypoint else "#{strip_entrypoint}/#{suffix_path}" end end |
#now ⇒ Integer
timestamp of current time
114 115 116 |
# File 'lib/hawkular/base_client.rb', line 114 def now Integer(Time.now.to_f * 1000) end |
#url(url_format, *params) ⇒ Object
44 45 46 |
# File 'lib/hawkular/base_client.rb', line 44 def url(url_format, *params) url_format % params.map { |p| ERB::Util.url_encode(p) } end |
#url_with_websocket_scheme(url) ⇒ String
Generate a new url using the websocket scheme. It changes the current scheme to ‘ws’ for ‘http’ and ‘wss’ for ‘https’ urls.
173 174 175 |
# File 'lib/hawkular/base_client.rb', line 173 def url_with_websocket_scheme(url) url.to_s.sub(/^http(s?)/, 'ws\1') end |