Class: RemoveBg::HttpConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/remove_bg/http_connection.rb

Constant Summary collapse

USER_AGENT =
"remove-bg-ruby-#{RemoveBg::VERSION}".freeze
HTTP_BASE_TIMEOUT =
20
HTTP_WRITE_TIMEOUT =
120

Class Method Summary collapse

Class Method Details

.build(api_url = RemoveBg::Api::URL) ⇒ Faraday::Connection

Returns:

  • (Faraday::Connection)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/remove_bg/http_connection.rb', line 17

def self.build(api_url = RemoveBg::Api::URL)
  retry_options = {
    max: 2,
    interval: 0.2,
    backoff_factor: 2,
    methods: [:post],
    exceptions: Faraday::Retry::Middleware::DEFAULT_EXCEPTIONS + [Faraday::ConnectionFailed],
  }

  request_options = Faraday::RequestOptions.new.tap do |req_options|
    req_options.timeout = HTTP_BASE_TIMEOUT
    req_options.open_timeout = HTTP_BASE_TIMEOUT
    req_options.write_timeout = HTTP_WRITE_TIMEOUT
  end

  http_options = {
    headers: { "User-Agent" => USER_AGENT },
    request: request_options,
  }

  Faraday.new(api_url, http_options) do |f|
    f.request :multipart
    f.request :url_encoded
    f.request :retry, retry_options
    f.adapter :net_http
  end
end