Class: Dogapi::APIService

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

Overview

Superclass that deals with the details of communicating with the DataDog API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, application_key, silent = true, timeout = nil, endpoint = nil, skip_ssl_validation = false) ⇒ APIService

Returns a new instance of APIService.



89
90
91
92
93
94
95
96
# File 'lib/dogapi/common.rb', line 89

def initialize(api_key, application_key, silent=true, timeout=nil, endpoint=nil, skip_ssl_validation=false)
  @api_key = api_key
  @application_key = application_key
  @api_host = endpoint || Dogapi.find_datadog_host()
  @silent = silent
  @skip_ssl_validation = skip_ssl_validation
  @timeout = timeout || 5
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



88
89
90
# File 'lib/dogapi/common.rb', line 88

def api_key
  @api_key
end

#application_keyObject (readonly)

Returns the value of attribute application_key.



88
89
90
# File 'lib/dogapi/common.rb', line 88

def application_key
  @application_key
end

Instance Method Details

#connectObject

Manages the HTTP connection



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dogapi/common.rb', line 99

def connect
  connection = Net::HTTP

  # Expose using a proxy without setting the HTTPS_PROXY or HTTP_PROXY variables
  proxy = Dogapi.find_proxy()

  if proxy
    proxy_uri = URI.parse(proxy)
    connection = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
  end

  uri = URI.parse(@api_host)
  session = connection.new(uri.host, uri.port)
  session.open_timeout = @timeout
  session.use_ssl = uri.scheme == 'https'
  if @skip_ssl_validation
    session.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  session.start do |conn|
    conn.read_timeout = @timeout
    yield conn
  end
end

#handle_redirect(conn, req, resp, retries = 10) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/dogapi/common.rb', line 198

def handle_redirect(conn, req, resp, retries=10)
  req.uri = URI.parse(resp.header['location'])
  new_response = conn.request(req)
  if retries > 1 && new_response.code.to_i / 100 == 3
    new_response = handle_redirect(conn, req, new_response, retries - 1)
  end
  new_response
end

#handle_response(resp) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/dogapi/common.rb', line 185

def handle_response(resp)
  if resp.code.to_i == 204 || resp.body == '' || resp.body == 'null' || resp.body.nil?
    return resp.code, {}
  end
  begin
    return resp.code, MultiJson.load(resp.body)
  rescue
    is_json = resp.content_type == 'application/json'
    raise "Response Content-Type is not application/json but is #{resp.content_type}: " + resp.body unless is_json
    raise 'Invalid JSON Response: ' + resp.body
  end
end

#prepare_params(extra_params, url, with_app_key) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/dogapi/common.rb', line 167

def prepare_params(extra_params, url, with_app_key)
  if should_set_api_and_app_keys_in_params?(url)
    params = { api_key: @api_key }
    params[:application_key] = @application_key if with_app_key
  else
    params = {}
  end
  params = extra_params.merge params unless extra_params.nil?
  qs_params = params.map { |k, v| CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s) }
  qs = '?' + qs_params.join('&')
  qs
end

#prepare_request(method, url, params, body, send_json, with_app_key) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/dogapi/common.rb', line 152

def prepare_request(method, url, params, body, send_json, with_app_key)
  url_with_params = url + params
  req = method.new(url_with_params)
  req['User-Agent'] = USER_AGENT
  unless should_set_api_and_app_keys_in_params?(url)
    req['DD-API-KEY'] = @api_key
    req['DD-APPLICATION-KEY'] = @application_key if with_app_key
  end
  if send_json
    req.content_type = 'application/json'
    req.body = MultiJson.dump(body)
  end
  return req
end

#request(method, url, extra_params, body, send_json, with_app_key = true) ⇒ Object

Prepares the request and handles the response

method is an implementation of Net::HTTP::Request (e.g. Net::HTTP::Post)

params is a Hash that will be converted to request parameters



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/dogapi/common.rb', line 135

def request(method, url, extra_params, body, send_json, with_app_key=true)
  resp = nil
  connect do |conn|
    begin
      params = prepare_params(extra_params, url, with_app_key)
      req = prepare_request(method, url, params, body, send_json, with_app_key)
      resp = conn.request(req)
      if resp.code.to_i / 100 == 3
        resp = handle_redirect(conn, req, resp)
      end
      return handle_response(resp)
    rescue Exception => e
      suppress_error_if_silent e
    end
  end
end

#should_set_api_and_app_keys_in_params?(url) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
183
# File 'lib/dogapi/common.rb', line 180

def should_set_api_and_app_keys_in_params?(url)
  set_of_urls = Set.new ['/api/v1/series', '/api/v1/check_run', '/api/v1/events', '/api/v1/screen']
  return set_of_urls.include?(url)
end

#suppress_error_if_silent(e) ⇒ Object



123
124
125
126
127
128
# File 'lib/dogapi/common.rb', line 123

def suppress_error_if_silent(e)
  raise e unless @silent

  warn e
  return -1, {}
end