chore: move billing to client api http (#619)
This commit is contained in:
parent
6471831561
commit
9d3d28ad89
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -160,7 +160,6 @@ members = [
|
|||
# xtask
|
||||
"xtask",
|
||||
"libs/tonic-proto",
|
||||
"libs/billing",
|
||||
]
|
||||
|
||||
[workspace.dependencies]
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
@ -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,14 +107,13 @@ impl BillingClient<'_> {
|
|||
.into_data()?;
|
||||
Ok(portal_url)
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_workspace_limits(
|
||||
client: &client_api::Client,
|
||||
&self,
|
||||
workspace_id: &str,
|
||||
) -> Result<WorkspaceUsageLimit, AppResponseError> {
|
||||
let url = format!("{}/api/workspace/{}/limit", &client.base_url, workspace_id);
|
||||
client
|
||||
let url = format!("{}/api/workspace/{}/limit", &self.base_url, workspace_id);
|
||||
self
|
||||
.http_client_with_auth(Method::GET, &url)
|
||||
.await?
|
||||
.send()
|
||||
|
|
@ -158,3 +123,4 @@ async fn get_workspace_limits(
|
|||
.await?
|
||||
.into_data()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
mod http;
|
||||
mod http_ai;
|
||||
mod http_billing;
|
||||
mod http_blob;
|
||||
mod http_collab;
|
||||
mod http_history;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue