peppermint v0.2.0 Peppermint View Source

Processless HTTP functions build on Mint.

Examples:

{:ok, response} = Peppermint.get("http://httpbin.org/")
{:ok, response} = Peppermint.post("http://httpbin.org/post", params: %{foo: "bar"})

Link to this section Summary

Request

Connect to the host, execute a request and disconnect.

Functions

Execute a request on a (Mint) connection. Response messages will be sent to the caller process

Handle a Mint message. Requires a map as accumulator which contains the requested request_ref's as keys and maps as values. Returns both the accumulator and any done responses.HTTP

Link to this section Types

Specs

request_error() :: {:error, Mint.Types.error() | :receive_timeout}

Link to this section Request

Link to this function

request(method, url, options \\ [])

View Source

Specs

request(atom(), String.t(), keyword()) ::
  {:ok, Peppermint.Response.t()} | request_error()

Connect to the host, execute a request and disconnect.

Link to this section Request Helpers

Link to this function

delete(url, options \\ [])

View Source

Specs

delete(String.t(), keyword()) ::
  {:ok, Peppermint.Response.t()} | request_error()

Specs

Link to this function

head(url, options \\ [])

View Source

Specs

Link to this function

options(url, options \\ [])

View Source

Specs

options(String.t(), keyword()) ::
  {:ok, Peppermint.Response.t()} | request_error()
Link to this function

patch(url, options \\ [])

View Source

Specs

patch(String.t(), keyword()) :: {:ok, Peppermint.Response.t()} | request_error()
Link to this function

post(url, options \\ [])

View Source

Specs

Specs

Link to this function

trace(url, options \\ [])

View Source

Specs

trace(String.t(), keyword()) :: {:ok, Peppermint.Response.t()} | request_error()

Link to this section Functions

Specs

close(Mint.HTTP.t()) :: {:ok, Mint.HTTP.t()}
Link to this function

connect(url, options \\ [])

View Source

Specs

connect(String.t(), keyword()) ::
  {:ok, Mint.HTTP.t()} | {:error, Mint.Types.error()}
connect(URI.t(), keyword()) ::
  {:ok, Mint.HTTP.t()} | {:error, Mint.Types.error()}
Link to this function

connect(scheme, host, port, options)

View Source

Specs

connect(Mint.Types.scheme(), String.t(), :inet.port_number(), keyword()) ::
  {:ok, Mint.HTTP.t()} | {:error, Mint.Types.error()}
Link to this function

execute(conn, method, path, options \\ [])

View Source

Specs

execute(Mint.HTTP.t(), atom(), String.t(), keyword()) ::
  {:ok, Mint.HTTP.t(), Mint.Types.request_ref()}
  | {:error, Mint.HTTP.t(), any()}

Execute a request on a (Mint) connection. Response messages will be sent to the caller process

Link to this function

handle_message(conn, message, acc)

View Source

Handle a Mint message. Requires a map as accumulator which contains the requested request_ref's as keys and maps as values. Returns both the accumulator and any done responses.HTTP

Can be used in a receive loop or for example in a GenServer.

{:ok, conn, request_ref} = Peppermint.request(conn, :get, "/")

acc = %{request_ref => %{}}

When receiving a message:

case handle_message(conn, message, acc) do
  {:ok, conn, {acc, results}} ->
    # check if your request_ref is in the results

  {:error, conn, reason, results} ->
    # error in the connection, you'd probably want to abort here

  {:unknown, conn, acc} ->
    # unknown message, use `Mint.HTTP.is_connection_message` to guard this from hapening
end