Crates.io | firestore_grpc |
lib.rs | firestore_grpc |
version | |
source | src |
created_at | 2020-03-11 08:20:16.139415 |
updated_at | 2024-12-08 00:19:17.913013 |
description | A gRPC client library for Firestore, generated automatically from the API definition files in Google APIs. |
homepage | |
repository | https://github.com/gkkachi/firestore-grpc |
max_upload_size | |
id | 217478 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Firestore client library for Rust.
If you use Cloud Run, please use https://github.com/gkkachi/firestore-grpc-cloudrun instead, which provides some useful functions such as get_client()
, get_project_id()
.
This example is experimental. In a real situation, you need to implement features such as token updates.
export PROJECT_ID=YOUR_PROJECT_ID
export TOKEN=`gcloud auth print-access-token`
Cargo.toml
[dependencies]
firestore_grpc = "0.109"
tokio = {version = "1.0", features = ["full"]}
main.rs
use firestore_grpc::tonic::{
codegen::InterceptedService,
metadata::MetadataValue,
transport::{Channel, ClientTlsConfig},
Request, Status,
};
use firestore_grpc::v1::{
firestore_client::FirestoreClient, value::ValueType, CreateDocumentRequest, Document, Value,
};
use std::env;
const URL: &'static str = "https://firestore.googleapis.com";
const DOMAIN: &'static str = "firestore.googleapis.com";
pub type BoxError = Box<dyn std::error::Error + Sync + Send + 'static>;
fn get_token() -> String {
env::var("TOKEN").unwrap()
}
fn get_project_id() -> String {
env::var("PROJECT_ID").unwrap()
}
async fn get_client() -> Result<FirestoreClient<InterceptedService<Channel, impl Fn(Request<()>) -> Result<Request<()>, Status>>>, BoxError> {
let endpoint = Channel::from_static(URL).tls_config(ClientTlsConfig::new().domain_name(DOMAIN));
let bearer_token = format!("Bearer {}", get_token());
let header_value = MetadataValue::from_str(&bearer_token)?;
let channel = endpoint?.connect().await?;
let service = FirestoreClient::with_interceptor(channel, move |mut req: Request<()>| {
req.metadata_mut()
.insert("authorization", header_value.clone());
Ok(req)
});
Ok(service)
}
async fn create_document() -> Result<Document, BoxError> {
let parent = format!(
"projects/{}/databases/(default)/documents",
get_project_id()
);
let collection_id = "greetings".into();
let document_id = "".into();
let mut fields = std::collections::HashMap::new();
fields.insert(
"message".into(),
Value {
value_type: Some(ValueType::StringValue("Hello world!".into())),
},
);
let document = Some(Document {
name: "".into(),
fields,
create_time: None,
update_time: None,
});
let res = get_client()
.await?
.create_document(CreateDocumentRequest {
parent,
collection_id,
document_id,
document,
mask: None,
})
.await?;
Ok(res.into_inner())
}
#[tokio::main]
async fn main() {
create_document().await.unwrap();
}