| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
use serde::{Deserialize, Serialize}; |
| 11 |
|
| 12 |
use crate::error::Result; |
| 13 |
use super::SyncKitClient; |
| 14 |
use super::helpers::check_response; |
| 15 |
|
| 16 |
|
| 17 |
#[derive(Debug, Clone, Default, Serialize, Deserialize)] |
| 18 |
pub struct SubscriptionStatus { |
| 19 |
|
| 20 |
pub active: bool, |
| 21 |
|
| 22 |
|
| 23 |
#[serde(rename = "tier")] |
| 24 |
pub interval: Option<String>, |
| 25 |
|
| 26 |
pub status: Option<String>, |
| 27 |
|
| 28 |
pub storage_limit_bytes: Option<i64>, |
| 29 |
|
| 30 |
|
| 31 |
#[serde(default)] |
| 32 |
pub pending_storage_limit_bytes: Option<i64>, |
| 33 |
|
| 34 |
pub storage_used_bytes: Option<i64>, |
| 35 |
|
| 36 |
pub current_period_end: Option<String>, |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 43 |
pub struct AppPricing { |
| 44 |
pub app_name: String, |
| 45 |
|
| 46 |
pub min_charge_cents: i64, |
| 47 |
|
| 48 |
|
| 49 |
pub per_gb_tenths_of_cent_per_month: i64, |
| 50 |
|
| 51 |
pub annual_multiplier: i64, |
| 52 |
pub min_cap_bytes: i64, |
| 53 |
pub max_cap_bytes: i64, |
| 54 |
} |
| 55 |
|
| 56 |
impl AppPricing { |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
pub fn quote_cents(&self, cap_bytes: i64, interval: BillingInterval) -> i64 { |
| 61 |
let cap_bytes = cap_bytes.clamp(self.min_cap_bytes, self.max_cap_bytes); |
| 62 |
let gib = cap_bytes_to_gib_ceil(cap_bytes); |
| 63 |
let storage_monthly = (gib * self.per_gb_tenths_of_cent_per_month + 9) / 10; |
| 64 |
let monthly = storage_monthly.max(self.min_charge_cents); |
| 65 |
match interval { |
| 66 |
BillingInterval::Monthly => monthly, |
| 67 |
BillingInterval::Annual => monthly * self.annual_multiplier, |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
fn cap_bytes_to_gib_ceil(cap_bytes: i64) -> i64 { |
| 73 |
const GIB: i64 = 1024 * 1024 * 1024; |
| 74 |
(cap_bytes + GIB - 1) / GIB |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 79 |
pub enum BillingInterval { |
| 80 |
Monthly, |
| 81 |
Annual, |
| 82 |
} |
| 83 |
|
| 84 |
impl BillingInterval { |
| 85 |
pub fn as_str(self) -> &'static str { |
| 86 |
match self { |
| 87 |
Self::Monthly => "monthly", |
| 88 |
Self::Annual => "annual", |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
pub fn from_str(s: &str) -> Self { |
| 95 |
match s { |
| 96 |
"annual" => Self::Annual, |
| 97 |
_ => Self::Monthly, |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
#[derive(Debug, Deserialize)] |
| 104 |
pub struct CheckoutResponse { |
| 105 |
|
| 106 |
pub checkout_url: String, |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 111 |
pub struct AccountInfo { |
| 112 |
|
| 113 |
pub email: String, |
| 114 |
|
| 115 |
pub username: String, |
| 116 |
} |
| 117 |
|
| 118 |
#[derive(Debug, Serialize, Deserialize)] |
| 119 |
pub struct PriceQuote { |
| 120 |
pub cap_bytes: i64, |
| 121 |
pub interval: String, |
| 122 |
pub price_cents: i64, |
| 123 |
} |
| 124 |
|
| 125 |
#[derive(Debug, Serialize)] |
| 126 |
struct AppPricingRequest<'a> { |
| 127 |
api_key: &'a str, |
| 128 |
} |
| 129 |
|
| 130 |
#[derive(Debug, Serialize)] |
| 131 |
struct QuoteRequest { |
| 132 |
cap_bytes: i64, |
| 133 |
interval: &'static str, |
| 134 |
} |
| 135 |
|
| 136 |
#[derive(Debug, Serialize)] |
| 137 |
struct CheckoutRequest { |
| 138 |
cap_bytes: i64, |
| 139 |
interval: &'static str, |
| 140 |
} |
| 141 |
|
| 142 |
#[derive(Debug, Serialize)] |
| 143 |
struct CapChangeRequest { |
| 144 |
cap_bytes: i64, |
| 145 |
} |
| 146 |
|
| 147 |
impl SyncKitClient { |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
pub async fn get_app_pricing(&self) -> Result<AppPricing> { |
| 152 |
let resp = self.http |
| 153 |
.post(&self.endpoints.app_pricing) |
| 154 |
.json(&AppPricingRequest { api_key: &self.config.api_key }) |
| 155 |
.send() |
| 156 |
.await?; |
| 157 |
let resp = check_response(resp).await?; |
| 158 |
Ok(resp.json().await?) |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
pub async fn quote_price(&self, cap_bytes: i64, interval: BillingInterval) -> Result<PriceQuote> { |
| 165 |
let token = self.require_token()?; |
| 166 |
let resp = self.http |
| 167 |
.post(&self.endpoints.subscription_quote) |
| 168 |
.bearer_auth(token.as_str()) |
| 169 |
.json(&QuoteRequest { cap_bytes, interval: interval.as_str() }) |
| 170 |
.send() |
| 171 |
.await?; |
| 172 |
let resp = check_response(resp).await?; |
| 173 |
Ok(resp.json().await?) |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
pub async fn get_account_info(&self) -> Result<AccountInfo> { |
| 180 |
let token = self.require_token()?; |
| 181 |
|
| 182 |
let resp = self.http |
| 183 |
.get(&self.endpoints.account) |
| 184 |
.bearer_auth(token.as_str()) |
| 185 |
.send() |
| 186 |
.await?; |
| 187 |
|
| 188 |
let resp = check_response(resp).await?; |
| 189 |
let info: AccountInfo = resp.json().await?; |
| 190 |
Ok(info) |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
pub async fn get_subscription_status(&self) -> Result<SubscriptionStatus> { |
| 198 |
let token = self.require_token()?; |
| 199 |
|
| 200 |
let resp = self.http |
| 201 |
.get(&self.endpoints.subscription) |
| 202 |
.bearer_auth(token.as_str()) |
| 203 |
.send() |
| 204 |
.await?; |
| 205 |
|
| 206 |
let resp = check_response(resp).await?; |
| 207 |
let status: SubscriptionStatus = resp.json().await?; |
| 208 |
Ok(status) |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
pub async fn create_subscription_checkout( |
| 215 |
&self, |
| 216 |
cap_bytes: i64, |
| 217 |
interval: BillingInterval, |
| 218 |
) -> Result<CheckoutResponse> { |
| 219 |
let token = self.require_token()?; |
| 220 |
|
| 221 |
let resp = self.http |
| 222 |
.post(&self.endpoints.subscription_checkout) |
| 223 |
.bearer_auth(token.as_str()) |
| 224 |
.json(&CheckoutRequest { cap_bytes, interval: interval.as_str() }) |
| 225 |
.send() |
| 226 |
.await?; |
| 227 |
|
| 228 |
let resp = check_response(resp).await?; |
| 229 |
let checkout: CheckoutResponse = resp.json().await?; |
| 230 |
Ok(checkout) |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
pub async fn queue_storage_cap_change(&self, cap_bytes: i64) -> Result<SubscriptionStatus> { |
| 237 |
let token = self.require_token()?; |
| 238 |
|
| 239 |
let resp = self.http |
| 240 |
.post(&self.endpoints.subscription_storage_cap) |
| 241 |
.bearer_auth(token.as_str()) |
| 242 |
.json(&CapChangeRequest { cap_bytes }) |
| 243 |
.send() |
| 244 |
.await?; |
| 245 |
|
| 246 |
let resp = check_response(resp).await?; |
| 247 |
let status: SubscriptionStatus = resp.json().await?; |
| 248 |
Ok(status) |
| 249 |
} |
| 250 |
} |
| 251 |
|