Class: ApimaticCalculator::FaradayClient

Inherits:
HttpClient
  • Object
show all
Defined in:
lib/apimatic_calculator/http/faraday_client.rb

Overview

An implementation of HttpClient.

Instance Method Summary collapse

Methods inherited from HttpClient

#delete, #get, #head, #patch, #post, #put

Constructor Details

#initialize(timeout:, max_retries:, retry_interval:, backoff_factor:, cache: false, verify: true) ⇒ FaradayClient

The constructor.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/apimatic_calculator/http/faraday_client.rb', line 13

def initialize(timeout:, max_retries:, retry_interval:,
               backoff_factor:, cache: false, verify: true)
  @connection = Faraday.new do |faraday|
    faraday.use Faraday::HttpCache, serializer: Marshal if cache
    faraday.use FaradayMiddleware::FollowRedirects
    faraday.use :gzip
    faraday.request :multipart
    faraday.request :url_encoded
    faraday.ssl[:ca_file] = Certifi.where
    faraday.ssl[:verify] = verify
    faraday.request :retry, max: max_retries, interval: retry_interval,
                            backoff_factor: backoff_factor
    faraday.adapter Faraday.default_adapter
    faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
    faraday.options[:timeout] = timeout if timeout > 0
  end
end

Instance Method Details

#convert_response(response, http_request) ⇒ Object

Method overridden from HttpClient.



62
63
64
65
# File 'lib/apimatic_calculator/http/faraday_client.rb', line 62

def convert_response(response, http_request)
  HttpResponse.new(response.status, response.reason_phrase,
                   response.headers, response.body, http_request)
end

#execute_as_binary(http_request) ⇒ Object

Method overridden from HttpClient.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/apimatic_calculator/http/faraday_client.rb', line 47

def execute_as_binary(http_request)
  response = @connection.send(
    http_request.http_method.downcase,
    http_request.query_url
  ) do |request|
    request.headers = http_request.headers
    unless http_request.http_method == HttpMethodEnum::GET &&
           http_request.parameters.empty?
      request.body = http_request.parameters
    end
  end
  convert_response(response, http_request)
end

#execute_as_string(http_request) ⇒ Object

Method overridden from HttpClient.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/apimatic_calculator/http/faraday_client.rb', line 32

def execute_as_string(http_request)
  response = @connection.send(
    http_request.http_method.downcase,
    http_request.query_url
  ) do |request|
    request.headers = http_request.headers
    unless http_request.http_method == HttpMethodEnum::GET &&
           http_request.parameters.empty?
      request.body = http_request.parameters
    end
  end
  convert_response(response, http_request)
end