Class: Wavefront::ApiCaller
- Inherits:
-
Object
- Object
- Wavefront::ApiCaller
- Includes:
- Mixins
- Defined in:
- lib/wavefront-sdk/core/api_caller.rb
Overview
Constructs and makes API calls to Wavefront.
Instance Attribute Summary collapse
-
#calling_class ⇒ Object
readonly
Returns the value of attribute calling_class.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#net ⇒ Object
readonly
Returns the value of attribute net.
-
#noop ⇒ Object
readonly
Returns the value of attribute noop.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
-
#delete(path) ⇒ Hash
Make a DELETE call to the Wavefront API and return the result as a Ruby hash.
-
#get(path, query = {}) ⇒ Hash
Make a GET call to the Wavefront API and return the result as a Ruby hash.
- #initialize(calling_class, creds = {}, opts = {}) ⇒ Nil constructor
-
#mk_conn(path, headers = {}) ⇒ URI::HTTPS
Create a Faraday connection object.
-
#post(path, body = nil, ctype = 'text/plain') ⇒ Hash
Make a POST call to the Wavefront API and return the result as a Ruby hash.
-
#put(path, body = nil, ctype = 'application/json') ⇒ Hash
Make a PUT call to the Wavefront API and return the result as a Ruby hash.
-
#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.
- #setup_class_vars(opts) ⇒ Object
-
#verbosity(conn, method, *args) ⇒ Object
Try to describe the actual HTTP calls we make.
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
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_class ⇒ Object (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 |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
15 16 17 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 15 def debug @debug end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
15 16 17 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 15 def logger @logger end |
#net ⇒ Object (readonly)
Returns the value of attribute net.
15 16 17 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 15 def net @net end |
#noop ⇒ Object (readonly)
Returns the value of attribute noop.
15 16 17 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 15 def noop @noop end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
15 16 17 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 15 def opts @opts end |
#verbose ⇒ Object (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.
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.
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.
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.
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.
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 |