Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Migrate accounts to the new HTTP client

Manav Rathia0275b2e9a6e
diff --git a/rust/apps/cli/src/auth_flow.rs b/rust/apps/cli/src/auth_flow.rs
index 6bb86d1605..b2b7e7105e 100644
--- a/rust/apps/cli/src/auth_flow.rs
+++ b/rust/apps/cli/src/auth_flow.rs
@@ -51,11 +51,17 @@ fn to_shared_error(error: Error) -> shared::Error {
Error::ApiError {
status,
code,
- message,
- } => shared::Error::from(ente_core::http_legacy::Error::Http {
- status,
- code,
- message,
+ message: _,
+ } => shared::Error::from(match code {
+ Some(code) => ente_core::http::Error::Api {
+ status,
+ path: String::new(),
+ code,
+ },
+ None => ente_core::http::Error::Http {
+ status,
+ path: String::new(),
+ },
}),
Error::Generic(message) => shared::Error::Generic(message),
}
diff --git a/rust/apps/cli/src/models/error.rs b/rust/apps/cli/src/models/error.rs
index c8a1f5f291..d7f8870f88 100644
--- a/rust/apps/cli/src/models/error.rs
+++ b/rust/apps/cli/src/models/error.rs
@@ -84,23 +84,26 @@ impl From<AuthError> for Error {
impl From<AccountsError> for Error {
fn from(err: AccountsError) -> Self {
match err {
- AccountsError::Http(ente_core::http_legacy::Error::Http {
- status,
- code,
- message,
- }) => Error::ApiError {
- status,
- code,
- message,
- },
- AccountsError::Http(ente_core::http_legacy::Error::Network(message)) => {
- Error::Generic(format!("Network error: {message}"))
- }
- AccountsError::Http(ente_core::http_legacy::Error::Parse(message)) => {
- Error::Generic(format!("JSON parse error: {message}"))
- }
- AccountsError::Http(ente_core::http_legacy::Error::InvalidUrl(message)) => {
- Error::InvalidConfig(message)
+ AccountsError::Http(error) => {
+ let message = error.to_string();
+ match error {
+ ente_core::http::Error::Http { status, .. } => Error::ApiError {
+ status,
+ code: None,
+ message,
+ },
+ ente_core::http::Error::Api { status, code, .. } => Error::ApiError {
+ status,
+ code: Some(code),
+ message,
+ },
+ ente_core::http::Error::Network(_) => {
+ Error::Generic(format!("Network error: {message}"))
+ }
+ ente_core::http::Error::Parse(_) => {
+ Error::Generic(format!("JSON parse error: {message}"))
+ }
+ }
}
AccountsError::Serialization(source) => Error::Serialization(source),
AccountsError::Crypto(message) => Error::Crypto(message),
diff --git a/rust/crates/accounts/src/client.rs b/rust/crates/accounts/src/client.rs
index 82c7398301..aa24fa73dc 100644
--- a/rust/crates/accounts/src/client.rs
+++ b/rust/crates/accounts/src/client.rs
@@ -4,7 +4,7 @@ use base64::{Engine, engine::general_purpose::STANDARD};
use ente_core::{
auth::{SrpAttributes as CoreSrpAttributes, SrpSession},
crypto::SecretVec,
- http_legacy::{Error as HttpError, HttpClient},
+ http::{self, Api, ApiConfig, Auth, Http},
};
use std::time::Duration;
@@ -52,9 +52,9 @@ fn require_srp_m2(auth_response: &AuthResponse) -> Result<&str> {
.ok_or_else(|| Error::AuthenticationFailed("Missing server proof".to_string()))
}
-/// Shared account client built on `ente_core::http_legacy::HttpClient`.
+/// Shared account client built on `ente_core::http::Api`.
pub struct AccountsClient {
- http: HttpClient,
+ api: Api,
client_package: String,
sleep_fn: SleepFn,
}
@@ -62,19 +62,27 @@ pub struct AccountsClient {
impl AccountsClient {
/// Construct a client from a config.
pub fn new(config: AccountsClientConfig) -> Result<Self> {
- let client_package = config.client_package.clone();
let sleep_fn = config.sleep_fn();
- let http = HttpClient::new_with_config(config.into())?;
+ let api = Api::new(
+ Http::new()?,
+ ApiConfig {
+ origin: config.base_url,
+ client_package: Some(config.client_package.clone()),
+ client_version: config.client_version,
+ user_agent: config.user_agent,
+ auth: config.auth_token.map(Auth::User),
+ },
+ );
Ok(Self {
- http,
- client_package,
+ api,
+ client_package: config.client_package,
sleep_fn,
})
}
/// Replace the auth token used for authenticated requests.
pub fn set_auth_token(&self, auth_token: Option<String>) {
- self.http.set_auth_token(auth_token);
+ self.api.set_auth(auth_token.map(Auth::User));
}
/// Return the client package associated with this client.
@@ -86,21 +94,17 @@ impl AccountsClient {
(self.sleep_fn)(duration).await;
}
- fn should_retry(error: &HttpError, policy: RetryPolicy) -> bool {
+ fn should_retry(error: &http::Error, policy: RetryPolicy) -> bool {
match policy {
RetryPolicy::NoRetry => false,
- RetryPolicy::Default => match error {
- HttpError::Network(_) => true,
- HttpError::Http { status, .. } => *status == 429 || *status >= 500,
- HttpError::Parse(_) | HttpError::InvalidUrl(_) => false,
- },
+ RetryPolicy::Default => error.is_retryable(),
}
}
async fn execute<T, F, Fut>(&self, policy: RetryPolicy, mut operation: F) -> Result<T>
where
F: FnMut() -> Fut,
- Fut: std::future::Future<Output = std::result::Result<T, HttpError>>,
+ Fut: std::future::Future<Output = std::result::Result<T, http::Error>>,
{
if policy == RetryPolicy::NoRetry {
return operation().await.map_err(Error::from);
@@ -134,8 +138,16 @@ impl AccountsClient {
pub async fn get_srp_attributes(&self, email: &str) -> Result<SrpAttributes> {
let query = [("email", email.to_string())];
let response: GetSrpAttributesResponse = self
- .execute(RetryPolicy::Default, || {
- self.http.get_json("/users/srp/attributes", &query)
+ .execute(RetryPolicy::Default, || async {
+ self.api
+ .get("/users/srp/attributes")
+ .query(&query)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await?;
Ok(response.attributes)
@@ -194,8 +206,16 @@ impl AccountsClient {
srp_user_id: srp_user_id.to_string(),
srp_a: STANDARD.encode(client_public),
};
- self.execute(RetryPolicy::Default, || {
- self.http.post_json("/users/srp/create-session", &request)
+ self.execute(RetryPolicy::Default, || async {
+ self.api
+ .post("/users/srp/create-session")
+ .json(&request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
@@ -212,8 +232,16 @@ impl AccountsClient {
session_id: session_id.to_string(),
srp_m1: STANDARD.encode(client_proof),
};
- self.execute(RetryPolicy::NoRetry, || {
- self.http.post_json("/users/srp/verify-session", &request)
+ self.execute(RetryPolicy::NoRetry, || async {
+ self.api
+ .post("/users/srp/verify-session")
+ .json(&request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
@@ -224,8 +252,15 @@ impl AccountsClient {
email: email.to_string(),
purpose: purpose.to_string(),
};
- self.execute(RetryPolicy::Default, || {
- self.http.post_empty("/users/ott", &request)
+ self.execute(RetryPolicy::Default, || async {
+ self.api
+ .post("/users/ott")
+ .json(&request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?;
+ Ok(())
})
.await
}
@@ -242,8 +277,16 @@ impl AccountsClient {
ott: ott.to_string(),
source: source.map(str::to_string),
};
- self.execute(RetryPolicy::NoRetry, || {
- self.http.post_json("/users/verify-email", &request)
+ self.execute(RetryPolicy::NoRetry, || async {
+ self.api
+ .post("/users/verify-email")
+ .json(&request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
@@ -251,24 +294,46 @@ impl AccountsClient {
/// Upload user key attributes.
pub async fn set_user_key_attributes(&self, key_attributes: KeyAttributes) -> Result<()> {
let request = SetUserAttributesRequest { key_attributes };
- self.execute(RetryPolicy::Default, || {
- self.http.put_empty("/users/attributes", &request)
+ self.execute(RetryPolicy::Default, || async {
+ self.api
+ .put("/users/attributes")
+ .json(&request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?;
+ Ok(())
})
.await
}
/// Upload recovery-key attributes.
pub async fn set_recovery_key_attributes(&self, request: SetRecoveryKeyRequest) -> Result<()> {
- self.execute(RetryPolicy::Default, || {
- self.http.put_empty("/users/recovery-key", &request)
+ self.execute(RetryPolicy::Default, || async {
+ self.api
+ .put("/users/recovery-key")
+ .json(&request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?;
+ Ok(())
})
.await
}
/// Start SRP setup for an authenticated user.
pub async fn setup_srp(&self, request: &SetupSrpRequest) -> Result<SetupSrpResponse> {
- self.execute(RetryPolicy::Default, || {
- self.http.post_json("/users/srp/setup", request)
+ self.execute(RetryPolicy::Default, || async {
+ self.api
+ .post("/users/srp/setup")
+ .json(request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
@@ -283,8 +348,16 @@ impl AccountsClient {
setup_id: setup_id.to_string(),
srp_m1: srp_m1.to_string(),
};
- self.execute(RetryPolicy::NoRetry, || {
- self.http.post_json("/users/srp/complete", &request)
+ self.execute(RetryPolicy::NoRetry, || async {
+ self.api
+ .post("/users/srp/complete")
+ .json(&request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
@@ -294,16 +367,31 @@ impl AccountsClient {
&self,
request: &UpdateSrpAndKeysRequest,
) -> Result<UpdateSrpAndKeysResponse> {
- self.execute(RetryPolicy::NoRetry, || {
- self.http.post_json("/users/srp/update", request)
+ self.execute(RetryPolicy::NoRetry, || async {
+ self.api
+ .post("/users/srp/update")
+ .json(request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
/// Get session validity and optional remote key attributes.
pub async fn get_session_validity(&self) -> Result<SessionValidityResponse> {
- self.execute(RetryPolicy::Default, || {
- self.http.get_json("/users/session-validity/v2", &[])
+ self.execute(RetryPolicy::Default, || async {
+ self.api
+ .get("/users/session-validity/v2")
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
@@ -311,8 +399,15 @@ impl AccountsClient {
/// Change the authenticated user's email.
pub async fn change_email(&self, email: &str, ott: &str) -> Result<()> {
let body = serde_json::json!({ "email": email, "ott": ott });
- self.execute(RetryPolicy::NoRetry, || {
- self.http.post_empty("/users/change-email", &body)
+ self.execute(RetryPolicy::NoRetry, || async {
+ self.api
+ .post("/users/change-email")
+ .json(&body)
+ .send()
+ .await?
+ .error_for_code()
+ .await?;
+ Ok(())
})
.await
}
@@ -320,8 +415,15 @@ impl AccountsClient {
/// Logout the current authenticated session.
pub async fn logout(&self) -> Result<()> {
let body = serde_json::json!({});
- self.execute(RetryPolicy::Default, || {
- self.http.post_empty("/users/logout", &body)
+ self.execute(RetryPolicy::Default, || async {
+ self.api
+ .post("/users/logout")
+ .json(&body)
+ .send()
+ .await?
+ .error_for_code()
+ .await?;
+ Ok(())
})
.await
}
@@ -329,8 +431,15 @@ impl AccountsClient {
/// Return whether two-factor is enabled.
pub async fn get_two_factor_status(&self) -> Result<bool> {
let response: TwoFactorStatusResponse = self
- .execute(RetryPolicy::Default, || {
- self.http.get_json("/users/two-factor/status", &[])
+ .execute(RetryPolicy::Default, || async {
+ self.api
+ .get("/users/two-factor/status")
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await?;
Ok(response.status)
@@ -339,16 +448,31 @@ impl AccountsClient {
/// Start TOTP setup.
pub async fn setup_two_factor(&self) -> Result<TwoFactorSecret> {
let body = serde_json::json!({});
- self.execute(RetryPolicy::NoRetry, || {
- self.http.post_json("/users/two-factor/setup", &body)
+ self.execute(RetryPolicy::NoRetry, || async {
+ self.api
+ .post("/users/two-factor/setup")
+ .json(&body)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
/// Enable TOTP two-factor with encrypted recovery material.
pub async fn enable_two_factor(&self, request: &EnableTwoFactorRequest) -> Result<()> {
- self.execute(RetryPolicy::NoRetry, || {
- self.http.post_empty("/users/two-factor/enable", request)
+ self.execute(RetryPolicy::NoRetry, || async {
+ self.api
+ .post("/users/two-factor/enable")
+ .json(request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?;
+ Ok(())
})
.await
}
@@ -356,8 +480,15 @@ impl AccountsClient {
/// Disable TOTP two-factor.
pub async fn disable_two_factor(&self) -> Result<()> {
let body = serde_json::json!({});
- self.execute(RetryPolicy::Default, || {
- self.http.post_empty("/users/two-factor/disable", &body)
+ self.execute(RetryPolicy::Default, || async {
+ self.api
+ .post("/users/two-factor/disable")
+ .json(&body)
+ .send()
+ .await?
+ .error_for_code()
+ .await?;
+ Ok(())
})
.await
}
@@ -368,8 +499,16 @@ impl AccountsClient {
session_id: session_id.to_string(),
code: code.to_string(),
};
- self.execute(RetryPolicy::NoRetry, || {
- self.http.post_json("/users/two-factor/verify", &request)
+ self.execute(RetryPolicy::NoRetry, || async {
+ self.api
+ .post("/users/two-factor/verify")
+ .json(&request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
@@ -390,8 +529,16 @@ impl AccountsClient {
},
),
];
- self.execute(RetryPolicy::Default, || {
- self.http.get_json("/users/two-factor/recover", &query)
+ self.execute(RetryPolicy::Default, || async {
+ self.api
+ .get("/users/two-factor/recover")
+ .query(&query)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
@@ -401,16 +548,31 @@ impl AccountsClient {
&self,
request: &RemoveTwoFactorRequest,
) -> Result<TwoFactorAuthorizationResponse> {
- self.execute(RetryPolicy::NoRetry, || {
- self.http.post_json("/users/two-factor/remove", request)
+ self.execute(RetryPolicy::NoRetry, || async {
+ self.api
+ .post("/users/two-factor/remove")
+ .json(request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
/// Get passkey recovery status.
pub async fn get_two_factor_recovery_status(&self) -> Result<TwoFactorRecoveryStatusResponse> {
- self.execute(RetryPolicy::Default, || {
- self.http.get_json("/users/two-factor/recovery-status", &[])
+ self.execute(RetryPolicy::Default, || async {
+ self.api
+ .get("/users/two-factor/recovery-status")
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
@@ -420,9 +582,15 @@ impl AccountsClient {
&self,
request: &ConfigurePasskeyRecoveryRequest,
) -> Result<()> {
- self.execute(RetryPolicy::Default, || {
- self.http
- .post_empty("/users/two-factor/passkeys/configure-recovery", request)
+ self.execute(RetryPolicy::Default, || async {
+ self.api
+ .post("/users/two-factor/passkeys/configure-recovery")
+ .json(request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?;
+ Ok(())
})
.await
}
@@ -430,17 +598,31 @@ impl AccountsClient {
/// Poll passkey verification completion.
pub async fn check_passkey_status(&self, session_id: &str) -> Result<AuthResponse> {
let query = [("sessionID", session_id.to_string())];
- self.execute(RetryPolicy::Default, || {
- self.http
- .get_json("/users/two-factor/passkeys/get-token", &query)
+ self.execute(RetryPolicy::Default, || async {
+ self.api
+ .get("/users/two-factor/passkeys/get-token")
+ .query(&query)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
/// Fetch accounts-app broker token and URL.
pub async fn get_accounts_token(&self) -> Result<AccountsTokenResponse> {
- self.execute(RetryPolicy::Default, || {
- self.http.get_json("/users/accounts-token", &[])
+ self.execute(RetryPolicy::Default, || async {
+ self.api
+ .get("/users/accounts-token")
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
})
.await
}
diff --git a/rust/crates/accounts/src/error.rs b/rust/crates/accounts/src/error.rs
index 36eb32bc02..f78de187ca 100644
--- a/rust/crates/accounts/src/error.rs
+++ b/rust/crates/accounts/src/error.rs
@@ -1,7 +1,7 @@
//! Shared error types for account flows.
use base64::DecodeError;
-use ente_core::{auth::AuthError, crypto, http_legacy::Error as HttpError};
+use ente_core::{auth::AuthError, crypto, http};
use thiserror::Error;
/// Result alias for the shared account crate.
@@ -11,8 +11,8 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
/// HTTP/transport or server error.
- #[error("{0}")]
- Http(#[from] HttpError),
+ #[error(transparent)]
+ Http(#[from] http::Error),
/// Serialization/deserialization error.
#[error("Serialization error: {0}")]
@@ -47,23 +47,7 @@ impl Error {
/// Return the HTTP status code if the error came from the API.
pub fn status_code(&self) -> Option<u16> {
match self {
- Error::Http(HttpError::Http { status, .. }) => Some(*status),
- _ => None,
- }
- }
-
- /// Return the structured server error code if available.
- pub fn api_code(&self) -> Option<&str> {
- match self {
- Error::Http(HttpError::Http { code, .. }) => code.as_deref(),
- _ => None,
- }
- }
-
- /// Return the server-provided message if available.
- pub fn api_message(&self) -> Option<&str> {
- match self {
- Error::Http(HttpError::Http { message, .. }) => Some(message.as_str()),
+ Error::Http(error) => error.status_code(),
_ => None,
}
}
diff --git a/rust/crates/accounts/src/types.rs b/rust/crates/accounts/src/types.rs
index e559bcb0ef..fced94bdc5 100644
--- a/rust/crates/accounts/src/types.rs
+++ b/rust/crates/accounts/src/types.rs
@@ -1,6 +1,6 @@
//! Shared reusable types for account clients.
-use ente_core::{http_legacy::HttpConfig, urls::PRODUCTION_API_BASE_URL};
+use ente_core::urls::PRODUCTION_API_BASE_URL;
use futures_timer::Delay;
use serde::{Deserialize, Serialize};
use std::{future::Future, pin::Pin, sync::Arc, time::Duration};
@@ -36,8 +36,6 @@ pub struct AccountsClientConfig {
pub client_version: Option<String>,
/// Optional user agent.
pub user_agent: Option<String>,
- /// Optional request timeout.
- pub timeout_secs: Option<u64>,
sleep_fn: SleepFn,
}
@@ -52,7 +50,6 @@ impl std::fmt::Debug for AccountsClientConfig {
.field("client_package", &self.client_package)
.field("client_version", &self.client_version)
.field("user_agent", &self.user_agent)
- .field("timeout_secs", &self.timeout_secs)
.field("sleep_fn", &"<configured>")
.finish()
}
@@ -67,7 +64,6 @@ impl AccountsClientConfig {
client_package: client_package.into(),
client_version: None,
user_agent: None,
- timeout_secs: None,
sleep_fn: default_sleep_fn(),
}
}
@@ -96,12 +92,6 @@ impl AccountsClientConfig {
self
}
- /// Set a request timeout in seconds.
- pub fn with_timeout_secs(mut self, timeout_secs: u64) -> Self {
- self.timeout_secs = Some(timeout_secs);
- self
- }
-
/// Override the async sleep implementation used for retry/backoff waits.
pub fn with_sleep_fn<F, Fut>(mut self, sleep_fn: F) -> Self
where
@@ -117,19 +107,6 @@ impl AccountsClientConfig {
}
}
-impl From<AccountsClientConfig> for HttpConfig {
- fn from(value: AccountsClientConfig) -> Self {
- HttpConfig {
- base_url: value.base_url,
- auth_token: value.auth_token,
- user_agent: value.user_agent,
- client_package: Some(value.client_package),
- client_version: value.client_version,
- timeout_secs: value.timeout_secs,
- }
- }
-}
-
/// Decrypted account secrets.
#[derive(Serialize, Deserialize, Zeroize)]
#[zeroize(drop)]