Class: DogapiDemo::APIService
- Inherits:
-
Object
- Object
- DogapiDemo::APIService
- Defined in:
- lib/dogapi-demo/common.rb
Overview
Superclass that deals with the details of communicating with the DataDog API
Direct Known Subclasses
V1::AlertService, V1::CommentService, V1::DashService, V1::EmbedService, V1::EventService, V1::MetricService, V1::MonitorService, V1::ScreenboardService, V1::SearchService, V1::ServiceCheckService, V1::SnapshotService, V1::TagService, V1::UserService
Instance Method Summary collapse
-
#connect ⇒ Object
Manages the HTTP connection.
-
#initialize(api_key, application_key, silent = true, timeout = nil) ⇒ APIService
constructor
A new instance of APIService.
-
#request(method, url, params, body, send_json) ⇒ Object
Prepares the request and handles the response.
- #suppress_error_if_silent(e) ⇒ Object
Constructor Details
#initialize(api_key, application_key, silent = true, timeout = nil) ⇒ APIService
Returns a new instance of APIService.
72 73 74 75 76 77 78 |
# File 'lib/dogapi-demo/common.rb', line 72 def initialize(api_key, application_key, silent=true, timeout=nil) @api_key = api_key @application_key = application_key @api_host = DogapiDemo.find_datadog_host() @silent = silent @timeout = timeout || 5 end |
Instance Method Details
#connect ⇒ Object
Manages the HTTP connection
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/dogapi-demo/common.rb', line 81 def connect connection = Net::HTTP # After ruby 2.0 Net::HTTP looks for the env variable but not ruby 1.9 if RUBY_VERSION < "2.0.0" proxy = ENV["HTTPS_PROXY"] || ENV["https_proxy"] || ENV["HTTP_PROXY"] || ENV["http_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 end uri = URI.parse(@api_host) session = connection.new(uri.host, uri.port) session.open_timeout = @timeout session.use_ssl = uri.scheme == 'https' session.start do |conn| conn.read_timeout = @timeout yield(conn) end end |
#request(method, url, params, body, send_json) ⇒ 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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/dogapi-demo/common.rb', line 117 def request(method, url, params, body, send_json) resp_obj = nil resp = nil connect do |conn| if params and params.size > 0 qs_params = params.map { |k, v| k.to_s + "=" + v.to_s } qs = "?" + qs_params.join("&") url = url + qs end req = method.new(url) if send_json req.content_type = 'application/json' req.body = MultiJson.dump(body) end resp = conn.request(req) resp_str = resp.body if resp.code != 204 and resp.body != '' and resp.body != 'null' and resp.body != nil begin resp_obj = MultiJson.load(resp.body) rescue raise 'Invalid JSON Response: ' + resp.body end else resp_obj = {} end end return resp.code, resp_obj end |
#suppress_error_if_silent(e) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/dogapi-demo/common.rb', line 103 def suppress_error_if_silent(e) if @silent warn e return -1, {} else raise e end end |