Class: ApimaticCalculator::FaradayClient

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

Overview

An implementation of HttpClient.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from HttpClient

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

Constructor Details

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

The constructor.



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

def initialize(timeout:, max_retries:, retry_interval:,
               backoff_factor:, retry_statuses:, retry_methods:,
               connection: nil, cache: false, verify: true)
  @connection = if connection.nil?
                  create_connection(timeout: timeout, max_retries: max_retries,
                                    retry_interval: retry_interval, backoff_factor: backoff_factor,
                                    retry_statuses: retry_statuses, retry_methods: retry_methods,
                                    cache: cache, verify: verify)
                else
                  connection
                end
end

Instance Attribute Details

#connectionObject (readonly)

The attribute readers for properties.



13
14
15
# File 'lib/apimatic_calculator/http/faraday_client.rb', line 13

def connection
  @connection
end

Instance Method Details

#convert_response(response, http_request) ⇒ Object

Method overridden from HttpClient.



89
90
91
92
# File 'lib/apimatic_calculator/http/faraday_client.rb', line 89

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

#create_connection(timeout:, max_retries:, retry_interval:, backoff_factor:, retry_statuses:, retry_methods:, cache: false, verify: true) ⇒ Object

Method to initialize connection.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/apimatic_calculator/http/faraday_client.rb', line 30

def create_connection(timeout:, max_retries:, retry_interval:,
                      backoff_factor:, retry_statuses:, retry_methods:,
                      cache: false, verify: true)
  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,
                            retry_statuses: retry_statuses,
                            methods: retry_methods,
                            retry_if: proc { |env, _exc|
                                        env.request.context['forced_retry'] ||= false
                                      }
    faraday.adapter Faraday.default_adapter
    faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
    faraday.options[:timeout] = timeout if timeout.positive?
  end
end

#execute_as_binary(http_request) ⇒ Object

Method overridden from HttpClient.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/apimatic_calculator/http/faraday_client.rb', line 72

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
    request.options.context ||= {}
    request.options.context.merge!(http_request.context)
    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.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/apimatic_calculator/http/faraday_client.rb', line 55

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.map { |k, v| [k.to_s, v.to_s] }
    request.options.context ||= {}
    request.options.context.merge!(http_request.context)
    unless http_request.http_method == HttpMethodEnum::GET &&
           http_request.parameters.empty?
      request.body = http_request.parameters
    end
  end
  convert_response(response, http_request)
end