reqkey 0.1.0

Official Rust SDK for ReqKey API key validation, credit metering, and analytics
Documentation
//! Warp validation filter and response recording usage.

use std::convert::Infallible;

use reqkey::{
    middleware::{KeyLocation, Middleware, MiddlewareConfig},
    warp::{recover, ReqKeyFilter, WarpRequest},
    Client,
};
use warp::Filter;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::from_env()?;
    let config = MiddlewareConfig::builder("api_payments")
        .key_location(KeyLocation::header("X-StartupName-Key"))
        .capture_query_params(true)
        .build()?;
    let reqkey = ReqKeyFilter::new(Middleware::new(client, config));

    let payments = warp::path("payments")
        .and(warp::post())
        .and(reqkey.filter())
        .and_then(|guard: WarpRequest| async move {
            let reply = warp::reply::json(&serde_json::json!({
                "created": true,
                "credits_remaining": guard
                    .decision()
                    .and_then(|decision| decision.credits_remaining),
            }));
            Ok::<_, Infallible>(guard.record(reply).await)
        })
        .recover(recover);

    warp::serve(payments).run(([127, 0, 0, 1], 3030)).await;
    Ok(())
}