acuity-index-api-rs 0.1.0

High-level Rust client for the acuity-index WebSocket API.
Documentation

acuity-index-api-rs

High-level Rust client for the acuity-index WebSocket API.

The indexer serves JSON-over-WebSocket on ws://127.0.0.1:8172 by default.

Quick Start

Add the crate to your Cargo.toml, connect to the indexer, and issue typed requests with IndexerClient.

Full API reference: API.md

use acuity_index_api_rs::{CustomKey, CustomValue, IndexerClient, Key};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = IndexerClient::connect("ws://127.0.0.1:8172").await?;

    let spans = client.status().await?;
    println!("indexed spans: {spans:?}");

    let events = client
        .get_events(
            Key::Custom(CustomKey {
                name: "ref_index".into(),
                value: CustomValue::U32(42),
            }),
            Some(100),
            None,
        )
        .await?;

    println!("{} matching events", events.events.len());
    for event_match in events.event_matches() {
        if let Some(decoded) = event_match.decoded_event {
            println!("{}::{}", decoded.pallet_name(), decoded.event_name());
        }
    }
    Ok(())
}

Subscription Example

use acuity_index_api_rs::{CustomKey, CustomValue, IndexerClient, Key};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = IndexerClient::connect("ws://127.0.0.1:8172").await?;

    let spans = client.status().await?;
    println!("indexed spans: {spans:?}");

    let mut subscription = client
        .subscribe_events(Key::Custom(CustomKey {
            name: "ref_index".into(),
            value: CustomValue::U32(42),
        }))
        .await?;

    while let Some(notification) = subscription.next().await {
        let notification = notification?;
        if let Some(decoded) = notification.decoded_event {
            println!("{}::{}", decoded.pallet_name(), decoded.event_name());
        }
        break;
    }

    subscription.unsubscribe().await?;
    Ok(())
}

subscribe_events and subscribe_status return owned subscription handles. Dropping a handle removes only that local receiver. Calling unsubscribe() also sends the corresponding unsubscribe request to the server.