Operate v0.1.0-beta.15 Operate.Adapter behaviour View Source
Operate adapter specification.
An adapter is responsible for loading tapes and ops from a datasource - potentially a web API, a database or even a Bitcoin node. Operate ships with two default adapters, although these can be swapped out with any other adpater by changing the configuration:
children = [
{Operate, [
tape_adapter: Operate.Adapter.Bob,
op_adapter: Operate.Adapter.OpApi
]}
]
Supervisor.start_link(children, strategy: :one_for_one)
Creating an adapter
An adapter must implement one or more of the following callbacks:
fetch_tx/2
- function that takes a txid and returns aOperate.BPU.Transaction.t/0
.fetch_tx_by/2
- function that takes a map and returns a list ofOperate.BPU.Transaction.t/0
tx.fetch_ops/2
- function that takes a list of Op references and returns a list ofOperate.Op.t/0
functions.
Example:
defmodule MyAdapter do
use Operate.Adapter
def fetch_tx(txid, opts) do
key = Keyword.get(opts, :api_key)
BitcoinApi.load_tx(txid, api_key: key)
|> to_bpu
end
defp to_bpu(tx) do
# Map tx object to `Operate.BPU.Transaction.t`
end
end
Using the above example, Operate can be configured with:
{Operate, [
tape_adapter: {MyAdapter, [api_key: "myapikey"]}
]}
Link to this section Summary
Callbacks
Fetches a list of Ops by the given list of Op references. Returns the result
in an :ok
/ :error
tuple pair.
As fetch_ops/2
, but returns the result or raises an exception.
Fetches a transaction by the given txid, and returns the result in an
:ok
/ :error
tuple pair.
As fetch_tx/2
, but returns the transaction or raises an exception.
Fetches a list of transactions by the given query map, and returns the result
in an :ok
/ :error
tuple pair.
As fetch_tx_by/2
, but returns the result or raises an exception.
Link to this section Callbacks
Specs
fetch_ops(list(), keyword()) :: {:ok, [Operate.Op.t(), ...]} | {:error, Exception.t()}
Fetches a list of Ops by the given list of Op references. Returns the result
in an :ok
/ :error
tuple pair.
Specs
fetch_ops!(list(), keyword()) :: [Operate.Op.t(), ...]
As fetch_ops/2
, but returns the result or raises an exception.
Specs
fetch_tx(String.t(), keyword()) :: {:ok, Operate.Tape.t()} | {:error, Exception.t()}
Fetches a transaction by the given txid, and returns the result in an
:ok
/ :error
tuple pair.
Specs
fetch_tx!(String.t(), keyword()) :: Operate.Tape.t()
As fetch_tx/2
, but returns the transaction or raises an exception.
Specs
fetch_tx_by(map(), keyword()) :: {:ok, [Operate.Tape.t(), ...]} | {:error, Exception.t()}
Fetches a list of transactions by the given query map, and returns the result
in an :ok
/ :error
tuple pair.
Specs
fetch_tx_by!(map(), keyword()) :: [Operate.Tape.t(), ...]
As fetch_tx_by/2
, but returns the result or raises an exception.