Packages
goth
1.3.0-rc.0
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.1
1.3.0
1.3.0-rc.5
1.3.0-rc.4
1.3.0-rc.3
1.3.0-rc.2
1.3.0-rc.1
1.3.0-rc.0
1.2.0
1.1.0
1.0.1
1.0.1-beta
1.0.0
0.11.1
0.11.0
0.10.0
0.9.0
0.8.2
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.0
0.5.1
0.5.0
0.4.0
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
0.0.3
0.0.2
0.0.1
A simple library to generate and retrieve Oauth2 tokens for use with Google Cloud Service accounts.
Current section
Files
Jump to
Current section
Files
lib/goth/http_client.ex
defmodule Goth.HTTPClient do
@moduledoc """
Specification for a Goth HTTP client.
The client is configured as a `{module, initial_state}` tuple where the module
implements this behaviour and `initial_state` is returned by the `c:init/1`
callback.
The `c:init/1` callback gives an opportunity to perform some initialization tasks just once.
"""
@type method() :: atom()
@type url() :: binary()
@type status() :: non_neg_integer()
@type header() :: {binary(), binary()}
@type body() :: binary()
@type initial_state() :: map()
@doc """
Callback to initialize the given HTTP client.
The returned `initial_state` needs to be a map and will be given to `c:request/6`.
"""
@callback init(opts :: keyword()) :: initial_state()
@doc """
Callback to make an HTTP request.
"""
@callback request(method(), url(), [header()], body(), opts :: keyword(), initial_state()) ::
{:ok, %{status: status, headers: [header()], body: body()}}
| {:error, Exception.t()}
@doc false
def init({module, opts}) when is_atom(module) and is_list(opts) do
initial_state = module.init(opts)
unless is_map(initial_state) do
raise "#{inspect(module)}.init/1 must return a map, got: #{inspect(initial_state)}"
end
{module, initial_state}
end
@doc false
def request({module, initial_state}, method, url, headers, body, opts) do
module.request(method, url, headers, body, opts, initial_state)
end
end