gRPC schema

View as markdown

Fetch the protobuf schema of the Synthient gRPC API.

With the grpcschema feature, the synthient::grpcschema module fetches the protobuf schema of the Synthient gRPC API using gRPC server reflection.

cargo add synthient --features grpcschema
  • Connection: Unless plaintext is set, the connection uses rustls with the platform's trusted certificates, whichever TLS feature the HTTP client uses.
  • Re-exports: The module re-exports prost, prost_types, and tonic's Code.

fetch

Starts a fetch of the protobuf files that define the server's services, along with all of their imports.

pub fn fetch(api_key: impl Into<String>) -> Fetch
use synthient::grpcschema;

let schema = match grpcschema::fetch(std::env::var("SYNTHIENT_API_KEY")?).await {
    Ok(schema) => schema,
    Err(err) => match err.explain() {
        Some(explanation) => return Err(format!("{explanation} {err}").into()),
        None => return Err(err.into()),
    },
};

println!("endpoint: {}", schema.endpoint);
for symbol in &schema.symbols {
    println!("service: {symbol}");
}
for file in &schema.descriptor_set.file {
    println!("file: {} (package {})", file.name(), file.package());
}
  • Auth: api_key is sent as x-api-key metadata unless it is blank.
  • Network: Nothing is sent until the returned Fetch is awaited, so change its options first.
  • Traits: Fetch implements IntoFuture with Output = Result<Schema, SchemaError>, and the future is Send + 'static.

Fetch

The default fetches every service from DEFAULT_ENDPOINT over TLS within 30 seconds.

pub fn endpoint(self, endpoint: impl Into<String>) -> Self
pub fn plaintext(self, plaintext: bool) -> Self
pub fn symbols<I, S>(self, symbols: I) -> Self
where
    I: IntoIterator<Item = S>,
    S: Into<String>,
pub fn timeout(self, timeout: Duration) -> Self
  • Name
    endpoint
    Description

    Sets the server as host:port, a bare host (port 443 is assumed), or a grpc:// or https:// URL. A blank endpoint uses DEFAULT_ENDPOINT.

  • Name
    plaintext
    Description

    Disables TLS when true, for local development servers.

  • Name
    symbols
    Description

    Resolves these fully qualified service or message names instead of every service the server lists. The listing leaves out the grpc.reflection services, so name one here to fetch its schema. Blank and repeated names are ignored.

  • Name
    timeout
    Description

    Sets how long the whole fetch may take. The default is 30 seconds. A fetch that runs out of time returns a SchemaError with the code Code::DeadlineExceeded.

Schema

The protobuf schema returned by fetch.

  • Name
    endpoint
    Type
    String
    Description

    The normalized address that was queried, as host:port.

  • Name
    symbols
    Type
    Vec<String>
    Description

    The service or message names that were resolved.

  • Name
    descriptor_set
    Type
    FileDescriptorSet
    Description

    The resolved files in dependency order, each file after the files it imports.

FileDescriptorSet is prost_types::FileDescriptorSet. Encode it with prost::Message::encode_to_vec for tools such as grpcurl and buf:

use synthient::grpcschema::{self, prost::Message};

let schema = grpcschema::fetch(std::env::var("SYNTHIENT_API_KEY")?).await?;
std::fs::write("synthient.binpb", schema.descriptor_set.encode_to_vec())?;

SchemaError

A failed schema fetch. Its Display output describes the failure, and source() returns the underlying error when there is one.

  • Name
    code
    Type
    Option<Code>
    Description

    The gRPC status code, or None when the failure was not a gRPC status, such as an invalid endpoint or an unusable reflection reply.

explain

Returns a human-readable explanation of a common failure, or None.

pub fn explain(&self) -> Option<&'static str>
gRPC codeExplanation
UnauthenticatedMissing or invalid API key.
PermissionDeniedThis API key does not have permission to inspect gRPC schemas.
NotFoundNo protobuf schema found for the requested symbol. Fetch without symbols to list the available services.
UnimplementedThe gRPC endpoint does not expose server reflection.
UnavailableCould not reach the gRPC endpoint. Check the address, network access, and TLS settings.
DeadlineExceededTimed out reading protobuf schemas from gRPC reflection. Use a longer timeout.

Constants

  • Name
    DEFAULT_ENDPOINT
    Type
    &str
    Description

    "grpc.synthient.com:443", the address of the Synthient gRPC API.