Method: FormAPI::ApiClient#call_api

Defined in:
lib/form_api/api_client.rb

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

Call an API with given options.

Returns:

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

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



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
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/form_api/api_client.rb', line 49

def call_api(http_method, path, opts = {})
  request = build_request(http_method, path, opts)
  response = request.run

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

  unless response.success?
    if response.timed_out?
      fail ApiError.new('Connection timed out')
    elsif response.code == 0
      # Errors from libcurl will be made visible here
      fail ApiError.new(:code => 0,
                        :message => response.return_message)
    else
      exception_message = nil
      response_json = JSON.parse(response.body) rescue nil
      if response_json.is_a? Hash
        if response_json['errors'].is_a? Array
          response_errors = response_json['errors'].join(', ')
        elsif response_json['error']
          response_errors = response_json['error']
        end
        if response_errors
          exception_message = "#{response.status_message}: #{response_errors}"
        end
      end

      unless exception_message
        exception_message = "#{response.status_message}: [Could not parse errors from response body]"
      end

      fail ApiError.new(:code => response.code,
                        :response_headers => response.headers,
                        :response_body => response.body),
           exception_message
    end
  end

  if opts[:return_type]
    data = deserialize(response, opts[:return_type])
  else
    data = nil
  end
  return data, response.code, response.headers
end