raxx v0.17.6 Raxx.SimpleClient

A very simple HTTP/1.1 client.

This client makes very few assumptions about how to send requests. i.e. Each request is sent over a new connection, no HTTP/1.1 pipelining.

This makes it ideal for testing servers

Usage

Build a request using the helpers available in Raxx module.

request = Raxx.request(:GET, "http://example.com")
|> Raxx.set_header("accept", "text/html")

Send the request asynchronously

A channel is returned to be used to then wait for a response when needed.

channel = Raxx.SimpleClient.send_async(request)

Wait for a response on the channel.

{:ok, response} = Raxx.SimpleClient.yield(channel, 2_000)

Send the request synchronously

{:ok, response} = Raxx.SimpleClient.send_sync(request, 2_000)

Link to this section Summary

Types

A reference to a running client process that can be used to yield or shutdown the request

Functions

Returns a specification to start this module under a supervisor

Send a request over a new channel

Send a request and wait for the response

Shutdown a running channel

Await the response from the given channel

Link to this section Types

Link to this type channel()
channel() :: %Raxx.SimpleClient.Channel{
  caller: pid(),
  client: pid(),
  reference: reference(),
  request: Raxx.Request.t()
}

A reference to a running client process that can be used to yield or shutdown the request

Link to this section Functions

Link to this function child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function send_async(request)
send_async(Raxx.Request.t()) :: channel()

Send a request over a new channel.

NOTE: Request streaming is not supported, so the request sent must be complete, i.e. have a full binary body or no body.

Link to this function send_sync(request, timeout)

Send a request and wait for the response.

This function handles shutting down the client in case of a timeout.

Link to this function shutdown(channel, timeout)
shutdown(channel(), integer()) ::
  {:ok, Raxx.Response.t() | nil} | {:error, pid()}

Shutdown a running channel.

If the response was already available when client is shutdown then it is returned. This can be used if a response is wanted immediatly.

channel = Raxx.SimpleClient.send_async(request)
# Do something slow
{:ok, maybe_response} = Raxx.SimpleClient.shutdown(request, 1000)
if response do
  # Look, a response
end

NOTE: timeout is allowed maximum for process to exit. It is not time waiting for a response.

Link to this function yield(channel, timeout)
yield(channel(), integer()) ::
  {:ok, Raxx.Response.t()} | {:error, :timeout | {:exit, term()}}

Await the response from the given channel.

NOTE: A response can only be yielded once, it checks the process mailbox for the response.

NOTE: If yielding returns {:error, :timeout} then a response could be received in the future. To ensure in this case that there is no message left in the mailbox, run shutdown/2 after yield.