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 a catch error function
Define a catch error function
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 the function to call
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 that the function was called correctly
Parameters:
function_name
- the function name to defineparams
- the function parameters usedrequest
-EXRequester.request
to use
Define a catch error function
Parameters:
function_name
- the function name to defineproposed_function
- the proposed function to define
Define a catch error function
Parameters:
params
- the function parameters usedfunction_name
- the function name to defineproposed_function
- the proposed function to define
Define the function to call
Parameters:
params
- the function parameters usedfunction
- the function name to definerequest
-EXRequester.request
to use
Exception to raise if the function was called incorrectly
Parameters:
called_function
- the function calledcorrect_function
- the correct function to call
Macros
Define the function to call
Parameters:
defintion_result
- the function definition resultparams
- the function parameters usedfunction
- the function name to defineproposed
- the proposesd function definition to userequest
-EXRequester.request
to use
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"
Read the module attribute that define the request HTTP method and path defined