class RemoveBg::HttpConnection

Constants

HTTP_BASE_TIMEOUT
HTTP_WRITE_TIMEOUT
USER_AGENT

Public Class Methods

build(api_url = RemoveBg::Api::URL) click to toggle source

@return [Faraday::Connection]

# 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