pub mod entitlements;
pub mod packages;
pub mod types;
pub mod wallet;
pub use types::{
BalanceDetail, BalanceDetailEntitlement, BuyResponse, ConsumeRecord, ConsumeRecordPage,
EntitlementBalance, EntitlementItem, ModelBucket, ModelByQuotaResponse, ModelCoefficient,
Order, OrderListItem, OrderStatus, PayPayload, PaymentMethod, TokenPackage, Transaction,
WalletStats,
};
use crate::core::client::Client;
use crate::core::http::DEFAULT_JSON_TIMEOUT_MS;
use crate::shared::{ApiResponse, Error, Result};
use serde::de::DeserializeOwned;
use tokio_util::sync::CancellationToken;
impl Client {
pub(crate) async fn billing_get<T: DeserializeOwned>(
&self,
path: &str,
signal: Option<CancellationToken>,
) -> Result<T> {
let (env, _) = self
.do_json_full::<ApiResponse<T>>(http::Method::GET, path, None, signal)
.await?;
unwrap_billing(path, env)
}
pub(crate) async fn billing_post<T: DeserializeOwned>(
&self,
path: &str,
signal: Option<CancellationToken>,
) -> Result<T> {
self.billing_post_body(path, None, signal).await
}
pub(crate) async fn billing_post_body<T: DeserializeOwned>(
&self,
path: &str,
body: Option<&str>,
signal: Option<CancellationToken>,
) -> Result<T> {
self.billing_post_body_with_timeout(path, body, signal, DEFAULT_JSON_TIMEOUT_MS)
.await
}
pub(crate) async fn billing_post_body_with_timeout<T: DeserializeOwned>(
&self,
path: &str,
body: Option<&str>,
signal: Option<CancellationToken>,
timeout_ms: u64,
) -> Result<T> {
let (bytes, _) = self
.do_json_full_raw(http::Method::POST, path, body, signal, timeout_ms, None)
.await?;
if bytes.is_empty() {
return Err(Error::other(format!("{path}: empty response body")));
}
let env: ApiResponse<T> = serde_json::from_slice(&bytes)
.map_err(|e| Error::other(format!("{path}: decode: {e}")))?;
unwrap_billing(path, Some(env))
}
}
fn unwrap_billing<T>(path: &str, env: Option<ApiResponse<T>>) -> Result<T> {
let env = env.ok_or_else(|| Error::other(format!("{path}: empty response body")))?;
if let Some(err) = env.business_error() {
return Err(err);
}
Ok(env.data)
}