View Source DigexRequest behaviour (DigexRequest v0.2.0)

DigexRequest

A digest access authentication implementation for Elixir. It tries to follow RFC 7616 as much as possible.

installation

Installation

If available in Hex, the package can be installed by adding digex_request to your list of dependencies in mix.exs:

def deps do
  [
    {:digex_request, "~> 0.1.0"}
  ]
end

usage

Usage

To use the digest request, you need to create a module and use the DigexRequest behaviour

def DigexClient do
  use DigexRequest
end

after that, build a request and send it:

DigexRequest.new(:get, "http://www.example.com", "username", "password") |> DigexClient.request()

By default the http client used is :httpc, you can override this behaviour by implementing the handle_request/5 function.

request function accept a keyword list that will be passed as it is to the handle_request/5, this is useful to configure some options like timeout.

For example if we want to use Finch to send the request

def DigexClient do
  use DigexRequest

  @impl DigexRequest
  def handle_request(method, url, headers, body, _opts) do
    case Finch.build(method, url, headers, body) |> Finch.request(MyFinch) do
      {:ok, response} ->
        {:ok, 
          %DigexRequest.Response{
            status: response.status, 
            headers: response.headers, 
            body: response.body}}

      {:error, error} ->
        {:error, error}
    end
  end
end

authorization-header-refresh

Authorization header refresh

Each call made by the client will update the DigexRequest struct, so you need to keep this request in order to reuse the authorization header.

req = DigexRequest.new(:get, "http://www.example.com", "username", "password")
{:ok, resp, req} = DigexClient.request(req)

# re-use the authorization header for another path
req = %DigexRequest{req | url: "http://www.example.com/another/path"}
{:ok, resp, req} = DigexClient.request(req)

Link to this section Summary

Callbacks

Send the query using an http client and map the response to DigexRequest.Reponse struct.

Link to this section Types

@type headers() :: [{header_name :: String.t(), header_value :: String.t()}]
@type method() :: :get | :head | :post | :delete | :put | :patch | :options | :trace
@type options() :: Keyword.t()
@type t() :: %DigexRequest{
  authorization: DigexRequest.Authorization.t() | nil,
  body: iodata(),
  headers: headers(),
  method: method(),
  password: String.t(),
  url: String.t(),
  username: String.t()
}

Link to this section Callbacks

Link to this callback

handle_request(method, t, headers, any, options)

View Source (optional)
@callback handle_request(method(), String.t(), headers(), any(), options()) ::
  {:ok, DigexRequest.Response.t()} | {:error, any()}

Send the query using an http client and map the response to DigexRequest.Reponse struct.

The default implementation use the httpc http client to send requests.

Link to this section Functions

Link to this function

new(method, url, username, password, headers \\ [], body \\ nil)

View Source
@spec new(method(), String.t(), String.t(), String.t(), headers(), iodata() | nil) ::
  t()

Build a digest request to be sent