Class: Hawkular::BaseClient

Inherits:
Object
  • Object
show all
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.

Defined Under Namespace

Classes: HawkularConnectionException, HawkularException

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClientUtils

#hawk_escape, #hawk_escape_id

Constructor Details

#initialize(entrypoint = nil, credentials = {}, options = {}) ⇒ BaseClient

Returns a new instance of BaseClient



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hawkular/base_client.rb', line 19

def initialize(entrypoint = nil,
               credentials = {},
               options = {})
  @entrypoint = entrypoint
  @credentials = {
    username: nil,
    password: nil,
    token: nil
  }.merge(credentials)
  @options = {
    verify_ssl: OpenSSL::SSL::VERIFY_PEER,
    headers: {}
  }.merge(options)
  @tenant = @options.delete(:tenant)
  @admin_token = @options.delete(:admin_token)

  @logger = Hawkular::Logger.new

  fail 'You need to provide an entrypoint' if entrypoint.nil?
end

Instance Attribute Details

#tenantsTenants (readonly)

Returns access tenants API

Returns:

  • (Tenants)

    access tenants API



17
18
19
# File 'lib/hawkular/base_client.rb', line 17

def tenants
  @tenants
end

Instance Method Details

#admin_headerObject



183
184
185
186
187
# File 'lib/hawkular/base_client.rb', line 183

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

Parameters:

  • credentials (Hash{:username,:password}) (defaults to: {})

Returns:

  • (String)

    Base64 encoded result



114
115
116
117
118
119
# File 'lib/hawkular/base_client.rb', line 114

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 `,`.

Parameters:

  • params (Hash) (defaults to: {})

    key-values pairs

Returns:

  • (String)

    complete query string to append to a base url, '' if no valid params



125
126
127
128
129
130
131
132
133
134
# File 'lib/hawkular/base_client.rb', line 125

def generate_query_params(params = {})
  params = params.select { |_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



72
73
74
75
76
77
78
79
80
# File 'lib/hawkular/base_client.rb', line 72

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



40
41
42
43
44
45
46
47
48
# File 'lib/hawkular/base_client.rb', line 40

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



50
51
52
53
54
55
56
57
58
59
# File 'lib/hawkular/base_client.rb', line 50

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



61
62
63
64
65
66
67
68
69
70
# File 'lib/hawkular/base_client.rb', line 61

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

Parameters:

  • entrypoint (String)

    base path (URIs are also accepted)

  • suffix_path (String)

    sufix path to be added if it doesn't exist

Returns:

  • (String)

    URL with path attached to it at the end



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/hawkular/base_client.rb', line 143

def normalize_entrypoint_url(entrypoint, suffix_path)
  fail 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

#nowInteger

timestamp of current time

Returns:

  • (Integer)

    timestamp



106
107
108
# File 'lib/hawkular/base_client.rb', line 106

def now
  Integer(Time.now.to_f * 1000)
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.

Parameters:

  • url (String|URI)

    url

Returns:

  • (String)

    URL with the scheme changed to 'ws' or 'wss' depending on the current scheme.



164
165
166
# File 'lib/hawkular/base_client.rb', line 164

def url_with_websocket_scheme(url)
  url.to_s.sub(/^http(s?)/, 'ws\1')
end