From c28d1471bf40a1e0c05c4003fc283d81ba37b0e7 Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Fri, 13 Sep 2024 14:29:47 +0200 Subject: [PATCH] feat: default project id (#191) * feat: default project id * fix: clippy * fix: use match --- src/db/mod.rs | 12 ++++++++++++ src/db/options.rs | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/src/db/mod.rs b/src/db/mod.rs index e3c4b94..31aa83c 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -114,6 +114,18 @@ impl FirestoreDb { .await } + pub async fn for_default_project_id() -> FirestoreResult { + match FirestoreDbOptions::for_default_project_id().await { + Some(options) => Self::with_options(options).await, + _ => Err(FirestoreError::InvalidParametersError( + FirestoreInvalidParametersError::new(FirestoreInvalidParametersPublicDetails::new( + "google_project_id".to_string(), + "Unable to retrieve google_project_id".to_string(), + )), + )), + } + } + pub async fn with_options_service_account_key_file( options: FirestoreDbOptions, service_account_key_path: std::path::PathBuf, diff --git a/src/db/options.rs b/src/db/options.rs index ffd5276..24fad80 100644 --- a/src/db/options.rs +++ b/src/db/options.rs @@ -1,3 +1,4 @@ +use gcloud_sdk::GoogleEnvironment; use rsb_derive::Builder; #[derive(Debug, Eq, PartialEq, Clone, Builder)] @@ -13,4 +14,12 @@ pub struct FirestoreDbOptions { pub firebase_api_url: Option, } +impl FirestoreDbOptions { + pub async fn for_default_project_id() -> Option { + let google_project_id = GoogleEnvironment::detect_google_project_id().await; + + google_project_id.map(FirestoreDbOptions::new) + } +} + pub const FIREBASE_DEFAULT_DATABASE_ID: &str = "(default)";