Skip to content

Commit

Permalink
Support for provided external token source
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence committed Jan 1, 2024
1 parent df2ec78 commit 5a3f966
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ caching-persistent = ["caching", "dep:redb"]

[dependencies]
tracing = "0.1"
gcloud-sdk = { version = "0.23", features = ["google-firestore-v1"] }
gcloud-sdk = { version = "0.24", features = ["google-firestore-v1"] }
hyper = { version ="0.14" }
struct-path = "0.2"
rvstruct = "0.3.2"
Expand Down Expand Up @@ -71,4 +71,3 @@ required-features = ["caching-memory"]
name = "caching_persistent_test"
path = "tests/caching_persistent_test.rs"
required-features = ["caching-persistent"]

78 changes: 78 additions & 0 deletions examples/token_auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use chrono::{DateTime, Utc};
use firestore::*;
use futures::stream::BoxStream;
use futures::TryStreamExt;
use serde::{Deserialize, Serialize};
use std::ops::Add;

pub fn config_env_var(name: &str) -> Result<String, String> {
std::env::var(name).map_err(|e| format!("{}: {}", name, e))
}

// Example structure to play with
#[derive(Debug, Clone, Deserialize, Serialize)]
struct MyTestStructure {
some_id: String,
some_string: String,
one_more_string: String,
some_num: u64,
created_at: DateTime<Utc>,
}

async fn my_token() -> gcloud_sdk::error::Result<gcloud_sdk::Token> {
Ok(gcloud_sdk::Token::new(
"Bearer".to_string(),
gcloud_sdk::SecretValue::from(config_env_var("TOKEN_VALUE")?),
chrono::Utc::now().add(std::time::Duration::from_secs(3600)),
))
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// Logging with debug enabled
let subscriber = tracing_subscriber::fmt()
.with_env_filter("firestore=debug")
.finish();
tracing::subscriber::set_global_default(subscriber)?;

// Create an instance
let db = FirestoreDb::with_options_token_source(
FirestoreDbOptions::new(config_env_var("PROJECT_ID")?),
gcloud_sdk::GCP_DEFAULT_SCOPES.clone(),
gcloud_sdk::TokenSourceType::ExternalSource(Box::new(
gcloud_sdk::ExternalJwtFunctionSource::new(my_token),
)),
)
.await?;

const TEST_COLLECTION_NAME: &'static str = "test-query";

// Query as a stream our data
let object_stream: BoxStream<FirestoreResult<MyTestStructure>> = db
.fluent()
.select()
.fields(
paths!(MyTestStructure::{some_id, some_num, some_string, one_more_string, created_at}),
)
.from(TEST_COLLECTION_NAME)
.filter(|q| {
q.for_all([
q.field(path!(MyTestStructure::some_num)).is_not_null(),
q.field(path!(MyTestStructure::some_string)).eq("Test"),
Some("Test2")
.and_then(|value| q.field(path!(MyTestStructure::one_more_string)).eq(value)),
])
})
.order_by([(
path!(MyTestStructure::some_num),
FirestoreQueryDirection::Descending,
)])
.obj()
.stream_query_with_errors()
.await?;

let as_vec: Vec<MyTestStructure> = object_stream.try_collect().await?;
println!("{:?}", as_vec);

Ok(())
}

0 comments on commit 5a3f966

Please sign in to comment.