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.
- #end_time(opts) ⇒ Object
-
#get(path, query = {}) ⇒ Hash
Make a GET call to the Wavefront API and return the result as a Ruby hash.
-
#get_flat_params(path, query = {}) ⇒ Object
Had to introduce this for the Wavefront::Dashboard#acls method, which uses a query string of multiple id=s.
-
#get_stream(path, query = {}, opts = {}) ⇒ Object
This is used by the Wavefront::Unstable::Spy methods to stream data.
- #initialize(calling_class, creds = {}, opts = {}) ⇒ Nil constructor
-
#mk_conn(path, headers = {}, opts = {}) ⇒ 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) ⇒ String
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
- #stream_connection(conn, query, 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
26 27 28 29 30 31 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 26 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.
18 19 20 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 18 def calling_class @calling_class end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
18 19 20 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 18 def debug @debug end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
18 19 20 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 18 def logger @logger end |
#net ⇒ Object (readonly)
Returns the value of attribute net.
18 19 20 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 18 def net @net end |
#noop ⇒ Object (readonly)
Returns the value of attribute noop.
18 19 20 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 18 def noop @noop end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
18 19 20 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 18 def opts @opts end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
18 19 20 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 18 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.
164 165 166 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 164 def delete(path) make_call(mk_conn(path), :delete) end |
#end_time(opts) ⇒ Object
121 122 123 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 121 def end_time(opts) Time.right_now + opts[:timeout] if opts[:timeout]&.positive? end |
#get(path, query = {}) ⇒ Hash
Make a GET call to the Wavefront API and return the result as a Ruby hash.
71 72 73 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 71 def get(path, query = {}) make_call(mk_conn(path, {}), :get, nil, query) end |
#get_flat_params(path, query = {}) ⇒ Object
Had to introduce this for the Wavefront::Dashboard#acls method, which uses a query string of multiple id=s. By default Faraday only uses the last one. You must set the ‘params_encoder`. Rather than convolute the existing logic, it was cleaner to add this method. Parameters are same as #get.
81 82 83 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 81 def get_flat_params(path, query = {}) make_call(flat_param_conn(path, query), :get) end |
#get_stream(path, query = {}, opts = {}) ⇒ Object
This is used by the Wavefront::Unstable::Spy methods to stream data. It prints to standard out.
97 98 99 100 101 102 103 104 105 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 97 def get_stream(path, query = {}, opts = {}) conn = flat_param_conn(path, query) verbosity(conn, :get, query) stream_connection(conn, query, opts) rescue Faraday::TimeoutError raise Wavefront::Exception::NetworkTimeout rescue StopIteration nil end |
#mk_conn(path, headers = {}, opts = {}) ⇒ 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.
50 51 52 53 54 55 56 57 58 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 50 def mk_conn(path, headers = {}, opts = {}) url = format('%<scheme>s://%<endpoint>s%<path>s', scheme: net[:scheme], endpoint: net[:endpoint], path: [net[:api_base], path].uri_concat) set_opts = { url: Addressable::URI.encode(url), headers: net[:headers].merge(headers) } Faraday.new(set_opts.merge(opts)) 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.
135 136 137 138 139 140 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 135 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.
151 152 153 154 155 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 151 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) ⇒ String
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.
174 175 176 177 178 179 180 181 182 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 174 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
33 34 35 36 37 38 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 33 def setup_class_vars(opts) @logger = Wavefront::Logger.new(opts) @noop = opts[:noop] || false @verbose = opts[:verbose] || false @debug = opts[:debug] || false end |
#stream_connection(conn, query, opts) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 107 def stream_connection(conn, query, opts) t_end = end_time(opts) conn.get do |req| req.params = query req..on_data = proc do |chunk, _size| raise StopIteration if t_end && Time.right_now >= t_end puts Time.now if opts[:timestamp_chunks] puts chunk end end end |
#verbosity(conn, method, *args) ⇒ Object
Try to describe the actual HTTP calls we make. There’s a bit of clumsy guesswork here
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/wavefront-sdk/core/api_caller.rb', line 187 def verbosity(conn, method, *args) return unless noop || verbose log format('uri: %<method>s %<path>s', method: method.upcase, path: conn.url_prefix) return unless args.last && !args.last.empty? log method == :get ? "params: #{args.last}" : "body: #{args.last}" end |