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",
]
[[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"

View File

@ -160,7 +160,6 @@ members = [
# xtask
"xtask",
"libs/tonic-proto",
"libs/billing",
]
[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::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<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
.client
.http_client_with_auth(Method::GET, &url)
.await?
.send()
@ -47,13 +30,8 @@ impl BillingClient<'_> {
workspace_subscription_plan: SubscriptionPlan,
success_url: &str,
) -> Result<String, AppResponseError> {
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<Vec<WorkspaceSubscriptionStatus>, 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<WorkspaceUsage, AppResponseError> {
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<String, AppResponseError> {
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<WorkspaceUsageLimit, AppResponseError> {
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::<AppResponse<WorkspaceUsageLimit>>()
.await?
.into_data()
async fn get_workspace_limits(
&self,
workspace_id: &str,
) -> Result<WorkspaceUsageLimit, AppResponseError> {
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::<AppResponse<WorkspaceUsageLimit>>()
.await?
.into_data()
}
}

View File

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

View File

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