#grpc-client #grpc #grpc-api #spicedb #authzed #zed

bin+lib spicedb-grpc

Auto-generated Rust gRPC client for SpiceDB

2 releases

0.1.1 Aug 10, 2024
0.1.0 Aug 3, 2024

#443 in Network programming

Download history 18/week @ 2024-08-18 230/week @ 2024-08-25 956/week @ 2024-09-01 1635/week @ 2024-09-08 1246/week @ 2024-09-15 1476/week @ 2024-09-22 2598/week @ 2024-09-29 1157/week @ 2024-10-06 1440/week @ 2024-10-13 2835/week @ 2024-10-20 3283/week @ 2024-10-27 2342/week @ 2024-11-03 3128/week @ 2024-11-10 1595/week @ 2024-11-17 1836/week @ 2024-11-24 1924/week @ 2024-12-01

8,500 downloads per month
Used in spicedb-client

Apache-2.0

160KB
2K SLoC

spicedb-grpc

Auto-generated Rust client for the SpiceDB gRPC API.

Features

  • Asynchronous gRPC client for SpiceDB via Tonic
  • Generated from the official AuthZed SpiceDB protobuf definitions

Installation

Add this to your Cargo.toml:

[dependencies]
spicedb-grpc = "0.1.1"

Usage

use spicedb_client::authzed::api::v1::permissions_service_client::PermissionsServiceClient;
use spicedb_client::authzed::api::v1::CheckPermissionRequest;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let spicedb_url =
        env::var("SPICEDB_URL").unwrap_or_else(|_| "http://localhost:50051".to_string());

    let preshared_key =
        env::var("SPICEDB_PRESHARED_KEY").unwrap_or_else(|_| "spicedb".to_string());
    let preshared_key: MetadataValue<_> = format!("bearer {preshared_key}").parse().unwrap();

    let channel = Channel::from_shared(spicedb_url)
        .unwrap()
        .connect()
        .await
        .unwrap();

    let interceptor = move |mut req: Request<()>| {
        req.metadata_mut()
            .insert("authorization", preshared_key.clone());
        Ok(req)
    };

    let mut client =
        PermissionsServiceClient::with_interceptor(channel.clone(), interceptor.clone());

    let request = CheckPermissionRequest {
        consistency: None,
        resource: Some(ObjectReference {
            object_type: "document".to_string(),
            object_id: "mydoc".to_string(),
        }),
        permission: "read".to_string(),
        subject: Some(SubjectReference {
            object: Some(ObjectReference {
                object_type: "user".to_string(),
                object_id: "me".to_string(),
            }),
            optional_relation: "".to_string(),
        }),
        context: None,
        with_tracing: false,
    };

    let response = client.check_permission(request).await.unwrap();

    println!("Response: {:?}", response);

    Ok(())
}

Documentation

See the Buf package documentation for buf.build/authzed/api.

Developing

First, install buf.

brew install bufbuild/buf/buf

Then download the proto files.

buf export buf.build/authzed/api -o proto

And generate the Rust code from the proto.

cargo run --features=gen gen

If any new files are generated, update lib.rs to include them.

Contributing

We welcome contributions!

License

This project is licensed under Apache 2.0.

Acknowledgements

This client is based on the official AuthZed SpiceDB API and uses Tonic for to generate the gRPC client.

Dependencies

~9–18MB
~242K SLoC