Terminus v0.1.1 Terminus.HTTP.Client View Source

A GenStage producer module for creating streaming HTTP requests. Each request is a GenStage process, enabling creating powerful concurrent data flows.

Usage

# Create the connection process
iex> Terminus.HTTP.Client.connect("http://x.bitfs.network")
{:ok, pid}

# Begin a request
iex> Terminus.HTTP.Client.request(pid, "GET", path)
:ok

# Stream the result
iex> GenStage.stream([{pid, cancel: :transient}])
%Stream{}

Link to this section Summary

Types

HTTP request status.

t()

Terminus HTTP State.

Functions

Manually stops the Client process, closing it's HTTP request.

Creates a new connection to a given server and returns a GenStage pid.

Manually stops and restarts the client's current request.

Sends a request to the connected server.

Starts the client process without links (outside of a supervision tree).

Starts a client process linked to the current process.

Link to this section Types

Specs

status() :: 0 | 1 | 2

HTTP request status.

  • 0 - Connecting
  • 1 - Connected
  • 2 - Closed

Specs

t() :: %Terminus.HTTP.Client{
  conn: Mint.HTTP.t(),
  decoder: atom(),
  demand: integer(),
  events: list(),
  last_event_id: String.t(),
  recycle_after: integer() | nil,
  recycle_ref: reference(),
  request: Terminus.HTTP.Request.t(),
  request_ref: reference(),
  response: Terminus.HTTP.Response.t(),
  status: status()
}

Terminus HTTP State.

Link to this section Functions

Link to this function

close(pid, reason \\ :shutdown)

View Source

Manually stops the Client process, closing it's HTTP request.

Link to this function

connect(base_url, options \\ [])

View Source

Specs

connect(String.t(), keyword()) :: {:ok, pid()} | {:error, Exception.t()}

Creates a new connection to a given server and returns a GenStage pid.

Options

The accepted options are:

  • :decoder - Request body binary.
  • :recycle_after - Number of seconds after which the request should be recycled if it has recieved no events (used in EventSource connections).

Specs

recycle(pid()) :: :ok

Manually stops and restarts the client's current request.

Alternatively, this can be automated by using the :recycle_after option when calling connect/2.

Link to this function

request(pid, method, path, options \\ [])

View Source

Specs

request(pid(), String.t(), String.t(), keyword()) :: :ok

Sends a request to the connected server.

The function is asynchronous and returns :ok. The GenStage pid can be subscribed to to listen to streaming data chunks.

Options

The accepted options are:

  • :headers - List of HTTP headers in tuple form.
  • :body - Request body binary.
  • :token - If provided, sets the Token HTTP header.
  • :last_event_id - Used with EventSource connections. If provided, sets the Last-Event-ID header.
Link to this function

start(base_url, options \\ [])

View Source

Specs

start(String.t(), keyword()) :: {:ok, pid()} | {:error, Exception.t()}

Starts the client process without links (outside of a supervision tree).

Link to this function

start_link(base_url, options \\ [])

View Source

Specs

start_link(String.t(), keyword()) :: {:ok, pid()} | {:error, Exception.t()}

Starts a client process linked to the current process.

This is often used to start the client process as part of a supervision tree.