HTTPill v0.2.2 HTTPill.Base behaviour View Source
This is the base module for everything you want to do with HTTPill.
Here are the methods that actually read your config, make the requests, parse the response and handle how this response will be returned.
Creating my own API clients
You can also use HTTPill.Base
and customize a lot of functions, creating
this way your own API client. The options you give when using will be applied
to HTTPill.Config
as the default configurations for anyone who uses this
client.
Here is an example for you
defmodule GitHub do
use HTTPill.Base, base_url: "https://api.github.com"
def after_process_response(resp) do
%{resp |
body: resp.body
|> Stream.map(fn ({k, v}) ->
{String.to_atom(k), v}
end)
|> Enum.into(%{})}
end
end
With this module you can make a GitHub
API calls easily with:
GitHub.get("/users/octocat/orgs")
It will make a request to GitHub and return a body with the keys converted to atoms. But be careful with that, since atoms are a limited resource.
Configuring
You can configure your HTTPill.Base
modules passing the opts when you’re
using it, like this:
defmodule Google do
use HTTPill.Base, base_url: "https://api.google.com"
end
Or even on your whole env, by setting this on your config files:
config :httpill, Google, base_url: "https://api.google.com"
Overriding functions
HTTPill.Base
defines a behaviour for the function callbacks that you can
implement. Just give a check on the callback list.
All of this functions are optionally overridable on your extensions.
Link to this section Summary
Functions
Returns the configuration for the given module
Callbacks
Called after processing any request
Called after processing any response (async too)
Called before processing any request
Called before processing any response (async too)
Link to this section Functions
Returns the configuration for the given module
.
This function merges the env configuration on top of the given
default_config
, which is empty by default.
Link to this section Callbacks
after_process_request(HTTPill.Request.t) :: HTTPill.Request.t
Called after processing any request
after_process_response(HTTPill.Response.t | HTTPill.AsyncResponse.any_t) :: HTTPill.Response.t | HTTPill.AsyncResponse.any_t
Called after processing any response (async too)
before_process_request(HTTPill.Request.t) :: HTTPill.Request.t
Called before processing any request
before_process_response(HTTPill.Response.t | HTTPill.AsyncResponse.any_t) :: HTTPill.Response.t | HTTPill.AsyncResponse.any_t
Called before processing any response (async too)