class Sqreen::Kit::HttpClient
An http client doing JSON requests
Constants
- DEFAULT_CONNECT_TIMEOUT_S
- DEFAULT_READ_TIMEOUT_S
Attributes
base_url[R]
@return [URI]
http[R]
Public Class Methods
new(server_url, retry_policy, opts = {})
click to toggle source
@param server_url [String] @param retry_policy [Sqreen::Kit::RetryPolicy]
# 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
Public Instance Methods
get(path, headers = {})
click to toggle source
# File lib/sqreen/kit/http_client.rb, line 59 def get(path, headers = {}) request(:GET, path, nil, headers) end
post(path, data, headers = {})
click to toggle source
@param path [String] @param data [String] @param headers [Hash{String=>String}]
# File lib/sqreen/kit/http_client.rb, line 55 def post(path, data, headers = {}) request(:POST, path, data, headers) end
Private Instance Methods
connect()
click to toggle source
# File lib/sqreen/kit/http_client.rb, line 79 def connect return if connected? logger.warn { "Connecting to #{base_url}..." } begin with_retry do http.start end rescue StandardError => e logger.warn "Cannot connect to #{base_url}: #{e.message}" raise else logger.warn 'Connection success' end end
connected?()
click to toggle source
# File lib/sqreen/kit/http_client.rb, line 95 def connected? http.started? end
disconnect()
click to toggle source
# File lib/sqreen/kit/http_client.rb, line 99 def disconnect http.finish if connected? end
init_static_headers()
click to toggle source
# File lib/sqreen/kit/http_client.rb, line 103 def init_static_headers platform = RUBY_PLATFORM platform += " #{ENV_JAVA['java.runtime.version']}" if defined?(ENV_JAVA) ua_string = "sqreen-kit/#{Sqreen::Kit.version} " \ "(#{platform}) ruby/#{RUBY_VERSION}" ua_string += " jruby/#{JRUBY_VERSION}" if defined?(JRUBY_VERSION) { 'User-Agent' => ua_string } end
parse_uri(uri)
click to toggle source
@return [URI::HTTP]
# File lib/sqreen/kit/http_client.rb, line 66 def parse_uri(uri) # This regexp is the Ruby constant URI::PATTERN::HOSTNAME augmented # with the _ character that is frequent in Docker linked containers. re = '(?:(?:[a-zA-Z\\d](?:[-_a-zA-Z\\d]*[a-zA-Z\\d])?)\\.)*(?:[a-zA-Z](?:[-_a-zA-Z\\d]*[a-zA-Z\\d])?)\\.?' parser = URI::Parser.new HOSTNAME: re uri += '/' unless uri.end_with? '/' parser.parse(uri) end
process_response(resp)
click to toggle source
@param [Net::HTTPResponse] resp
# File lib/sqreen/kit/http_client.rb, line 147 def process_response(resp) body = resp.body logger.debug { "Body of response: #{body}" } if resp.code == '401' || resp.code == '403' raise AuthenticationError.new(resp.code, body) end unless %w[200 201 202].include?(resp.code) raise UnexpectedStatusError.new(resp.code, body) end { code: resp.code, body: body, } end
request(method, path, data, headers = {})
click to toggle source
# File lib/sqreen/kit/http_client.rb, line 112 def request(method, path, data, headers = {}) connect now = Time.now headers = @static_headers.merge(headers) @req_nb += 1 path = (base_url + path).path with_retry do logger.debug do format('%s %s %s (%s)', method, path, data.inspect, headers.inspect) end resp = case method when :GET http.get(path, headers) when :POST http.post(path, data, headers) end result = process_response resp logger.debug do format('%s %s (DONE: %s in %f ms)', method, path, resp && resp.code, (Time.now - now) * 1000) end result end # end with_retry end
with_retry(&block)
click to toggle source
# File lib/sqreen/kit/http_client.rb, line 75 def with_retry(&block) @retry_policy.execute(&block) end