Exth.Transport.Transportable protocol (Exth v0.4.2)
View SourceProtocol defining the interface for JSON-RPC transport implementations.
This protocol enables different transport mechanisms (HTTP, WebSocket, IPC, etc.) to be used interchangeably for making JSON-RPC requests.
Implementing the Protocol
To implement a new transport, you need to define both new/2
and call/2
:
defimpl Transportable, for: MyTransport do
def new(transport, opts) do
# Initialize your transport with the given options
end
def call(transport, request) do
# Make the RPC request and return the response
end
end
Example Usage
# Create a new transport instance
transport = Transportable.new(
%MyTransport{},
rpc_url: "https://example.com/rpc",
)
# Make an RPC request
{:ok, response} = Transportable.call(transport, request)
Summary
Functions
Makes a request using the configured transport.
Creates a new transport instance with the given options.
Types
@type call_response() :: :ok | {:ok, String.t()} | {:error, error_reason()}
@type error_reason() :: Exception.t() | String.t() | term()
@type t() :: term()
All the types that implement this protocol.
Functions
@spec call(t(), String.t()) :: call_response()
Makes a request using the configured transport.
Parameters
transport
- The configured transport structrequest
- Encoded JSON-RPC request
Returns
{:ok, response}
- Successful request with encoded response{:error, reason}
- Request failed with error reason (Exception.t() or map())
Creates a new transport instance with the given options.
Parameters
transport
- The transport struct to initializeopts
- Keyword list of options specific to the transport implementation
Returns
- The initialized transport struct
Example
transport = Transportable.new(
%MyTransport{},
rpc_url: "https://example.com/rpc",
)