Method: MoneyKit::ApiClient#call_api

Defined in:
lib/moneykit/api_client.rb

#call_api(http_method, path, opts = {}) ⇒ Array<(Object, Integer, Hash)>

Call an API with given options.

Returns:

  • (Array<(Object, Integer, Hash)>)

    an array of 3 elements: the data deserialized from response body (could be nil), response status code and response headers.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/moneykit/api_client.rb', line 48

def call_api(http_method, path, opts = {})
  stream = nil
  begin
    response = connection(opts).public_send(http_method.to_sym.downcase) do |req|
      build_request(http_method, path, req, opts)
      stream = download_file(request) if opts[:return_type] == 'File' || opts[:return_type] == 'Binary'
    end

    config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n" if config.debugging

    unless response.success?
      if response.status == 0 && response.respond_to?(:return_message)
        # Errors from libcurl will be made visible here
        raise ApiError.new(code: 0,
                           message: response.return_message)
      else
        raise ApiError.new(code: response.status,
                           response_headers: response.headers,
                           response_body: response.body),
              response.reason_phrase
      end
    end
  rescue Faraday::TimeoutError
    raise ApiError, 'Connection timed out'
  rescue Faraday::ConnectionFailed
    raise ApiError, 'Connection failed'
  end

  data = if opts[:return_type] == 'File' || opts[:return_type] == 'Binary'
           deserialize_file(stream)
         elsif opts[:return_type]
           deserialize(response, opts[:return_type])
         end
  [data, response.status, response.headers]
end