Method: APISmith::Client::InstanceMethods#request!

Defined in:
lib/api_smith/client.rb

#request!(method, path, options, *param_types) ⇒ Object

Performs a HTTP request using HTTParty, using a set of expanded options built up by the current client.

Parameters:

  • method (:get, :post, :put, :delete)

    the http request method to use

  • path (String)

    the request path, relative to either the endpoint or /.

  • options (Hash{Symbol => Object})

    the options for the given request.

  • param_types (Array<Symbol>)

    the given parameter types (e.g. :body, :query) to add to the request

Options Hash (options):

  • :skip_endpoint (true, false)

    If true, don't expand the given path before processing.

  • :response_container (Array)

    If present, it will traverse an array of objects to unpack

  • :extra_request (Hash)

    Extra raw, request options to pass in to the request

  • :extra_body (Hash)

    Any parameters to add to the request body

  • :extra_query (Hash)

    Any parameters to add to the request query string

  • :transform (#call)

    An object, invoked via #call, that takes the response and transformers it into a useable form.

Returns:

  • the response, defaults to the raw HTTParty::Response

See Also:

  • #path_for
  • #extract_response
  • #transform_response


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/api_smith/client.rb', line 103

def request!(method, path, options, *param_types)
  # Merge in the default request options, e.g. those to be passed to HTTParty raw
  request_options = merged_options_for(:request, options)
  # Exapdn the path out into a full version when the endpoint is present.
  full_path = request_options[:skip_endpoint] ? path : path_for(path)
  # For each of the given param_types (e.g. :query, :body) will automatically
  # merge in the options for the current request.
  param_types.each do |type|
    request_options[type] = merged_options_for(type, options)
  end
  # Finally, use HTTParty to get the response
  response = instrument_request method, full_path, options do
    self.class.send method, full_path, request_options
  end
  # Pre-process the response to check for errors.
  check_response_errors response
  # Unpack the response using the :response_container option
  inner_response = extract_response path, response, options
  # Finally, apply any transformations
  transform_response inner_response, options
end