Class: Wavefront::CoreApi

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

Overview

Abstract class from which all API classes inherit. When you make any call to the Wavefront API from this SDK, you are returned an OpenStruct object.

Returns:

  • a Wavefront::Response object

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?

Methods included from Validators

#uuid?, #wf_account_id?, #wf_alert_id?, #wf_alert_severity?, #wf_apitoken_id?, #wf_aws_external_id?, #wf_cloudintegration_id?, #wf_dashboard_id?, #wf_derivedmetric_id?, #wf_distribution?, #wf_distribution_count?, #wf_distribution_interval?, #wf_distribution_values?, #wf_epoch?, #wf_event_id?, #wf_granularity?, #wf_ingestionpolicy_id?, #wf_integration_id?, #wf_link_id?, #wf_link_template?, #wf_maintenance_window_id?, #wf_message_id?, #wf_metric_name?, #wf_metricspolicy_id?, #wf_monitoredapplication_id?, #wf_monitoredcluster_id?, #wf_ms_ts?, #wf_name?, #wf_notificant_id?, #wf_permission?, #wf_point?, #wf_point_tag?, #wf_point_tags?, #wf_proxy_id?, #wf_role_id?, #wf_sampling_value?, #wf_savedsearch_entity?, #wf_savedsearch_id?, #wf_serviceaccount_id?, #wf_source_id?, #wf_spansamplingpolicy_id?, #wf_string?, #wf_tag?, #wf_trace?, #wf_ts?, #wf_user_id?, #wf_usergroup_id?, #wf_value?, #wf_version?, #wf_webhook_id?

Constructor Details

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

Create a new API object. This will always be called from a class which inherits this one. If the inheriting class defines #post_initialize, that method will be called afterwards, with the same arguments.

Parameters:

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

    must contain the keys ‘endpoint` (the Wavefront API server) and `token`, the user token with which you wish to access the endpoint. Can optionally contain `agent`, which will become the `user-agent` string sent with all requests. Passed through to the ApiCaller class.

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

    options governing class behaviour. Expected keys are ‘debug`, `noop` and `verbose`, all boolean; and `logger`, which must be a standard Ruby logger object. You can also pass :response_only. If this is true, you will only be returned a hash of the ’response’ object returned by Wavefront. Passed through to the ApiCaller class.



40
41
42
43
44
45
46
47
# File 'lib/wavefront-sdk/core/api.rb', line 40

def initialize(creds = {}, opts = {})
  @creds   = creds
  @opts    = opts
  @api     = setup_api(creds, opts)
  @s_api   = setup_api(creds, opts)
  @logger  = Wavefront::Logger.new(opts)
  post_initialize(creds, opts) if respond_to?(:post_initialize)
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



20
21
22
# File 'lib/wavefront-sdk/core/api.rb', line 20

def api
  @api
end

#credsObject (readonly)

Returns the value of attribute creds.



20
21
22
# File 'lib/wavefront-sdk/core/api.rb', line 20

def creds
  @creds
end

#loggerObject (readonly)

Returns the value of attribute logger.



20
21
22
# File 'lib/wavefront-sdk/core/api.rb', line 20

def logger
  @logger
end

#optsObject (readonly)

Returns the value of attribute opts.



20
21
22
# File 'lib/wavefront-sdk/core/api.rb', line 20

def opts
  @opts
end

#update_keysObject (readonly)

Returns the value of attribute update_keys.



20
21
22
# File 'lib/wavefront-sdk/core/api.rb', line 20

def update_keys
  @update_keys
end

Instance Method Details

#api_baseString

Derive the first part of the API path from the class name. You can override this in your class if you wish. This method is called by the ApiCaller class.

Returns:

  • (String)

    portion of API URI



59
60
61
# File 'lib/wavefront-sdk/core/api.rb', line 59

def api_base
  self.class.name.split('::').last.downcase
end

#api_pathObject

The API path is normally /api/v2/something, but not always. Override this method if not



66
67
68
# File 'lib/wavefront-sdk/core/api.rb', line 66

def api_path
  ['', 'api', 'v2', api_base].uri_concat
end

#hash_for_update(old, new) ⇒ Hash

Doing a PUT to update an object requires only a certain subset of the keys returned by #describe(). This method takes the existing description of an object and turns it into a new hash which can be PUT.

Parameters:

  • old (Hash)

    a hash of the existing object

  • new (Hash)

    the keys you wish to update

Returns:

  • (Hash)

    a hash containing only the keys which need to be sent to the API. Keys will be symbolized.

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
# File 'lib/wavefront-sdk/core/api.rb', line 94

def hash_for_update(old, new)
  raise ArgumentError unless old.is_a?(Hash) && new.is_a?(Hash)

  old.merge(new).transform_keys(&:to_sym).select do |k, _v|
    update_keys.include?(k)
  end
end

#setup_api(creds, opts) ⇒ Object



49
50
51
# File 'lib/wavefront-sdk/core/api.rb', line 49

def setup_api(creds, opts)
  Wavefront::ApiCaller.new(self, creds, opts)
end

#time_to_ms(time) ⇒ Ingeter

Convert an epoch timestamp into epoch milliseconds. If the timestamp looks like it’s already epoch milliseconds, return it as-is.

Parameters:

  • t (Integer)

    epoch timestamp

Returns:

  • (Ingeter)

    epoch millisecond timestamp



77
78
79
80
81
82
# File 'lib/wavefront-sdk/core/api.rb', line 77

def time_to_ms(time)
  return false unless time.is_a?(Integer)
  return time if time.to_s.size == 13

  (time.to_f * 1000).round
end