HTTPill v0.2.0 HTTPill.Base 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 lot of functions, all of which can be overridden (by
redefining them). The following list are the ones made specifically to be
overridden:
before_process_request/1
after_process_request/1
before_process_response/1
after_process_response/1
process_any_async_response/1
The names suggest what they stand for, but you can find more info on the docs.
Link to this section Summary
Functions
Returns the configuration for the given module
Link to this section Types
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.