1use crate::io::network::ApiError;
2use crate::model::api::{
3 CancelSubscriptionError, CancelSubscriptionRequest, GetSubscriptionInfoRequest,
4 StripeAccountTier, SubscriptionInfo, UpgradeAccountAppStoreError,
5 UpgradeAccountAppStoreRequest, UpgradeAccountGooglePlayError, UpgradeAccountGooglePlayRequest,
6 UpgradeAccountStripeError, UpgradeAccountStripeRequest,
7};
8use crate::model::errors::{core_err_unexpected, LbErrKind, LbResult};
9use crate::Lb;
10
11impl Lb {
12 #[instrument(level = "debug", skip(self, account_tier), err(Debug))]
13 pub async fn upgrade_account_stripe(&self, account_tier: StripeAccountTier) -> LbResult<()> {
14 let account = self.get_account()?;
15
16 self.client
17 .request(account, UpgradeAccountStripeRequest { account_tier })
18 .await
19 .map_err(|err| match err {
20 ApiError::Endpoint(err) => match err {
21 UpgradeAccountStripeError::OldCardDoesNotExist => {
22 LbErrKind::OldCardDoesNotExist
23 }
24 UpgradeAccountStripeError::AlreadyPremium => LbErrKind::AlreadyPremium,
25 UpgradeAccountStripeError::InvalidCardNumber => LbErrKind::CardInvalidNumber,
26 UpgradeAccountStripeError::InvalidCardExpYear => LbErrKind::CardInvalidExpYear,
27 UpgradeAccountStripeError::InvalidCardExpMonth => {
28 LbErrKind::CardInvalidExpMonth
29 }
30 UpgradeAccountStripeError::InvalidCardCvc => LbErrKind::CardInvalidCvc,
31 UpgradeAccountStripeError::CardDecline => LbErrKind::CardDecline,
32 UpgradeAccountStripeError::InsufficientFunds => {
33 LbErrKind::CardInsufficientFunds
34 }
35 UpgradeAccountStripeError::TryAgain => LbErrKind::TryAgain,
36 UpgradeAccountStripeError::CardNotSupported => LbErrKind::CardNotSupported,
37 UpgradeAccountStripeError::ExpiredCard => LbErrKind::CardExpired,
38 UpgradeAccountStripeError::ExistingRequestPending => {
39 LbErrKind::ExistingRequestPending
40 }
41 UpgradeAccountStripeError::UserNotFound => LbErrKind::AccountNonexistent,
42 },
43 ApiError::SendFailed(_) => LbErrKind::ServerUnreachable,
44 ApiError::ClientUpdateRequired => LbErrKind::ClientUpdateRequired,
45 _ => core_err_unexpected(err),
46 })?;
47
48 Ok(())
49 }
50
51 #[instrument(level = "debug", skip(self), err(Debug))]
52 pub async fn upgrade_account_google_play(
53 &self, purchase_token: &str, account_id: &str,
54 ) -> LbResult<()> {
55 let account = self.get_account()?;
56
57 self.client
58 .request(
59 account,
60 UpgradeAccountGooglePlayRequest {
61 purchase_token: purchase_token.to_string(),
62 account_id: account_id.to_string(),
63 },
64 )
65 .await
66 .map_err(|err| match err {
67 ApiError::Endpoint(err) => match err {
68 UpgradeAccountGooglePlayError::AlreadyPremium => LbErrKind::AlreadyPremium,
69 UpgradeAccountGooglePlayError::InvalidPurchaseToken => {
70 LbErrKind::InvalidPurchaseToken
71 }
72 UpgradeAccountGooglePlayError::ExistingRequestPending => {
73 LbErrKind::ExistingRequestPending
74 }
75 UpgradeAccountGooglePlayError::UserNotFound => core_err_unexpected(err),
76 },
77 ApiError::SendFailed(_) => LbErrKind::ServerUnreachable,
78 ApiError::ClientUpdateRequired => LbErrKind::ClientUpdateRequired,
79 _ => core_err_unexpected(err),
80 })?;
81
82 Ok(())
83 }
84
85 #[instrument(level = "debug", skip(self), err(Debug))]
86 pub async fn upgrade_account_app_store(
87 &self, original_transaction_id: String, app_account_token: String,
88 ) -> LbResult<()> {
89 let account = self.get_account()?;
90
91 self.client
92 .request(
93 account,
94 UpgradeAccountAppStoreRequest { original_transaction_id, app_account_token },
95 )
96 .await
97 .map_err(|err| match err {
98 ApiError::Endpoint(err) => match err {
99 UpgradeAccountAppStoreError::AlreadyPremium => LbErrKind::AlreadyPremium,
100 UpgradeAccountAppStoreError::InvalidAuthDetails => {
101 LbErrKind::InvalidAuthDetails
102 }
103 UpgradeAccountAppStoreError::ExistingRequestPending => {
104 LbErrKind::ExistingRequestPending
105 }
106 UpgradeAccountAppStoreError::AppStoreAccountAlreadyLinked => {
107 LbErrKind::AppStoreAccountAlreadyLinked
108 }
109 UpgradeAccountAppStoreError::UserNotFound => core_err_unexpected(err),
110 },
111 ApiError::SendFailed(_) => LbErrKind::ServerUnreachable,
112 ApiError::ClientUpdateRequired => LbErrKind::ClientUpdateRequired,
113 _ => core_err_unexpected(err),
114 })?;
115
116 Ok(())
117 }
118
119 #[instrument(level = "debug", skip(self), err(Debug))]
120 pub async fn cancel_subscription(&self) -> LbResult<()> {
121 let account = self.get_account()?;
122
123 self.client
124 .request(account, CancelSubscriptionRequest {})
125 .await
126 .map_err(|err| match err {
127 ApiError::Endpoint(CancelSubscriptionError::NotPremium) => LbErrKind::NotPremium,
128 ApiError::Endpoint(CancelSubscriptionError::AlreadyCanceled) => {
129 LbErrKind::AlreadyCanceled
130 }
131 ApiError::Endpoint(CancelSubscriptionError::UsageIsOverFreeTierDataCap) => {
132 LbErrKind::UsageIsOverFreeTierDataCap
133 }
134 ApiError::Endpoint(CancelSubscriptionError::ExistingRequestPending) => {
135 LbErrKind::ExistingRequestPending
136 }
137 ApiError::Endpoint(CancelSubscriptionError::CannotCancelForAppStore) => {
138 LbErrKind::CannotCancelSubscriptionForAppStore
139 }
140 ApiError::SendFailed(_) => LbErrKind::ServerUnreachable,
141 ApiError::ClientUpdateRequired => LbErrKind::ClientUpdateRequired,
142 _ => core_err_unexpected(err),
143 })?;
144
145 Ok(())
146 }
147
148 #[instrument(level = "debug", skip(self), err(Debug))]
149 pub async fn get_subscription_info(&self) -> LbResult<Option<SubscriptionInfo>> {
150 let account = self.get_account()?;
151
152 Ok(self
153 .client
154 .request(account, GetSubscriptionInfoRequest {})
155 .await
156 .map_err(|err| match err {
157 ApiError::SendFailed(_) => LbErrKind::ServerUnreachable,
158 ApiError::ClientUpdateRequired => LbErrKind::ClientUpdateRequired,
159 _ => core_err_unexpected(err),
160 })?
161 .subscription_info)
162 }
163}