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}"
HTTP_BASE_TIMEOUT =
10
HTTP_WRITE_TIMEOUT =
120

Class Method Summary collapse

Class Method Details

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

Returns:

  • (Faraday::Connection)


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

def self.build(api_url = RemoveBg::Api::URL)
  retry_options = {
    max: 2,
    interval: 0.2,
    backoff_factor: 2,
    methods: [:post],
    exceptions: Faraday::Request::Retry::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