| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
use bytes::Bytes; |
| 11 |
use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
| 12 |
|
| 13 |
use super::SyncKitClient; |
| 14 |
use super::helpers::{Idempotency, check_response}; |
| 15 |
use crate::error::Result; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Serialize, Deserialize)] |
| 26 |
#[serde(transparent)] |
| 27 |
pub struct Cents(pub i64); |
| 28 |
|
| 29 |
impl Cents { |
| 30 |
|
| 31 |
pub const fn get(self) -> i64 { |
| 32 |
self.0 |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
#[must_use] |
| 37 |
pub fn saturating_add(self, other: Cents) -> Cents { |
| 38 |
Cents(self.0.saturating_add(other.0)) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
#[must_use] |
| 43 |
pub fn saturating_mul(self, factor: i64) -> Cents { |
| 44 |
Cents(self.0.saturating_mul(factor)) |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
#[must_use] |
| 49 |
pub fn max(self, other: Cents) -> Cents { |
| 50 |
Cents(self.0.max(other.0)) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
impl std::fmt::Display for Cents { |
| 55 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 56 |
write!(f, "{}", self.0) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
#[derive(Debug, Clone, Default, Serialize, Deserialize)] |
| 62 |
#[non_exhaustive] |
| 63 |
pub struct SubscriptionStatus { |
| 64 |
|
| 65 |
pub active: bool, |
| 66 |
|
| 67 |
|
| 68 |
#[serde(rename = "tier")] |
| 69 |
pub interval: Option<BillingInterval>, |
| 70 |
|
| 71 |
pub status: Option<String>, |
| 72 |
|
| 73 |
pub storage_limit_bytes: Option<i64>, |
| 74 |
|
| 75 |
|
| 76 |
#[serde(default)] |
| 77 |
pub pending_storage_limit_bytes: Option<i64>, |
| 78 |
|
| 79 |
pub storage_used_bytes: Option<i64>, |
| 80 |
|
| 81 |
pub current_period_end: Option<String>, |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 88 |
#[non_exhaustive] |
| 89 |
pub struct AppPricing { |
| 90 |
pub app_name: String, |
| 91 |
|
| 92 |
pub min_charge_cents: Cents, |
| 93 |
|
| 94 |
|
| 95 |
pub per_gb_tenths_of_cent_per_month: i64, |
| 96 |
|
| 97 |
pub annual_multiplier: i64, |
| 98 |
pub min_cap_bytes: i64, |
| 99 |
pub max_cap_bytes: i64, |
| 100 |
} |
| 101 |
|
| 102 |
impl AppPricing { |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
pub fn quote_cents(&self, cap_bytes: i64, interval: BillingInterval) -> Cents { |
| 112 |
let cap_bytes = cap_bytes.clamp(self.min_cap_bytes, self.max_cap_bytes); |
| 113 |
let gib = cap_bytes_to_gib_ceil(cap_bytes); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
let storage_monthly = Cents( |
| 118 |
gib.saturating_mul(self.per_gb_tenths_of_cent_per_month) |
| 119 |
.saturating_add(9) |
| 120 |
/ 10, |
| 121 |
); |
| 122 |
let monthly = storage_monthly.max(self.min_charge_cents); |
| 123 |
match interval { |
| 124 |
BillingInterval::Monthly => monthly, |
| 125 |
BillingInterval::Annual => monthly.saturating_mul(self.annual_multiplier), |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
fn cap_bytes_to_gib_ceil(cap_bytes: i64) -> i64 { |
| 131 |
const GIB: i64 = 1024 * 1024 * 1024; |
| 132 |
cap_bytes.saturating_add(GIB - 1) / GIB |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 142 |
pub enum BillingInterval { |
| 143 |
Monthly, |
| 144 |
Annual, |
| 145 |
} |
| 146 |
|
| 147 |
impl Serialize for BillingInterval { |
| 148 |
fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> { |
| 149 |
serializer.serialize_str(self.as_str()) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
impl<'de> Deserialize<'de> for BillingInterval { |
| 154 |
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> { |
| 155 |
let s = String::deserialize(deserializer)?; |
| 156 |
Ok(BillingInterval::from_wire(&s)) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
impl BillingInterval { |
| 161 |
pub fn as_str(self) -> &'static str { |
| 162 |
match self { |
| 163 |
Self::Monthly => "monthly", |
| 164 |
Self::Annual => "annual", |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
pub fn from_wire(s: &str) -> Self { |
| 173 |
match s { |
| 174 |
"annual" => Self::Annual, |
| 175 |
other => { |
| 176 |
if !other.is_empty() && other != "monthly" { |
| 177 |
tracing::debug!(tag = %other, "unknown billing interval tag; defaulting to monthly"); |
| 178 |
} |
| 179 |
Self::Monthly |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
#[derive(Debug, Deserialize)] |
| 187 |
#[non_exhaustive] |
| 188 |
pub struct CheckoutResponse { |
| 189 |
|
| 190 |
pub checkout_url: String, |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 195 |
#[non_exhaustive] |
| 196 |
pub struct AccountInfo { |
| 197 |
|
| 198 |
pub email: String, |
| 199 |
|
| 200 |
pub username: String, |
| 201 |
} |
| 202 |
|
| 203 |
#[derive(Debug, Serialize, Deserialize)] |
| 204 |
#[non_exhaustive] |
| 205 |
pub struct PriceQuote { |
| 206 |
pub cap_bytes: i64, |
| 207 |
pub interval: BillingInterval, |
| 208 |
pub price_cents: Cents, |
| 209 |
} |
| 210 |
|
| 211 |
#[derive(Debug, Serialize)] |
| 212 |
struct AppPricingRequest<'a> { |
| 213 |
api_key: &'a str, |
| 214 |
} |
| 215 |
|
| 216 |
#[derive(Debug, Serialize)] |
| 217 |
struct QuoteRequest { |
| 218 |
cap_bytes: i64, |
| 219 |
interval: &'static str, |
| 220 |
} |
| 221 |
|
| 222 |
#[derive(Debug, Serialize)] |
| 223 |
struct CheckoutRequest { |
| 224 |
cap_bytes: i64, |
| 225 |
interval: &'static str, |
| 226 |
} |
| 227 |
|
| 228 |
#[derive(Debug, Serialize)] |
| 229 |
struct CapChangeRequest { |
| 230 |
cap_bytes: i64, |
| 231 |
} |
| 232 |
|
| 233 |
impl SyncKitClient { |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
#[tracing::instrument(skip(self))] |
| 238 |
pub async fn get_app_pricing(&self) -> Result<AppPricing> { |
| 239 |
let body = Bytes::from(serde_json::to_vec(&AppPricingRequest { |
| 240 |
api_key: &self.config.api_key, |
| 241 |
})?); |
| 242 |
self.retry_request_json(Idempotency::ReadOnly, || { |
| 243 |
let req = self |
| 244 |
.http |
| 245 |
.post(&self.endpoints.app_pricing) |
| 246 |
.header("content-type", "application/json") |
| 247 |
.body(body.clone()); |
| 248 |
async move { check_response(req.send().await?).await } |
| 249 |
}) |
| 250 |
.await |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
#[tracing::instrument(skip(self))] |
| 257 |
pub async fn quote_price( |
| 258 |
&self, |
| 259 |
cap_bytes: i64, |
| 260 |
interval: BillingInterval, |
| 261 |
) -> Result<PriceQuote> { |
| 262 |
let token = self.require_token()?; |
| 263 |
let body = Bytes::from(serde_json::to_vec(&QuoteRequest { |
| 264 |
cap_bytes, |
| 265 |
interval: interval.as_str(), |
| 266 |
})?); |
| 267 |
self.retry_request_json(Idempotency::ReadOnly, || { |
| 268 |
let req = self |
| 269 |
.http |
| 270 |
.post(&self.endpoints.subscription_quote) |
| 271 |
.bearer_auth(token.as_str()) |
| 272 |
.header("content-type", "application/json") |
| 273 |
.body(body.clone()); |
| 274 |
async move { check_response(req.send().await?).await } |
| 275 |
}) |
| 276 |
.await |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
#[tracing::instrument(skip(self))] |
| 283 |
pub async fn get_account_info(&self) -> Result<AccountInfo> { |
| 284 |
let token = self.require_token()?; |
| 285 |
self.retry_request_json(Idempotency::ReadOnly, || { |
| 286 |
let req = self |
| 287 |
.http |
| 288 |
.get(&self.endpoints.account) |
| 289 |
.bearer_auth(token.as_str()); |
| 290 |
async move { check_response(req.send().await?).await } |
| 291 |
}) |
| 292 |
.await |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
#[tracing::instrument(skip(self))] |
| 300 |
pub async fn get_subscription_status(&self) -> Result<SubscriptionStatus> { |
| 301 |
let token = self.require_token()?; |
| 302 |
self.retry_request_json(Idempotency::ReadOnly, || { |
| 303 |
let req = self |
| 304 |
.http |
| 305 |
.get(&self.endpoints.subscription) |
| 306 |
.bearer_auth(token.as_str()); |
| 307 |
async move { check_response(req.send().await?).await } |
| 308 |
}) |
| 309 |
.await |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
#[tracing::instrument(skip(self))] |
| 316 |
pub async fn create_subscription_checkout( |
| 317 |
&self, |
| 318 |
cap_bytes: i64, |
| 319 |
interval: BillingInterval, |
| 320 |
) -> Result<CheckoutResponse> { |
| 321 |
let token = self.require_token()?; |
| 322 |
let body = Bytes::from(serde_json::to_vec(&CheckoutRequest { |
| 323 |
cap_bytes, |
| 324 |
interval: interval.as_str(), |
| 325 |
})?); |
| 326 |
|
| 327 |
|
| 328 |
self.retry_request_json(Idempotency::Unsafe, || { |
| 329 |
let req = self |
| 330 |
.http |
| 331 |
.post(&self.endpoints.subscription_checkout) |
| 332 |
.bearer_auth(token.as_str()) |
| 333 |
.header("content-type", "application/json") |
| 334 |
.body(body.clone()); |
| 335 |
async move { check_response(req.send().await?).await } |
| 336 |
}) |
| 337 |
.await |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
#[tracing::instrument(skip(self))] |
| 344 |
pub async fn queue_storage_cap_change(&self, cap_bytes: i64) -> Result<SubscriptionStatus> { |
| 345 |
let token = self.require_token()?; |
| 346 |
let body = Bytes::from(serde_json::to_vec(&CapChangeRequest { cap_bytes })?); |
| 347 |
self.retry_request_json( |
| 348 |
Idempotency::IdempotentWrite { |
| 349 |
on: "pending cap change (upsert)", |
| 350 |
}, |
| 351 |
|| { |
| 352 |
let req = self |
| 353 |
.http |
| 354 |
.post(&self.endpoints.subscription_storage_cap) |
| 355 |
.bearer_auth(token.as_str()) |
| 356 |
.header("content-type", "application/json") |
| 357 |
.body(body.clone()); |
| 358 |
async move { check_response(req.send().await?).await } |
| 359 |
}, |
| 360 |
) |
| 361 |
.await |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
#[cfg(test)] |
| 366 |
mod tests { |
| 367 |
use super::*; |
| 368 |
|
| 369 |
fn pricing(min_charge: i64, per_gb_tenths: i64, annual_mult: i64) -> AppPricing { |
| 370 |
AppPricing { |
| 371 |
app_name: "test".into(), |
| 372 |
min_charge_cents: Cents(min_charge), |
| 373 |
per_gb_tenths_of_cent_per_month: per_gb_tenths, |
| 374 |
annual_multiplier: annual_mult, |
| 375 |
min_cap_bytes: 0, |
| 376 |
max_cap_bytes: i64::MAX, |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
#[test] |
| 381 |
fn quote_cents_applies_floor_and_annual_multiplier() { |
| 382 |
|
| 383 |
|
| 384 |
let p = pricing(16, 50, 10); |
| 385 |
let ten_gib = 10 * 1024 * 1024 * 1024; |
| 386 |
assert_eq!(p.quote_cents(ten_gib, BillingInterval::Monthly), Cents(50)); |
| 387 |
assert_eq!(p.quote_cents(ten_gib, BillingInterval::Annual), Cents(500)); |
| 388 |
|
| 389 |
assert_eq!(p.quote_cents(1, BillingInterval::Monthly), Cents(16)); |
| 390 |
} |
| 391 |
|
| 392 |
#[test] |
| 393 |
fn quote_cents_saturates_on_hostile_server_numbers() { |
| 394 |
|
| 395 |
|
| 396 |
let p = pricing(0, i64::MAX, i64::MAX); |
| 397 |
let big = p.quote_cents(i64::MAX, BillingInterval::Annual); |
| 398 |
assert!(big.get() >= 0, "price must never wrap negative, got {big}"); |
| 399 |
assert_eq!(big, Cents(i64::MAX)); |
| 400 |
} |
| 401 |
|
| 402 |
#[test] |
| 403 |
fn billing_interval_from_wire_defaults_to_monthly() { |
| 404 |
assert_eq!( |
| 405 |
BillingInterval::from_wire("annual"), |
| 406 |
BillingInterval::Annual |
| 407 |
); |
| 408 |
assert_eq!( |
| 409 |
BillingInterval::from_wire("monthly"), |
| 410 |
BillingInterval::Monthly |
| 411 |
); |
| 412 |
assert_eq!( |
| 413 |
BillingInterval::from_wire("weekly"), |
| 414 |
BillingInterval::Monthly |
| 415 |
); |
| 416 |
assert_eq!(BillingInterval::from_wire(""), BillingInterval::Monthly); |
| 417 |
} |
| 418 |
|
| 419 |
#[test] |
| 420 |
fn billing_interval_serde_roundtrips_as_wire_string() { |
| 421 |
|
| 422 |
assert_eq!( |
| 423 |
serde_json::to_string(&BillingInterval::Annual).unwrap(), |
| 424 |
"\"annual\"" |
| 425 |
); |
| 426 |
assert_eq!( |
| 427 |
serde_json::from_str::<BillingInterval>("\"monthly\"").unwrap(), |
| 428 |
BillingInterval::Monthly |
| 429 |
); |
| 430 |
|
| 431 |
assert_eq!( |
| 432 |
serde_json::from_str::<BillingInterval>("\"weekly\"").unwrap(), |
| 433 |
BillingInterval::Monthly |
| 434 |
); |
| 435 |
} |
| 436 |
|
| 437 |
#[test] |
| 438 |
fn cents_is_wire_transparent_to_i64() { |
| 439 |
assert_eq!(serde_json::to_string(&Cents(1234)).unwrap(), "1234"); |
| 440 |
assert_eq!(serde_json::from_str::<Cents>("1234").unwrap(), Cents(1234)); |
| 441 |
} |
| 442 |
} |
| 443 |
|