summaryrefslogtreecommitdiffhomepage
path: root/mullvad-api/src
diff options
context:
space:
mode:
authorJonathan <jonathan@mullvad.net>2023-10-16 15:20:47 +0200
committerJonathan <jonathan@mullvad.net>2023-10-16 18:34:07 +0200
commit3d5cbe67e09bd3fb06b21397a3142714dd8924ea (patch)
tree3aeeedb1efe3dd755c732075eb66afc93fa00b66 /mullvad-api/src
parentd59972f5b20724cc9df24b77e5c0aa8aa31a504f (diff)
downloadmullvadvpn-3d5cbe67e09bd3fb06b21397a3142714dd8924ea.tar.xz
mullvadvpn-3d5cbe67e09bd3fb06b21397a3142714dd8924ea.zip
Add android conditional compilation for google pay
Add conditional compilation for google pay API access for only android. Also allow new error type to be parsed. Additionally fix review comments, formatting and warnings.
Diffstat (limited to 'mullvad-api/src')
-rw-r--r--mullvad-api/src/lib.rs7
-rw-r--r--mullvad-api/src/rest.rs32
2 files changed, 35 insertions, 4 deletions
diff --git a/mullvad-api/src/lib.rs b/mullvad-api/src/lib.rs
index 647b5cf071..63f5c2ad5b 100644
--- a/mullvad-api/src/lib.rs
+++ b/mullvad-api/src/lib.rs
@@ -5,8 +5,10 @@ use chrono::{offset::Utc, DateTime};
use futures::channel::mpsc;
use futures::Stream;
use hyper::Method;
+#[cfg(target_os = "android")]
+use mullvad_types::account::{PlayPurchase, PlayPurchasePaymentToken};
use mullvad_types::{
- account::{AccountToken, PlayPurchase, PlayPurchasePaymentToken, VoucherSubmission},
+ account::{AccountToken, VoucherSubmission},
version::AppVersion,
};
use proxy::ApiConnectionMode;
@@ -63,6 +65,7 @@ pub const API_IP_CACHE_FILENAME: &str = "api-ip-address.txt";
const ACCOUNTS_URL_PREFIX: &str = "accounts/v1";
const APP_URL_PREFIX: &str = "app/v1";
+#[cfg(target_os = "android")]
const GOOGLE_PAYMENTS_URL_PREFIX: &str = "payments/google-play/v1";
pub static API: LazyManual<ApiEndpoint> = LazyManual::new(ApiEndpoint::from_env_vars);
@@ -458,6 +461,7 @@ impl AccountsProxy {
}
}
+ #[cfg(target_os = "android")]
pub fn init_play_purchase(
&mut self,
account_token: AccountToken,
@@ -490,6 +494,7 @@ impl AccountsProxy {
}
}
+ #[cfg(target_os = "android")]
pub fn verify_play_purchase(
&mut self,
account_token: AccountToken,
diff --git a/mullvad-api/src/rest.rs b/mullvad-api/src/rest.rs
index 674bcf8c4e..c3687a1eee 100644
--- a/mullvad-api/src/rest.rs
+++ b/mullvad-api/src/rest.rs
@@ -394,10 +394,17 @@ impl From<Request> for RestRequest {
}
#[derive(serde::Deserialize)]
-pub struct ErrorResponse {
+struct OldErrorResponse {
pub code: String,
}
+/// If `NewErrorResponse::type` is not defined it should default to "about:blank"
+const DEFAULT_ERROR_TYPE: &str = "about:blank";
+#[derive(serde::Deserialize)]
+struct NewErrorResponse {
+ pub r#type: Option<String>,
+}
+
#[derive(Clone)]
pub struct RequestFactory {
hostname: String,
@@ -600,8 +607,27 @@ pub async fn handle_error_response<T>(response: Response) -> Result<T> {
status => match get_body_length(&response) {
0 => status.canonical_reason().unwrap_or("Unexpected error"),
body_length => {
- let err: ErrorResponse = deserialize_body_inner(response, body_length).await?;
- return Err(Error::ApiError(status, err.code));
+ return match response.headers().get("content-type") {
+ Some(content_type) if content_type == "application/problem+json" => {
+ // TODO: We should make sure we unify the new error format and the old
+ // error format so that they both produce the same Errors for the same
+ // problems after being processed.
+ let err: NewErrorResponse =
+ deserialize_body_inner(response, body_length).await?;
+ // The new error type replaces the `code` field with the `type` field.
+ // This is what is used to programmatically check the error.
+ Err(Error::ApiError(
+ status,
+ err.r#type
+ .unwrap_or_else(|| String::from(DEFAULT_ERROR_TYPE)),
+ ))
+ }
+ _ => {
+ let err: OldErrorResponse =
+ deserialize_body_inner(response, body_length).await?;
+ Err(Error::ApiError(status, err.code))
+ }
+ };
}
},
};