class Dogapi::APIService

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

Attributes

api_key[R]
application_key[R]

Public Class Methods

new(api_key, application_key, silent=true, timeout=nil, endpoint=nil, skip_ssl_validation=false) click to toggle source
   # File lib/dogapi/common.rb
89 def initialize(api_key, application_key, silent=true, timeout=nil, endpoint=nil, skip_ssl_validation=false)
90   @api_key = api_key
91   @application_key = application_key
92   @api_host = endpoint || Dogapi.find_datadog_host()
93   @silent = silent
94   @skip_ssl_validation = skip_ssl_validation
95   @timeout = timeout || 5
96 end

Public Instance Methods

connect() { |conn| ... } click to toggle source

Manages the HTTP connection

    # File lib/dogapi/common.rb
 99 def connect
100   connection = Net::HTTP
101 
102   # Expose using a proxy without setting the HTTPS_PROXY or HTTP_PROXY variables
103   proxy = Dogapi.find_proxy()
104 
105   if proxy
106     proxy_uri = URI.parse(proxy)
107     connection = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
108   end
109 
110   uri = URI.parse(@api_host)
111   session = connection.new(uri.host, uri.port)
112   session.open_timeout = @timeout
113   session.use_ssl = uri.scheme == 'https'
114   if @skip_ssl_validation
115     session.verify_mode = OpenSSL::SSL::VERIFY_NONE
116   end
117   session.start do |conn|
118     conn.read_timeout = @timeout
119     yield conn
120   end
121 end
handle_redirect(conn, req, resp, retries=10) click to toggle source
    # File lib/dogapi/common.rb
198 def handle_redirect(conn, req, resp, retries=10)
199   req.uri = URI.parse(resp.header['location'])
200   new_response = conn.request(req)
201   if retries > 1 && new_response.code.to_i / 100 == 3
202     new_response = handle_redirect(conn, req, new_response, retries - 1)
203   end
204   new_response
205 end
handle_response(resp) click to toggle source
    # File lib/dogapi/common.rb
185 def handle_response(resp)
186   if resp.code.to_i == 204 || resp.body == '' || resp.body == 'null' || resp.body.nil?
187     return resp.code, {}
188   end
189   begin
190     return resp.code, MultiJson.load(resp.body)
191   rescue
192     is_json = resp.content_type == 'application/json'
193     raise "Response Content-Type is not application/json but is #{resp.content_type}: " + resp.body unless is_json
194     raise 'Invalid JSON Response: ' + resp.body
195   end
196 end
prepare_params(extra_params, url, with_app_key) click to toggle source
    # File lib/dogapi/common.rb
167 def prepare_params(extra_params, url, with_app_key)
168   if should_set_api_and_app_keys_in_params?(url)
169     params = { api_key: @api_key }
170     params[:application_key] = @application_key if with_app_key
171   else
172     params = {}
173   end
174   params = extra_params.merge params unless extra_params.nil?
175   qs_params = params.map { |k, v| CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s) }
176   qs = '?' + qs_params.join('&')
177   qs
178 end
prepare_request(method, url, params, body, send_json, with_app_key) click to toggle source
    # File lib/dogapi/common.rb
152 def prepare_request(method, url, params, body, send_json, with_app_key)
153   url_with_params = url + params
154   req = method.new(url_with_params)
155   req['User-Agent'] = USER_AGENT
156   unless should_set_api_and_app_keys_in_params?(url)
157     req['DD-API-KEY'] = @api_key
158     req['DD-APPLICATION-KEY'] = @application_key if with_app_key
159   end
160   if send_json
161     req.content_type = 'application/json'
162     req.body = MultiJson.dump(body)
163   end
164   return req
165 end
request(method, url, extra_params, body, send_json, with_app_key=true) click to toggle source

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

    # File lib/dogapi/common.rb
135 def request(method, url, extra_params, body, send_json, with_app_key=true)
136   resp = nil
137   connect do |conn|
138     begin
139       params = prepare_params(extra_params, url, with_app_key)
140       req = prepare_request(method, url, params, body, send_json, with_app_key)
141       resp = conn.request(req)
142       if resp.code.to_i / 100 == 3
143         resp = handle_redirect(conn, req, resp)
144       end
145       return handle_response(resp)
146     rescue Exception => e
147       suppress_error_if_silent e
148     end
149   end
150 end
should_set_api_and_app_keys_in_params?(url) click to toggle source
    # File lib/dogapi/common.rb
180 def should_set_api_and_app_keys_in_params?(url)
181   set_of_urls = Set.new ['/api/v1/series', '/api/v1/check_run', '/api/v1/events', '/api/v1/screen']
182   return set_of_urls.include?(url)
183 end
suppress_error_if_silent(e) click to toggle source
    # File lib/dogapi/common.rb
123 def suppress_error_if_silent(e)
124   raise e unless @silent
125 
126   warn e
127   return -1, {}
128 end