acosmi/billing/
entitlements.rs1use super::types::{
5 BalanceDetail, ConsumeRecordPage, EntitlementBalance, EntitlementItem, ModelBucket,
6 ModelByQuotaResponse, ModelCoefficient,
7};
8use crate::core::client::Client;
9use crate::core::http::COEF_CACHE_TTL_MS;
10use crate::shared::{Error, Result};
11use tokio_util::sync::CancellationToken;
12
13impl Client {
14 pub async fn get_balance(
16 &self,
17 signal: Option<CancellationToken>,
18 ) -> Result<EntitlementBalance> {
19 self.billing_get("/entitlements/balance", signal).await
20 }
21
22 pub async fn get_balance_detail(
24 &self,
25 signal: Option<CancellationToken>,
26 ) -> Result<BalanceDetail> {
27 self.billing_get("/entitlements/balance-detail", signal)
28 .await
29 }
30
31 pub async fn list_entitlements(
33 &self,
34 status: &str,
35 signal: Option<CancellationToken>,
36 ) -> Result<Vec<EntitlementItem>> {
37 let path = if status.is_empty() {
38 "/entitlements".to_string()
39 } else {
40 format!("/entitlements?status={}", urlencoding(status))
41 };
42 self.billing_get(&path, signal).await
43 }
44
45 pub async fn list_consume_records(
47 &self,
48 page: i64,
49 page_size: i64,
50 signal: Option<CancellationToken>,
51 ) -> Result<ConsumeRecordPage> {
52 let path = format!("/entitlements/consume-records?page={page}&pageSize={page_size}");
53 self.billing_get(&path, signal).await
54 }
55
56 pub async fn claim_monthly_free(
58 &self,
59 signal: Option<CancellationToken>,
60 ) -> Result<EntitlementItem> {
61 self.billing_post("/entitlements/claim-monthly", signal)
62 .await
63 }
64
65 pub async fn get_by_model(
67 &self,
68 model_id: &str,
69 signal: Option<CancellationToken>,
70 ) -> Result<ModelByQuotaResponse> {
71 if model_id.is_empty() {
72 return Err(Error::other("modelID required"));
73 }
74 let path = format!("/entitlements/by-model?modelId={}", urlencoding(model_id));
75 self.billing_get(&path, signal).await
76 }
77
78 pub async fn list_buckets(
80 &self,
81 signal: Option<CancellationToken>,
82 ) -> Result<Vec<ModelBucket>> {
83 self.billing_get("/entitlements/buckets", signal).await
84 }
85
86 pub async fn list_coefficients(
90 &self,
91 signal: Option<CancellationToken>,
92 ) -> Result<Vec<ModelCoefficient>> {
93 {
95 let cache = self.coef_cache().read().unwrap();
96 if let Some((data, t)) = cache.as_ref() {
97 if (t.elapsed().as_millis() as u64) < COEF_CACHE_TTL_MS {
98 return Ok(data.clone()); }
100 }
101 }
102 let data: Vec<ModelCoefficient> = self
103 .billing_get("/entitlements/coefficients", signal)
104 .await?;
105 *self.coef_cache().write().unwrap() = Some((data.clone(), std::time::Instant::now()));
106 Ok(data)
107 }
108
109 pub fn invalidate_coefficient_cache(&self) {
113 *self.coef_cache().write().unwrap() = None;
114 }
115}
116
117pub(crate) fn urlencoding(s: &str) -> String {
119 let mut out = String::with_capacity(s.len());
120 for b in s.bytes() {
121 match b {
122 b'A'..=b'Z'
123 | b'a'..=b'z'
124 | b'0'..=b'9'
125 | b'-'
126 | b'_'
127 | b'.'
128 | b'!'
129 | b'~'
130 | b'*'
131 | b'\''
132 | b'('
133 | b')' => out.push(b as char),
134 _ => out.push_str(&format!("%{b:02X}")),
135 }
136 }
137 out
138}