Class: Grafana::WebRequest

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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'

Parameters:

  • base_url (String)

    URL which shall be queried

  • options (Hash) (defaults to: {})

    options, which shall be merged to the request. Also allows ‘logger` option



17
18
19
20
21
22
23
24
25
# File 'lib/grafana/webrequest.rb', line 17

def initialize(base_url, options = {})
  @base_url = base_url
  default_options = { accept: 'application/json', request: Net::HTTP::Get, content_type: 'application/json' }
  @options = default_options.merge(options.reject { |k, _v| k == :logger && k == :relative_url && k == :ssl_disable_verify })
  @relative_url = options[:relative_url]
  @logger = options[:logger] || Logger.new(nil)
  @ssl_disable_verify = options[:ssl_disable_verify] || false
  @ssl_cert = options[:ssl_cert]
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/grafana/webrequest.rb', line 6

def options
  @options
end

#relative_urlObject

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

Parameters:

  • timeout (Integer) (defaults to: nil)

    number of seconds to wait, before the http request is cancelled, defaults to 60 seconds

  • return_ssl_error (Boolean) (defaults to: false)

    True, if the SSL error object shall be returned on SSL error

Returns:

  • (Response)

    HTTP response object



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.message)
    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