Hui.Http behaviour (Hui v0.10.4) View Source

A behaviour module for handling HTTP requests and responses.

This module is responsible for dispatching Solr request encapsulated in Hui.Http.t/0 struct. It underpins the core functions of Hui, as well as provides default implementation and built-in HTTP client capability based on Erlang httpc.

Using other HTTP clients

Instead of using the built-in client, other HTTP clients may be developed by implementing Hui.Http behaviour and deployed through application configuration. For example, Hui provides another client option - Hui.Http.Httpoison.

  config :hui,
    http_client: Hui.Http.Httpoison

Hui.Http.Httpoison depends on HTTPoison. The dependency needs to be specified in mix.exs. Add :httpoison to the applications section of the mix file to start up the client for runtime.

  defp deps do
    [
      {:httpoison, "~> 1.7"}
    ]
  end

Link to this section Summary

Types

Request or response body which can be in iodata or parsed (as map) format.

The request url in iodata format consisting the full path and encoded query params.

Response tuple from a HTTP request consists of the Hui.Http.t/0 struct and Solr response.

t()

The main request and response data struct.

Functions

Dispatch HTTP request to a Solr endpoint using the built-in Erlang httpc client.

Dispatch HTTP request to a Solr endpoint using a given client implementing the Hui.Http behaviour.

Callbacks

Dispatch HTTP request to a Solr endpoint.

Link to this section Types

Specs

body() :: nil | iodata() | map()

Request or response body which can be in iodata or parsed (as map) format.

Specs

request_url() :: iodata()

The request url in iodata format consisting the full path and encoded query params.

Specs

response() :: {:ok, t()} | {:error, Hui.Error.t()}

Response tuple from a HTTP request consists of the Hui.Http.t/0 struct and Solr response.

Specs

t() :: %Hui.Http{
  body: body(),
  headers: list(),
  method: :get | :post,
  options: keyword(),
  status: nil | integer(),
  url: request_url()
}

The main request and response data struct.

Link to this section Functions

Specs

dispatch(request :: t()) :: response()

Dispatch HTTP request to a Solr endpoint using the built-in Erlang httpc client.

This is a default implementation of dispatch/1 callback based on httpc.

Example

  request = %Hui.Http{
              url: ["http://localhost:8080/solr/select", "?", "q=loch"],
              headers: [{"accept", "application/json"}],
              options: [{:timeout, 1000}]
            }

  {:ok, response} = HTTP.dispatch(request)

Find out more about the available options from httpc documentation.

Link to this function

dispatch(request, client)

View Source

Specs

dispatch(request :: t(), client :: module()) :: response()

Dispatch HTTP request to a Solr endpoint using a given client implementing the Hui.Http behaviour.

Same as dispatch/1 but invoking request through dynamic dispatching. See Hui.Http.Httpoison for a reference client implementation based on HTTPoison that provides additional options such as connection pooling.

Link to this section Callbacks

Link to this callback

dispatch(request)

View Source (optional)

Specs

dispatch(request :: t()) :: response()

Dispatch HTTP request to a Solr endpoint.

This callback is optional and can be used to adapt other HTTP clients to provide different HTTP options and performance. Hui provides Hui.Http.Httpoison, a reference implementation of this callback that can be used in conjunction with dispatch/2.

If the callback is not implemented, the default built-in httpc-based client will be used.