1use serde::{Deserialize, Serialize};
7
8use crate::client::Client;
9use crate::error::Result;
10
11#[derive(Debug, Clone, Deserialize)]
13pub struct CreditPack {
14 pub id: String,
16
17 #[serde(default)]
19 pub label: String,
20
21 #[serde(default)]
23 pub amount_usd: f64,
24
25 #[serde(default)]
27 pub ticks: i64,
28
29 #[serde(default)]
31 pub description: Option<String>,
32
33 #[serde(default)]
35 pub popular: Option<bool>,
36}
37
38#[derive(Debug, Clone, Deserialize)]
40pub struct CreditPacksResponse {
41 pub packs: Vec<CreditPack>,
43}
44
45#[derive(Debug, Clone, Serialize)]
47pub struct CreditPurchaseRequest {
48 pub pack_id: String,
50
51 #[serde(skip_serializing_if = "Option::is_none")]
53 pub success_url: Option<String>,
54
55 #[serde(skip_serializing_if = "Option::is_none")]
57 pub cancel_url: Option<String>,
58}
59
60#[derive(Debug, Clone, Deserialize)]
62pub struct CreditPurchaseResponse {
63 pub checkout_url: String,
65}
66
67#[derive(Debug, Clone, Deserialize)]
69pub struct CreditBalanceResponse {
70 pub balance_ticks: i64,
72
73 pub balance_usd: f64,
75}
76
77#[derive(Debug, Clone, Deserialize)]
79pub struct CreditTier {
80 #[serde(default)]
82 pub name: Option<String>,
83
84 #[serde(default)]
86 pub min_balance: i64,
87
88 #[serde(default)]
90 pub discount_percent: f64,
91
92 #[serde(flatten)]
94 pub extra: serde_json::Value,
95}
96
97#[derive(Debug, Clone, Deserialize)]
99pub struct CreditTiersResponse {
100 pub tiers: Vec<CreditTier>,
102}
103
104#[derive(Debug, Clone, Serialize)]
106pub struct DevProgramApplyRequest {
107 pub use_case: String,
109
110 #[serde(skip_serializing_if = "Option::is_none")]
112 pub company: Option<String>,
113
114 #[serde(skip_serializing_if = "Option::is_none")]
116 pub expected_usd: Option<f64>,
117
118 #[serde(skip_serializing_if = "Option::is_none")]
120 pub website: Option<String>,
121}
122
123#[derive(Debug, Clone, Deserialize)]
125pub struct DevProgramApplyResponse {
126 pub status: String,
128}
129
130impl Client {
131 pub async fn credit_packs(&self) -> Result<CreditPacksResponse> {
133 let (resp, _meta) = self
134 .get_json::<CreditPacksResponse>("/qai/v1/credits/packs")
135 .await?;
136 Ok(resp)
137 }
138
139 pub async fn credit_purchase(&self, req: &CreditPurchaseRequest) -> Result<CreditPurchaseResponse> {
141 let (resp, _meta) = self
142 .post_json::<CreditPurchaseRequest, CreditPurchaseResponse>(
143 "/qai/v1/credits/purchase",
144 req,
145 )
146 .await?;
147 Ok(resp)
148 }
149
150 pub async fn credit_balance(&self) -> Result<CreditBalanceResponse> {
152 let (resp, _meta) = self
153 .get_json::<CreditBalanceResponse>("/qai/v1/credits/balance")
154 .await?;
155 Ok(resp)
156 }
157
158 pub async fn credit_tiers(&self) -> Result<CreditTiersResponse> {
160 let (resp, _meta) = self
161 .get_json::<CreditTiersResponse>("/qai/v1/credits/tiers")
162 .await?;
163 Ok(resp)
164 }
165
166 pub async fn dev_program_apply(&self, req: &DevProgramApplyRequest) -> Result<DevProgramApplyResponse> {
168 let (resp, _meta) = self
169 .post_json::<DevProgramApplyRequest, DevProgramApplyResponse>(
170 "/qai/v1/credits/dev-program",
171 req,
172 )
173 .await?;
174 Ok(resp)
175 }
176}