Class: Wavefront::ApiCaller

Inherits:
Object
  • Object
show all
Includes:
Mixins
Defined in:
lib/wavefront-sdk/core/api_caller.rb

Overview

Constructs and makes API calls to Wavefront.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins

#log, #parse_relative_time, #parse_time, #relative_time, #time_multiplier, #valid_relative_time?

Constructor Details

#initialize(calling_class, creds = {}, opts = {}) ⇒ Nil

Parameters:

  • calling_class

    [

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

    Wavefront credentials

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


23
24
25
26
27
28
# File 'lib/wavefront-sdk/core/api_caller.rb', line 23

def initialize(calling_class, creds = {}, opts = {})
  @calling_class = calling_class
  @opts          = opts
  setup_class_vars(opts)
  setup_endpoint(creds)
end

Instance Attribute Details

#calling_classObject (readonly)

Returns the value of attribute calling_class.



15
16
17
# File 'lib/wavefront-sdk/core/api_caller.rb', line 15

def calling_class
  @calling_class
end

#debugObject (readonly)

Returns the value of attribute debug.



15
16
17
# File 'lib/wavefront-sdk/core/api_caller.rb', line 15

def debug
  @debug
end

#loggerObject (readonly)

Returns the value of attribute logger.



15
16
17
# File 'lib/wavefront-sdk/core/api_caller.rb', line 15

def logger
  @logger
end

#netObject (readonly)

Returns the value of attribute net.



15
16
17
# File 'lib/wavefront-sdk/core/api_caller.rb', line 15

def net
  @net
end

#noopObject (readonly)

Returns the value of attribute noop.



15
16
17
# File 'lib/wavefront-sdk/core/api_caller.rb', line 15

def noop
  @noop
end

#optsObject (readonly)

Returns the value of attribute opts.



15
16
17
# File 'lib/wavefront-sdk/core/api_caller.rb', line 15

def opts
  @opts
end

#verboseObject (readonly)

Returns the value of attribute verbose.



15
16
17
# File 'lib/wavefront-sdk/core/api_caller.rb', line 15

def verbose
  @verbose
end

Instance Method Details

#delete(path) ⇒ Hash

Make a DELETE call to the Wavefront API and return the result as a Ruby hash.

Parameters:

  • path (String)

    path to be appended to the #net path.

Returns:

  • (Hash)

    API response



104
105
106
# File 'lib/wavefront-sdk/core/api_caller.rb', line 104

def delete(path)
  make_call(mk_conn(path), :delete)
end

#get(path, query = {}) ⇒ Hash

Make a GET call to the Wavefront API and return the result as a Ruby hash.

Parameters:

  • path (String)

    path to be appended to the #net path.

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

    optional key-value pairs with will be made into aquery string

Returns:

  • (Hash)

    API response



61
62
63
# File 'lib/wavefront-sdk/core/api_caller.rb', line 61

def get(path, query = {})
  make_call(mk_conn(path), :get, nil, query)
end

#mk_conn(path, headers = {}) ⇒ URI::HTTPS

Create a Faraday connection object. The server comes from the endpoint passed to the initializer in the ‘creds’ hash; the root of the URI is dynamically derived by the #setup_endpoint method.

Parameters:

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

    additional headers

Returns:

  • (URI::HTTPS)


45
46
47
48
49
50
# File 'lib/wavefront-sdk/core/api_caller.rb', line 45

def mk_conn(path, headers = {})
  url = format('%s://%s%s', net[:scheme], net[:endpoint],
               [net[:api_base], path].uri_concat)
  Faraday.new(url:     Addressable::URI.encode(url),
              headers: net[:headers].merge(headers))
end

#post(path, body = nil, ctype = 'text/plain') ⇒ Hash

Make a POST call to the Wavefront API and return the result as a Ruby hash.

Parameters:

  • path (String)

    path to be appended to the #net path.

  • body (String, Object) (defaults to: nil)

    optional body text to post. Objects will be converted to JSON

  • ctype (String) (defaults to: 'text/plain')

    the content type to use when posting

Returns:

  • (Hash)

    API response



75
76
77
78
79
80
# File 'lib/wavefront-sdk/core/api_caller.rb', line 75

def post(path, body = nil, ctype = 'text/plain')
  body = body.to_json unless body.is_a?(String)
  make_call(mk_conn(path,  'Content-Type': ctype,
                           'Accept': 'application/json'),
            :post, nil, body)
end

#put(path, body = nil, ctype = 'application/json') ⇒ Hash

Make a PUT call to the Wavefront API and return the result as a Ruby hash.

Parameters:

  • path (String)

    path to be appended to the #net path.

  • body (String) (defaults to: nil)

    optional body text to post

  • ctype (String) (defaults to: 'application/json')

    the content type to use when putting

Returns:

  • (Hash)

    API response



91
92
93
94
95
# File 'lib/wavefront-sdk/core/api_caller.rb', line 91

def put(path, body = nil, ctype = 'application/json')
  make_call(mk_conn(path,  'Content-Type': ctype,
                           'Accept': 'application/json'),
            :put, nil, body.to_json)
end

#respond(resp) ⇒ Object

If we need to massage a raw response to fit what the Wavefront::Response class expects (I’m looking at you, ‘User’), a class can provide a #response_shim method.



112
113
114
115
116
117
118
119
120
# File 'lib/wavefront-sdk/core/api_caller.rb', line 112

def respond(resp)
  body = if calling_class.respond_to?(:response_shim)
           calling_class.response_shim(resp.body, resp.status)
         else
           resp.body
         end

  Wavefront::Response.new(body, resp.status, @opts)
end

#setup_class_vars(opts) ⇒ Object



30
31
32
33
34
35
# File 'lib/wavefront-sdk/core/api_caller.rb', line 30

def setup_class_vars(opts)
  @logger    = Wavefront::Logger.new(opts)
  @noop      = opts[:noop] || false
  @verbose   = opts[:verbose] || false
  @debug     = opts[:debug] || false
end

#verbosity(conn, method, *args) ⇒ Object

Try to describe the actual HTTP calls we make. There’s a bit of clumsy guesswork here



125
126
127
128
129
130
131
132
# File 'lib/wavefront-sdk/core/api_caller.rb', line 125

def verbosity(conn, method, *args)
  return unless noop || verbose
  log format('uri: %s %s', method.upcase, conn.url_prefix)

  return unless args.last && !args.last.empty?

  log method == :get ? "params: #{args.last}" : "body: #{args.last}"
end