Class: Sqreen::Kit::HttpClient

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/sqreen/kit/http_client.rb,
lib/sqreen/kit/http_client/authentication_error.rb,
lib/sqreen/kit/http_client/unexpected_status_error.rb

Overview

An http client doing JSON requests

Defined Under Namespace

Classes: AuthenticationError, UnexpectedStatusError

Constant Summary collapse

DEFAULT_CONNECT_TIMEOUT_S =
5
DEFAULT_READ_TIMEOUT_S =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_url, retry_policy, opts = {}) ⇒ HttpClient

Returns a new instance of HttpClient.

Parameters:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sqreen/kit/http_client.rb', line 30

def initialize(server_url, retry_policy, opts = {})
  @base_url = parse_uri(server_url)

  @retry_policy = retry_policy

  @http = ::Net::HTTP.new(@base_url.host, @base_url.port,
                          opts[:proxy_address] || :ENV,
                          opts[:proxy_port], opts[:proxy_user],
                          opts[:proxy_pass])
  @http.use_ssl = @base_url.scheme.downcase == 'https'
  @http.cert_store = opts[:certificate_store]
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE if ENV['SQREEN_SSL_NO_VERIFY'] # for testing

  @http.open_timeout = opts[:connect_timeout] || DEFAULT_CONNECT_TIMEOUT_S
  @http.ssl_timeout = opts[:read_timeout] || DEFAULT_READ_TIMEOUT_S
  @http.read_timeout = opts[:read_timeout] || DEFAULT_READ_TIMEOUT_S

  @req_nb = 1

  @static_headers = init_static_headers
end

Instance Attribute Details

#base_urlURI (readonly)

Returns:

  • (URI)


26
27
28
# File 'lib/sqreen/kit/http_client.rb', line 26

def base_url
  @base_url
end

#httpObject (readonly)

Returns the value of attribute http.



23
24
25
# File 'lib/sqreen/kit/http_client.rb', line 23

def http
  @http
end

Instance Method Details

#get(path, headers = {}) ⇒ Object



59
60
61
# File 'lib/sqreen/kit/http_client.rb', line 59

def get(path, headers = {})
  request(:GET, path, nil, headers)
end

#post(path, data, headers = {}) ⇒ Object

Parameters:

  • path (String)
  • data (String)
  • headers (Hash{String=>String}) (defaults to: {})


55
56
57
# File 'lib/sqreen/kit/http_client.rb', line 55

def post(path, data, headers = {})
  request(:POST, path, data, headers)
end