exrequester v0.0.1 EXRequester

EXRequester requester main class

Summary

Functions

Check that the function was called correctly

Ensure that the http method was defined

Define the function to call

Exception to raise if the function was called incorrectly

Return all the possible attributes to define in the method

Macros

Clear the module attributes after defining the functions

Define a request function

Read the module attribute that defines the headers

Read the module attribute that define the request HTTP method and path defined

Read the module attribute that the query keys

Functions

check_called_correctly(function_name, params, request)

Check that the function was called correctly

Parameters:

  • function_name - the function name to define
  • params - the function parameters used
  • request - EXRequester.request to use
check_has_http_method(arr)

Ensure that the http method was defined

define_catch_error_for_empty(function_name, proposed_function)

Define a catch error function

Parameters:

  • function_name - the function name to define
  • proposed_function - the proposed function to define
define_catch_error_function(arg1, function_name, proposed_function)

Define a catch error function

Parameters:

  • params - the function parameters used
  • function_name - the function name to define
  • proposed_function - the proposed function to define
define_function(arg1, function_name, request)

Define the function to call

Parameters:

  • params - the function parameters used
  • function - the function name to define
  • request - EXRequester.request to use
exception_to_raise(called_function, correct_function)

Exception to raise if the function was called incorrectly

Parameters:

  • called_function - the function called
  • correct_function - the correct function to call
perform_request_and_parse(function_name, params, request)
possible_attributes()

Return all the possible attributes to define in the method

post_defreq(arg)

Macros

clear_attributes()

Clear the module attributes after defining the functions

define_function(defintion_result, params, function, proposed, request)

Define the function to call

Parameters:

  • defintion_result - the function definition result
  • params - the function parameters used
  • function - the function name to define
  • proposed - the proposesd function definition to use
  • request - EXRequester.request to use
defreq(head)

Define a request function

Parameters:

  • head - the function header AST

Example

To define an api:

defmodule SampleAPI do
  use EXRequester

  @headers [
    Authorization: :auth,
    Key1: :key1
  ]
  @get "/path/to/resource/{resource_id}"
  defreq get_resource(resource_id: resource_id, auth: auth, key1: key1)
end

Then to call it:

SampleAPI.client("http://base_url.com")
|> SampleAPI.get_resource(resource_id: 123, auth1: "1", key1: "2")

This will hit http://base_url.com/path/to/resource/123 The Authorization and Key1 headers will also be set.

If you want to decode the response, pass a decoder as a parameter when calling get_resource For example:

  SampleAPI.client("http://base_url.com")
  |> SampleAPI.get_resource(resource_id: 123, auth1: "1", key1: "2", decoder: fn response ->
    "Value is " <> response.body
  end)

The anonymous function passed to decoder will receive an EXRequester.Response. This function can parse the response and return a parsed response. The parsed response will be finally returned.

The example above returns "Value is Content of body"

get_request_headers()

Read the module attribute that defines the headers

get_request_path_and_method()

Read the module attribute that define the request HTTP method and path defined

get_request_query()

Read the module attribute that the query keys