From e1f47c3efdc3b8f19c7d86951c900a7bbfa6acb5 Mon Sep 17 00:00:00 2001 From: Oguz Kocer Date: Thu, 4 Sep 2025 12:51:16 -0400 Subject: [PATCH] Add request URL parameter to WpAppNotifier trait Enhance the `requested_with_invalid_authentication` method in the WpAppNotifier trait to include the request URL, providing better context for authentication failures. Changes: - Update WpAppNotifier trait method to accept request URL parameter - Modify generated request executor code to capture and pass URL - Update all Rust implementations across test and CLI modules - Update Kotlin EmptyAppNotifier implementation - Update Swift EmptyAppNotifier and MockAppNotifier implementations - Add NetworkRequestAccessor trait import to generated code --- .../rs/wordpress/api/kotlin/EmptyAppNotifier.kt | 2 +- native/swift/Sources/wordpress-api/AppNotifier.swift | 2 +- native/swift/Tests/wordpress-api/Support/Mocks.swift | 2 +- wp_api/src/lib.rs | 2 +- wp_api_integration_tests/src/lib.rs | 2 +- .../tests/test_app_notifier_immut.rs | 2 +- wp_com_e2e/src/main.rs | 2 +- wp_derive_request_builder/src/generate.rs | 11 +++++++---- wp_rs_cli/src/bin/wp_rs_cli/main.rs | 5 +++-- 9 files changed, 17 insertions(+), 13 deletions(-) diff --git a/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/EmptyAppNotifier.kt b/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/EmptyAppNotifier.kt index ba38c7d3a..1e1a1e51d 100644 --- a/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/EmptyAppNotifier.kt +++ b/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/EmptyAppNotifier.kt @@ -3,7 +3,7 @@ package rs.wordpress.api.kotlin import uniffi.wp_api.WpAppNotifier class EmptyAppNotifier : WpAppNotifier { - override suspend fun requestedWithInvalidAuthentication() { + override suspend fun requestedWithInvalidAuthentication(requestUrl: String) { // no-op } } diff --git a/native/swift/Sources/wordpress-api/AppNotifier.swift b/native/swift/Sources/wordpress-api/AppNotifier.swift index 8adad8332..bfe501771 100644 --- a/native/swift/Sources/wordpress-api/AppNotifier.swift +++ b/native/swift/Sources/wordpress-api/AppNotifier.swift @@ -2,7 +2,7 @@ import Foundation import WordPressAPIInternal class EmptyAppNotifier: @unchecked Sendable, WpAppNotifier { - func requestedWithInvalidAuthentication() async { + func requestedWithInvalidAuthentication(requestUrl: String) async { // no-op } } diff --git a/native/swift/Tests/wordpress-api/Support/Mocks.swift b/native/swift/Tests/wordpress-api/Support/Mocks.swift index 57e5767d2..c739173c9 100644 --- a/native/swift/Tests/wordpress-api/Support/Mocks.swift +++ b/native/swift/Tests/wordpress-api/Support/Mocks.swift @@ -2,7 +2,7 @@ import Foundation import WordPressAPI final class MockAppNotifier: WpAppNotifier { - func requestedWithInvalidAuthentication() async { + func requestedWithInvalidAuthentication(requestUrl: String) async { // no-op } } diff --git a/wp_api/src/lib.rs b/wp_api/src/lib.rs index 182842af1..c478e6e2a 100644 --- a/wp_api/src/lib.rs +++ b/wp_api/src/lib.rs @@ -201,7 +201,7 @@ pub enum IntegerOrString { #[uniffi::export(with_foreign)] #[async_trait::async_trait] pub trait WpAppNotifier: Send + Sync + std::fmt::Debug { - async fn requested_with_invalid_authentication(&self); + async fn requested_with_invalid_authentication(&self, request_url: String); } #[macro_export] diff --git a/wp_api_integration_tests/src/lib.rs b/wp_api_integration_tests/src/lib.rs index ff2806aa3..7c1100730 100644 --- a/wp_api_integration_tests/src/lib.rs +++ b/wp_api_integration_tests/src/lib.rs @@ -235,7 +235,7 @@ pub struct EmptyAppNotifier; #[async_trait] impl WpAppNotifier for EmptyAppNotifier { - async fn requested_with_invalid_authentication(&self) { + async fn requested_with_invalid_authentication(&self, _request_url: String) { // no-op } } diff --git a/wp_api_integration_tests/tests/test_app_notifier_immut.rs b/wp_api_integration_tests/tests/test_app_notifier_immut.rs index b3da5713c..c99a4244c 100644 --- a/wp_api_integration_tests/tests/test_app_notifier_immut.rs +++ b/wp_api_integration_tests/tests/test_app_notifier_immut.rs @@ -84,7 +84,7 @@ impl FooAppNotifier { #[async_trait] impl WpAppNotifier for FooAppNotifier { - async fn requested_with_invalid_authentication(&self) { + async fn requested_with_invalid_authentication(&self, _request_url: String) { let result = (self.requested_with_invalid_authentication_fn)(); self.has_triggered_notification .store(result, Ordering::Relaxed); diff --git a/wp_com_e2e/src/main.rs b/wp_com_e2e/src/main.rs index 0b820649b..33ff34030 100644 --- a/wp_com_e2e/src/main.rs +++ b/wp_com_e2e/src/main.rs @@ -28,7 +28,7 @@ pub struct EmptyAppNotifier; #[async_trait] impl WpAppNotifier for EmptyAppNotifier { - async fn requested_with_invalid_authentication(&self) { + async fn requested_with_invalid_authentication(&self, _request_url: String) { // no-op } } diff --git a/wp_derive_request_builder/src/generate.rs b/wp_derive_request_builder/src/generate.rs index e9eab2784..009fa0144 100644 --- a/wp_derive_request_builder/src/generate.rs +++ b/wp_derive_request_builder/src/generate.rs @@ -74,31 +74,34 @@ fn generate_async_request_executor( pub async #fn_signature -> Result<#response_type_ident, #error_type> { use #crate_ident::api_error::MaybeWpError; use #crate_ident::middleware::PerformsRequests; + use #crate_ident::request::NetworkRequestAccessor; let perform_request = async || { #request_from_request_builder + let request_url: String = request.url().into(); let response = self.perform(std::sync::Arc::new(request)).await?; let response_status_code = response.status_code; let parsed_response = response.parse(); let unauthorized = parsed_response.is_unauthorized_error().unwrap_or_default() || (response_status_code == 401 && self.fetch_authentication_state().await.map(|auth_state| auth_state.is_unauthorized()).unwrap_or_default()); - Ok((parsed_response, unauthorized)) + Ok((parsed_response, unauthorized, request_url)) }; let mut parsed_response; let mut unauthorized; + let mut request_url; // The Rust compiler has trouble figuring out the return type. The statement below helps it. let result: Result<_, #error_type> = perform_request().await; - (parsed_response, unauthorized) = result?; + (parsed_response, unauthorized, request_url) = result?; // If the auth provider successfully refreshed the authentication, we can retry the request. if unauthorized && self.delegate.auth_provider.refresh().await { // The response of the retried request will be returned instead of the original one. - (parsed_response, unauthorized) = perform_request().await?; + (parsed_response, unauthorized, request_url) = perform_request().await?; } if unauthorized { - self.delegate.app_notifier.requested_with_invalid_authentication().await; + self.delegate.app_notifier.requested_with_invalid_authentication(request_url).await; } parsed_response diff --git a/wp_rs_cli/src/bin/wp_rs_cli/main.rs b/wp_rs_cli/src/bin/wp_rs_cli/main.rs index a9dc1ce25..4ef47c4d3 100644 --- a/wp_rs_cli/src/bin/wp_rs_cli/main.rs +++ b/wp_rs_cli/src/bin/wp_rs_cli/main.rs @@ -410,9 +410,10 @@ async fn build_api_client(args: &AuthArgs, url: &Option) -> Result