diff --git a/Cargo.lock b/Cargo.lock index 5efc9a3e..fd24c714 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1886,6 +1886,7 @@ dependencies = [ "getrandom 0.2.12", "gotrue", "infra", + "lazy_static", "mime", "parking_lot 0.12.1", "percent-encoding", diff --git a/libs/client-api/Cargo.toml b/libs/client-api/Cargo.toml index 54150512..9442d025 100644 --- a/libs/client-api/Cargo.toml +++ b/libs/client-api/Cargo.toml @@ -46,6 +46,7 @@ serde_urlencoded = "0.7.1" futures.workspace = true pin-project = "1.1.5" percent-encoding = "2.3.1" +lazy_static = { workspace = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] tokio-retry = "0.3" @@ -72,4 +73,4 @@ test_util = ["scraper"] template = ["workspace-template"] sync_verbose_log = ["collab-rt-protocol/verbose_log"] test_fast_sync = [] -enable_brotli = ["brotli"] \ No newline at end of file +enable_brotli = ["brotli"] diff --git a/libs/client-api/src/http_billing.rs b/libs/client-api/src/http_billing.rs index 7612b48e..36a779bd 100644 --- a/libs/client-api/src/http_billing.rs +++ b/libs/client-api/src/http_billing.rs @@ -9,9 +9,23 @@ use shared_entity::{ response::{AppResponse, AppResponseError}, }; +lazy_static::lazy_static! { + static ref BASE_BILLING_URL: Option = match std::env::var("APPFLOWY_CLOUD_BASE_BILLING_URL") { + Ok(url) => Some(url), + Err(err) => { + tracing::warn!("std::env::var(APPFLOWY_CLOUD_BASE_BILLING_URL): {}", err); + None + }, + }; +} + impl Client { + pub fn base_billing_url(&self) -> &str { + BASE_BILLING_URL.as_deref().unwrap_or(&self.base_url) + } + pub async fn customer_id(&self) -> Result { - let url = format!("{}/billing/api/v1/customer-id", &self.base_url); + let url = format!("{}/billing/api/v1/customer-id", self.base_billing_url()); let resp = self .http_client_with_auth(Method::GET, &url) .await? @@ -30,7 +44,10 @@ impl Client { workspace_subscription_plan: SubscriptionPlan, success_url: &str, ) -> Result { - let url = format!("{}/billing/api/v1/subscription-link", &self.base_url,); + let url = format!( + "{}/billing/api/v1/subscription-link", + self.base_billing_url() + ); let resp = self .http_client_with_auth(Method::GET, &url) .await? @@ -52,7 +69,10 @@ impl Client { } pub async fn cancel_subscription(&self, workspace_id: &str) -> Result<(), AppResponseError> { - let url = format!("{}/billing/api/v1/cancel-subscription", &self.base_url); + let url = format!( + "{}/billing/api/v1/cancel-subscription", + self.base_billing_url() + ); let resp = self .http_client_with_auth(Method::POST, &url) .await? @@ -65,7 +85,10 @@ impl Client { pub async fn list_subscription( &self, ) -> Result, AppResponseError> { - let url = format!("{}/billing/api/v1/subscription-status", &self.base_url,); + let url = format!( + "{}/billing/api/v1/subscription-status", + self.base_billing_url(), + ); let resp = self .http_client_with_auth(Method::GET, &url) .await? @@ -95,7 +118,10 @@ impl Client { } pub async fn get_portal_session_link(&self) -> Result { - let url = format!("{}/billing/api/v1/portal-session-link", &self.base_url,); + let url = format!( + "{}/billing/api/v1/portal-session-link", + self.base_billing_url() + ); let portal_url = self .http_client_with_auth(Method::GET, &url) .await? @@ -112,7 +138,11 @@ impl Client { &self, workspace_id: &str, ) -> Result { - let url = format!("{}/api/workspace/{}/limit", &self.base_url, workspace_id); + let url = format!( + "{}/api/workspace/{}/limit", + self.base_billing_url(), + workspace_id + ); self .http_client_with_auth(Method::GET, &url) .await?