Class: Dogapi::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/dogapi/common.rb

Overview

DEPRECATED: Going forward, use the newer APIService.

Direct Known Subclasses

EventService, MetricService

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_host = Dogapi.find_datadog_host) ⇒ Service

DEPRECATED: Going forward, use the newer APIService.



40
41
42
43
# File 'lib/dogapi/common.rb', line 40

def initialize(api_key, api_host=Dogapi.find_datadog_host)
  @api_key = api_key
  @host = api_host
end

Instance Method Details

#connectObject

DEPRECATED: Going forward, use the newer APIService.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dogapi/common.rb', line 46

def connect
  warn '[DEPRECATION] Dogapi::Service has been deprecated in favor of the newer V1 services'
  uri = URI.parse(@host)
  session = Net::HTTP.new(uri.host, uri.port)
  if 'https' == uri.scheme
    session.use_ssl = true
  end
  session.start do |conn|
    yield(conn)
  end
end

#request(method, url, params) ⇒ Object

DEPRECATED: Going forward, use the newer APIService.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/dogapi/common.rb', line 59

def request(method, url, params)
  warn '[DEPRECATION] Dogapi::Service has been deprecated in favor of the newer V1 services'
  if !params.has_key? :api_key
    params[:api_key] = @api_key
  end

  resp_obj = nil
  connect do |conn|
    req = method.new(url)
    req.set_form_data params
    resp = conn.request(req)
    begin
      resp_obj = MultiJson.load(resp.body)
    rescue
      raise 'Invalid JSON Response: ' + resp.body
    end

    if resp_obj.has_key? 'error'
      request_string = params.pretty_inspect
      error_string = resp_obj['error']
      raise "Failed request\n#{request_string}#{error_string}"
    end
  end
  resp_obj
end