diff --git a/Cargo.lock b/Cargo.lock index 637d6d82..b7b76e91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1212,18 +1212,6 @@ dependencies = [ "serde", ] -[[package]] -name = "billing" -version = "0.1.0" -dependencies = [ - "client-api", - "reqwest 0.11.27", - "serde", - "serde_json", - "shared-entity", - "tokio", -] - [[package]] name = "bincode" version = "1.3.3" diff --git a/Cargo.toml b/Cargo.toml index 2bd46d00..537892c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -160,7 +160,6 @@ members = [ # xtask "xtask", "libs/tonic-proto", - "libs/billing", ] [workspace.dependencies] diff --git a/libs/billing/Cargo.toml b/libs/billing/Cargo.toml deleted file mode 100644 index c9a3cb94..00000000 --- a/libs/billing/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "billing" -version = "0.1.0" -edition = "2021" - -[dependencies] -client-api = { path = "../client-api" } -shared-entity = { path = "../shared-entity" } - -reqwest = { workspace = true } -serde = { workspace = true } -serde_json = { workspace = true } -tokio = { workspace = true } diff --git a/libs/billing/src/lib.rs b/libs/client-api/src/http_billing.rs similarity index 55% rename from libs/billing/src/lib.rs rename to libs/client-api/src/http_billing.rs index 177ccbd0..81b64bcb 100644 --- a/libs/billing/src/lib.rs +++ b/libs/client-api/src/http_billing.rs @@ -1,35 +1,18 @@ -pub mod entities; -use crate::entities::WorkspaceUsageLimit; -use client_api::error::AppResponseError; -use entities::{RecurringInterval, SubscriptionPlan, WorkspaceSubscriptionStatus, WorkspaceUsage}; +use crate::Client; use reqwest::Method; use serde_json::json; -use shared_entity::response::AppResponse; - -pub struct BillingClient<'a> { - billing_base_url: String, - client: &'a client_api::Client, -} - -impl<'a> From<&'a client_api::Client> for BillingClient<'a> { - fn from(client: &'a client_api::Client) -> Self { - Self { - billing_base_url: client.base_url.clone(), - client, - } - } -} - -impl BillingClient<'_> { - pub fn set_billing_base_url(&mut self, billing_base_url: String) { - self.billing_base_url = billing_base_url; - } +use shared_entity::{ + dto::billing_dto::{ + RecurringInterval, SubscriptionPlan, WorkspaceSubscriptionStatus, WorkspaceUsage, + WorkspaceUsageLimit, + }, + response::{AppResponse, AppResponseError}, +}; +impl Client { pub async fn customer_id(&self) -> Result { - let url = format!("{}/billing/api/v1/customer-id", &self.billing_base_url,); - + let url = format!("{}/billing/api/v1/customer-id", &self.base_url); let resp = self - .client .http_client_with_auth(Method::GET, &url) .await? .send() @@ -47,13 +30,8 @@ impl BillingClient<'_> { workspace_subscription_plan: SubscriptionPlan, success_url: &str, ) -> Result { - let url = format!( - "{}/billing/api/v1/subscription-link", - &self.billing_base_url, - ); - + let url = format!("{}/billing/api/v1/subscription-link", &self.base_url,); let resp = self - .client .http_client_with_auth(Method::GET, &url) .await? .query(&[ @@ -74,12 +52,8 @@ impl BillingClient<'_> { } pub async fn cancel_subscription(&self, workspace_id: &str) -> Result<(), AppResponseError> { - let url = format!( - "{}/billing/api/v1/cancel-subscription", - &self.billing_base_url, - ); + let url = format!("{}/billing/api/v1/cancel-subscription", &self.base_url); let resp = self - .client .http_client_with_auth(Method::POST, &url) .await? .json(&json!({ "workspace_id": workspace_id })) @@ -91,12 +65,8 @@ impl BillingClient<'_> { pub async fn list_subscription( &self, ) -> Result, AppResponseError> { - let url = format!( - "{}/billing/api/v1/subscription-status", - &self.billing_base_url - ); + let url = format!("{}/billing/api/v1/subscription-status", &self.base_url,); let resp = self - .client .http_client_with_auth(Method::GET, &url) .await? .send() @@ -107,13 +77,13 @@ impl BillingClient<'_> { .into_data() } - pub async fn get_workspace_usage( + pub async fn get_billing_workspace_usage( &self, workspace_id: &str, ) -> Result { - let num_members = self.client.get_workspace_members(workspace_id).await?.len(); - let limits = get_workspace_limits(self.client, workspace_id).await?; - let doc_usage = self.client.get_workspace_usage(workspace_id).await?; + let num_members = self.get_workspace_members(workspace_id).await?.len(); + let limits = self.get_workspace_limits(workspace_id).await?; + let doc_usage = self.get_workspace_usage(workspace_id).await?; let workspace_usage = WorkspaceUsage { member_count: num_members, @@ -125,12 +95,8 @@ impl BillingClient<'_> { } pub async fn get_portal_session_link(&self) -> Result { - let url = format!( - "{}/billing/api/v1/portal-session-link", - &self.billing_base_url, - ); + let url = format!("{}/billing/api/v1/portal-session-link", &self.base_url,); let portal_url = self - .client .http_client_with_auth(Method::GET, &url) .await? .send() @@ -141,20 +107,20 @@ impl BillingClient<'_> { .into_data()?; Ok(portal_url) } -} -async fn get_workspace_limits( - client: &client_api::Client, - workspace_id: &str, -) -> Result { - let url = format!("{}/api/workspace/{}/limit", &client.base_url, workspace_id); - client - .http_client_with_auth(Method::GET, &url) - .await? - .send() - .await? - .error_for_status()? - .json::>() - .await? - .into_data() + async fn get_workspace_limits( + &self, + workspace_id: &str, + ) -> Result { + let url = format!("{}/api/workspace/{}/limit", &self.base_url, workspace_id); + self + .http_client_with_auth(Method::GET, &url) + .await? + .send() + .await? + .error_for_status()? + .json::>() + .await? + .into_data() + } } diff --git a/libs/client-api/src/lib.rs b/libs/client-api/src/lib.rs index 36901b6a..4cd7823b 100644 --- a/libs/client-api/src/lib.rs +++ b/libs/client-api/src/lib.rs @@ -1,5 +1,6 @@ mod http; mod http_ai; +mod http_billing; mod http_blob; mod http_collab; mod http_history; diff --git a/libs/billing/src/entities.rs b/libs/shared-entity/src/dto/billing_dto.rs similarity index 100% rename from libs/billing/src/entities.rs rename to libs/shared-entity/src/dto/billing_dto.rs diff --git a/libs/shared-entity/src/dto/mod.rs b/libs/shared-entity/src/dto/mod.rs index 2e0d37a8..34d98890 100644 --- a/libs/shared-entity/src/dto/mod.rs +++ b/libs/shared-entity/src/dto/mod.rs @@ -1,5 +1,6 @@ pub mod ai_dto; pub mod auth_dto; +pub mod billing_dto; pub mod history_dto; pub mod search_dto; pub mod workspace_dto;