Factom API Client
Asynchronous rust client for the Factom API. Full documentation is available here. Serialisation is handled by serde and the futures runtime is handled by tokio.
Installation
Add to cargo.toml:
[]
= "1.0.2"
Quickstart
use *;
// Create new api handler for factomd/walletd on the localhost on ports 8088 and 8089 respectively
let api = new;
// Methods return a future to be spawned onto a runtime or synchronously fetched
let request = api.properties;
// Fetch is a blocking helper method to get the result of a future
// Http or Json errors raise here.
// API errors such as invalid method or parameters will passed along in the response to be handled later
let response = fetch.expect;
dbg!;
/*
Response {
jsonrpc: "2.0",
id: 0,
result: result(
Object(
{
"factomdapiversion": String(
"2.0"
),
"factomdversion": String(
"6.1.0"
)
}
)
)
}
*/
Usage
use *;
Synchronous call from remote host
// For https calls use Factom::from_https_host()
let api = from_host;
let request = api.heights;
let response = fetch.unwrap;
dbg!;
Different hosts or ports for factomd and walletd
// Factomd open node and walletd locally on port 3003. Id is included in the json-rpc call.
let api = Factom
let request = api.wallet_balances;
let response = fetch.unwrap;
dbg!;
Synchronous call using Tokio block_on and custom json-rpc id
// Create Tokio runtime
let mut runtime = new.expect;
let api = new;
let entryhash = "6ecd7c6c40d0e9dbb52457343e083d4306c5b4cd2d6e623ba67cf9d18b39faa7";
// Add custom id
let entry_query = api.entry
.set_id;
// block_on waits for future to return result
let response = runtime.block_on.unwrap;
dbg!;
// Shutdown runtime once idle
shutdown;
Asynchronous requests using futures
let mut runtime = new.unwrap;
let api = new;
// Closure to parse heights response
let height_handler = ;
// Main heights request
let heights_query = api.heights
// Handle successful execution of future
.map
// Print FetchError info if it occurs
.map_err;
// Closure to print entry content
let entry_handler = ;
// Main entry request
let entryhash = "6ecd7c6c40d0e9dbb52457343e083d4306c5b4cd2d6e623ba67cf9d18b39faa7";
let entry_query = api.entry
.map
.map_err;
// Spawn queries into current runtime
runtime.spawn;
runtime.spawn;
shutdown;
Passing messages between async tasks
use oneshot;
let mut runtime = new.unwrap;
let api = from_host;
// Oneshot used for passing single values, use mpsc to pass streams.
let = ;
// Heights query transmits the response data
let heights = api.heights
.map
.map_err;
// Spawn into current runtime
runtime.spawn;
// Reciever prints out response
runtime.spawn;
shutdown;
Testing
A custom database (~700kb) is located in /tests/env/ and needs to moved into the ~/.factom folder. If factomd/walletd are not run locally, you will need to modify the HOST variable in tests/mod.rs, see the tests readme for more information.