Terminus v0.1.1 Terminus.HTTPStream View Source

A mixin module providing functions for creating streaming HTTP requests.

Examples

defmodule BitFS do
  use Terminus.HTTPStream, hosts: ["x.bitfs.network"]

  def load_file(path) do
    case request(:fetch, "GET", path, nil) do
      {:ok, data} ->
        data
      {:error, reason} ->
        raise reason
    end
  end

  def stream_file(path) do
    case request(:stream, "GET", path, nil) do
      {:ok, stream} ->
        stream
      {:error, reason} ->
        raise reason
    end
  end
end

Link to this section Summary

Functions

Opens a HTTP connection and begins a request, returning the binary/0 body of the response.

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 Functions

Link to this function

fetch(method, host, path, options \\ [])

View Source

Specs

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

Opens a HTTP connection and begins a request, returning the binary/0 body of the response.

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

Link to this function

handle_callback(stream, ondata)

View Source

Specs

handle_callback(Enumerable.t(), Terminus.Streamer.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.

Specs

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, options \\ [])

View Source

Specs

request(String.t(), String.t(), String.t(), keyword()) ::
  {:ok, Enumerable.t() | pid()} | {:error, Exception.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, options \\ [])

View Source

Specs

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

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

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