chore: move billing to client api http (#619)

This commit is contained in:
Zack 2024-06-13 14:38:58 +08:00 committed by GitHub
parent 6471831561
commit 9d3d28ad89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 35 additions and 93 deletions

12
Cargo.lock generated
View File

@ -1212,18 +1212,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "billing"
version = "0.1.0"
dependencies = [
"client-api",
"reqwest 0.11.27",
"serde",
"serde_json",
"shared-entity",
"tokio",
]
[[package]] [[package]]
name = "bincode" name = "bincode"
version = "1.3.3" version = "1.3.3"

View File

@ -160,7 +160,6 @@ members = [
# xtask # xtask
"xtask", "xtask",
"libs/tonic-proto", "libs/tonic-proto",
"libs/billing",
] ]
[workspace.dependencies] [workspace.dependencies]

View File

@ -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 }

View File

@ -1,35 +1,18 @@
pub mod entities; use crate::Client;
use crate::entities::WorkspaceUsageLimit;
use client_api::error::AppResponseError;
use entities::{RecurringInterval, SubscriptionPlan, WorkspaceSubscriptionStatus, WorkspaceUsage};
use reqwest::Method; use reqwest::Method;
use serde_json::json; use serde_json::json;
use shared_entity::response::AppResponse; use shared_entity::{
dto::billing_dto::{
pub struct BillingClient<'a> { RecurringInterval, SubscriptionPlan, WorkspaceSubscriptionStatus, WorkspaceUsage,
billing_base_url: String, WorkspaceUsageLimit,
client: &'a client_api::Client, },
} response::{AppResponse, AppResponseError},
};
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;
}
impl Client {
pub async fn customer_id(&self) -> Result<String, AppResponseError> { pub async fn customer_id(&self) -> Result<String, AppResponseError> {
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 let resp = self
.client
.http_client_with_auth(Method::GET, &url) .http_client_with_auth(Method::GET, &url)
.await? .await?
.send() .send()
@ -47,13 +30,8 @@ impl BillingClient<'_> {
workspace_subscription_plan: SubscriptionPlan, workspace_subscription_plan: SubscriptionPlan,
success_url: &str, success_url: &str,
) -> Result<String, AppResponseError> { ) -> Result<String, AppResponseError> {
let url = format!( let url = format!("{}/billing/api/v1/subscription-link", &self.base_url,);
"{}/billing/api/v1/subscription-link",
&self.billing_base_url,
);
let resp = self let resp = self
.client
.http_client_with_auth(Method::GET, &url) .http_client_with_auth(Method::GET, &url)
.await? .await?
.query(&[ .query(&[
@ -74,12 +52,8 @@ impl BillingClient<'_> {
} }
pub async fn cancel_subscription(&self, workspace_id: &str) -> Result<(), AppResponseError> { pub async fn cancel_subscription(&self, workspace_id: &str) -> Result<(), AppResponseError> {
let url = format!( let url = format!("{}/billing/api/v1/cancel-subscription", &self.base_url);
"{}/billing/api/v1/cancel-subscription",
&self.billing_base_url,
);
let resp = self let resp = self
.client
.http_client_with_auth(Method::POST, &url) .http_client_with_auth(Method::POST, &url)
.await? .await?
.json(&json!({ "workspace_id": workspace_id })) .json(&json!({ "workspace_id": workspace_id }))
@ -91,12 +65,8 @@ impl BillingClient<'_> {
pub async fn list_subscription( pub async fn list_subscription(
&self, &self,
) -> Result<Vec<WorkspaceSubscriptionStatus>, AppResponseError> { ) -> Result<Vec<WorkspaceSubscriptionStatus>, AppResponseError> {
let url = format!( let url = format!("{}/billing/api/v1/subscription-status", &self.base_url,);
"{}/billing/api/v1/subscription-status",
&self.billing_base_url
);
let resp = self let resp = self
.client
.http_client_with_auth(Method::GET, &url) .http_client_with_auth(Method::GET, &url)
.await? .await?
.send() .send()
@ -107,13 +77,13 @@ impl BillingClient<'_> {
.into_data() .into_data()
} }
pub async fn get_workspace_usage( pub async fn get_billing_workspace_usage(
&self, &self,
workspace_id: &str, workspace_id: &str,
) -> Result<WorkspaceUsage, AppResponseError> { ) -> Result<WorkspaceUsage, AppResponseError> {
let num_members = self.client.get_workspace_members(workspace_id).await?.len(); let num_members = self.get_workspace_members(workspace_id).await?.len();
let limits = get_workspace_limits(self.client, workspace_id).await?; let limits = self.get_workspace_limits(workspace_id).await?;
let doc_usage = self.client.get_workspace_usage(workspace_id).await?; let doc_usage = self.get_workspace_usage(workspace_id).await?;
let workspace_usage = WorkspaceUsage { let workspace_usage = WorkspaceUsage {
member_count: num_members, member_count: num_members,
@ -125,12 +95,8 @@ impl BillingClient<'_> {
} }
pub async fn get_portal_session_link(&self) -> Result<String, AppResponseError> { pub async fn get_portal_session_link(&self) -> Result<String, AppResponseError> {
let url = format!( let url = format!("{}/billing/api/v1/portal-session-link", &self.base_url,);
"{}/billing/api/v1/portal-session-link",
&self.billing_base_url,
);
let portal_url = self let portal_url = self
.client
.http_client_with_auth(Method::GET, &url) .http_client_with_auth(Method::GET, &url)
.await? .await?
.send() .send()
@ -141,20 +107,20 @@ impl BillingClient<'_> {
.into_data()?; .into_data()?;
Ok(portal_url) Ok(portal_url)
} }
}
async fn get_workspace_limits( async fn get_workspace_limits(
client: &client_api::Client, &self,
workspace_id: &str, workspace_id: &str,
) -> Result<WorkspaceUsageLimit, AppResponseError> { ) -> Result<WorkspaceUsageLimit, AppResponseError> {
let url = format!("{}/api/workspace/{}/limit", &client.base_url, workspace_id); let url = format!("{}/api/workspace/{}/limit", &self.base_url, workspace_id);
client self
.http_client_with_auth(Method::GET, &url) .http_client_with_auth(Method::GET, &url)
.await? .await?
.send() .send()
.await? .await?
.error_for_status()? .error_for_status()?
.json::<AppResponse<WorkspaceUsageLimit>>() .json::<AppResponse<WorkspaceUsageLimit>>()
.await? .await?
.into_data() .into_data()
}
} }

View File

@ -1,5 +1,6 @@
mod http; mod http;
mod http_ai; mod http_ai;
mod http_billing;
mod http_blob; mod http_blob;
mod http_collab; mod http_collab;
mod http_history; mod http_history;

View File

@ -1,5 +1,6 @@
pub mod ai_dto; pub mod ai_dto;
pub mod auth_dto; pub mod auth_dto;
pub mod billing_dto;
pub mod history_dto; pub mod history_dto;
pub mod search_dto; pub mod search_dto;
pub mod workspace_dto; pub mod workspace_dto;