Class: Grafana::WebRequest
- Inherits:
-
Object
- Object
- Grafana::WebRequest
- Defined in:
- lib/grafana/webrequest.rb
Overview
This class standardizes all webcalls. Key functionality is to properly support HTTPS calls as a base functionality.
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#relative_url ⇒ Object
Returns the value of attribute relative_url.
Instance Method Summary collapse
-
#execute(timeout = nil, return_ssl_error = false) ⇒ Response
Executes the HTTP request.
-
#initialize(base_url, options = {}) ⇒ WebRequest
constructor
Initializes a specific HTTP request.
Constructor Details
#initialize(base_url, options = {}) ⇒ WebRequest
Initializes a specific HTTP request.
Default (can be overridden, by specifying the options Hash):
accept: 'application/json'
request: Net::HTTP::Get
content_type: 'application/json'
17 18 19 20 21 22 23 24 25 |
# File 'lib/grafana/webrequest.rb', line 17 def initialize(base_url, = {}) @base_url = base_url = { accept: 'application/json', request: Net::HTTP::Get, content_type: 'application/json' } @options = .merge(.reject { |k, _v| k == :logger && k == :relative_url && k == :ssl_disable_verify }) @relative_url = [:relative_url] @logger = [:logger] || Logger.new(nil) @ssl_disable_verify = [:ssl_disable_verify] || false @ssl_cert = [:ssl_cert] end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/grafana/webrequest.rb', line 6 def @options end |
#relative_url ⇒ Object
Returns the value of attribute relative_url.
6 7 8 |
# File 'lib/grafana/webrequest.rb', line 6 def relative_url @relative_url end |
Instance Method Details
#execute(timeout = nil, return_ssl_error = false) ⇒ Response
Executes the HTTP request
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/grafana/webrequest.rb', line 32 def execute(timeout = nil, return_ssl_error = false) timeout ||= 60 uri = URI.parse("#{@base_url}#{@relative_url}") @http = Net::HTTP.new(uri.host, uri.port) configure_ssl if @base_url =~ /^https/ @http.read_timeout = timeout.to_i request = @options[:request].new(uri.request_uri) request['Accept'] = @options[:accept] if @options[:accept] request['Content-Type'] = @options[:content_type] if @options[:content_type] request['Authorization'] = @options[:authorization] if @options[:authorization] request.body = @options[:body] @logger.debug("Requesting #{uri} with '#{@options[:body]}' and timeout '#{timeout}'") begin response = @http.request(request) rescue OpenSSL::SSL::SSLError => e @logger.error(e.) return e if return_ssl_error return nil end @logger.debug("Received response #{response}") @logger.debug("HTTP response body: #{response.body}") unless response.code =~ /^2.*/ response end |