Module: Avatax::RequestDecorator

Includes:
SpreeAvataxOfficial::HttpHelper
Defined in:
app/models/avatax/request_decorator.rb

Constant Summary

Constants included from SpreeAvataxOfficial::HttpHelper

SpreeAvataxOfficial::HttpHelper::CONNECTION_ERRORS

Instance Method Summary collapse

Methods included from SpreeAvataxOfficial::HttpHelper

#mock_error_response

Instance Method Details

#request(method, path, model, options = {}, apiversion = "", headers = Hash.new) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/avatax/request_decorator.rb', line 5

def request(method, path, model, options = {}, apiversion="", headers=Hash.new)
  max_retries                  ||= ::SpreeAvataxOfficial::Config.max_retries
  uri_encoded_path               = URI.parse(path).to_s
  response                       = connection.send(method) do |request|
    request.headers['X-Avalara-Client'] = request.headers['X-Avalara-Client'].gsub("API_VERSION", apiversion)
    request.headers=request.headers.merge(headers)  unless headers.empty?
    request.options['timeout'] ||= 1_200
    case method
    when :get, :delete
      request.url("#{uri_encoded_path}?#{URI.encode_www_form(options)}")
    when :post, :put
      request.url("#{uri_encoded_path}?#{URI.encode_www_form(options)}")
      request.headers['Content-Type'] = 'application/json'
      request.body                    = model.to_json unless model.empty?
    end
  end

  if faraday_response
    response
  else
    response.body
  end
rescue *::SpreeAvataxOfficial::HttpHelper::CONNECTION_ERRORS => e
  retry unless (max_retries -= 1).zero?

  mock_error_response(e) # SpreeAvataxOfficial::HttpHelper method
end