Terminus v0.0.2 Terminus.Streamer View Source

A mixin module providing functions for creating streaming HTTP requests.

Examples

defmodule BitFS do
  use Terminus.Streamer, host: "x.bitfs.network"

  def load_file(path) do
    case stream("GET", path, nil) do
      {:ok, stream} ->
        stream |> Enum.to_list |> Enum.join
      {:error, reason} ->
        raise reason
    end
  end
end

Link to this section Summary

Types

On-data callback function.

Functions

Runs the given streaming Enumerable.t/0 and executes the callback on each element of the stream.

Normalizes the given Bitquery, automatically expanding shorthand queries.

Opens a HTTP connection and begins a request, returning the GenStage pid of the request.

Opens a HTTP connection and begins a request, returning a streaming Enumerable.t/0.

Link to this section Types

Link to this type

callback()

View Source
callback() :: function() | nil

On-data callback function.

Link to this section Functions

Link to this function

handle_callback(stream, ondata)

View Source
handle_callback(Enumerable.t(), callback()) ::
  {:ok, Enumerable.t()} | :ok | {:error, String.t()}

Runs the given streaming Enumerable.t/0 and executes the callback on each element of the stream.

If the given callback is nil then the stream is passed through and returned.

Link to this function

normalize_query(query)

View Source
normalize_query(map()) :: map()

Normalizes the given Bitquery, automatically expanding shorthand queries.

Examples

iex> normalize_query(%{find: %{"tx.h" => "abc"}})
%{
  v: 3,
  q: %{
    find: %{
      "tx.h" => "abc"
    }
  }
}
Link to this function

request(method, host, path, body, opts \\ [])

View Source
request(String.t(), String.t(), String.t(), String.t() | nil, keyword()) ::
  {:ok, pid()} | {:error, String.t()}

Opens a HTTP connection and begins a request, returning the GenStage pid of the request.

Returns the result in an :ok / :error tuple pair.

Link to this function

stream(method, host, path, body, opts \\ [])

View Source
stream(String.t(), String.t(), String.t(), String.t() | nil, keyword()) ::
  {:ok, Enumerable.t() | pid()} | {:error, String.t()}

Opens a HTTP connection and begins a request, returning a streaming Enumerable.t/0.

Returns the result in an :ok / :error tuple pair.