withone 0.2.0

Typed Rust client for the One API: the client, webhooks and the action type every withone-<platform> crate builds on.
Documentation
use one::One;
use std::{env::var, error::Error, time::Duration};

/// Registers a webhook listener and dispatches deliveries to typed handlers.
///
/// `listen` is idempotent: it reconciles the listener named `my-app` rather
/// than creating a second one, so running this repeatedly is safe.
///
/// An HTTP server should pass the exact raw body and `X-Webhook-Signature`
/// header to `handle`. Re-serializing parsed JSON breaks signature validation.
///
/// Run:
///   ONE_SECRET=sk_... ONE_WEBHOOK_SECRET=whsec_... \
///     cargo run -p withone --example webhooks
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let one = One::from_env()?;

    let hooks = one
        .webhooks(var("ONE_WEBHOOK_SECRET")?.into())
        .max_event_age(Duration::from_secs(900))
        .connection("live::gmail::abc", |scope| {
            scope
                .connection_deleted(|delivery| async move {
                    println!("gmail connection {} went away", delivery.payload().connection_id);
                })
                .oauth_failed(|delivery| async move {
                    println!("gmail oauth failed: {}", delivery.payload().error);
                })
        })
        .connection_created(|delivery| async move {
            println!("new {} connection: {}", delivery.payload().platform, delivery.payload().key);
        })
        .project_created(|delivery| async move {
            println!("project {} created", delivery.payload().name);
        })
        .on_any(|delivery| async move {
            println!("[{}] {}", delivery.id(), delivery.event());
        });

    let subscription = hooks.listen("my-app", "https://example.com/hooks").await?;
    println!("listening as {} for {:?}", subscription.key, hooks.events());

    let raw_body = br#"{
        "event":"connection.created",
        "event_id":"3fa85f64-5717-4562-b3fc-2c963f66afa6",
        "timestamp":"2026-08-07T00:00:00Z",
        "payload":{},
        "metadata":null
    }"#;
    match hooks.handle(raw_body, "sha256=deadbeef").await {
        Ok(delivery) => println!("dispatched {}", delivery.event()),
        Err(error) => println!("rejected: {error}")
    }

    Ok(())
}