Class: DefaultHttpClient

Inherits:
HttpClient show all
Defined in:
lib/default_http_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HttpClient

#response

Constructor Details

#initialize(config) ⇒ DefaultHttpClient

Returns a new instance of DefaultHttpClient.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/default_http_client.rb', line 16

def initialize(config)
  @config = config
  @session = Faraday.new("") do |f|
    f.request :retry,
              max: config.max_retries,
              interval: config.retry_interval,
              interval_randomness: 0.5,
              backoff_factor: 2
    f.options.timeout = config.connect_timeout
    f.options.open_timeout = config.connection_request_timeout
    f.adapter :net_http_persistent, pool_size: config.pool_size do |http|
      http.idle_timeout = config.pool_idle_timeout
    end
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/default_http_client.rb', line 10

def config
  @config
end

#sessionObject

Returns the value of attribute session.



10
11
12
# File 'lib/default_http_client.rb', line 10

def session
  @session
end

Class Method Details

.create(config) ⇒ Object



12
13
14
# File 'lib/default_http_client.rb', line 12

def self.create(config)
  DefaultHttpClient.new(config)
end

.default_response(status_code, status_message, content_type, content) ⇒ Object



51
52
53
54
55
# File 'lib/default_http_client.rb', line 51

def self.default_response(status_code, status_message, content_type, content)
  env = Faraday::Env.from(status: status_code, body: content || status_message,
                    response_headers: { "Content-Type" => content_type })
  Faraday::Response.new(env)
end

Instance Method Details

#closeObject



47
48
49
# File 'lib/default_http_client.rb', line 47

def close
  @session.close
end

#get(url, query, headers) ⇒ Object

def context_data end



35
36
37
# File 'lib/default_http_client.rb', line 35

def get(url, query, headers)
  @session.get(url, query, headers)
end

#post(url, query, headers, body) ⇒ Object



43
44
45
# File 'lib/default_http_client.rb', line 43

def post(url, query, headers, body)
  @session.post(add_tracking(url, query), body, headers)
end

#put(url, query, headers, body) ⇒ Object



39
40
41
# File 'lib/default_http_client.rb', line 39

def put(url, query, headers, body)
  @session.put(add_tracking(url, query), body, headers)
end