Skip to content

Commit 6eac5df

Browse files
authored
Add request URL parameter to WpAppNotifier trait (#873)
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
1 parent b8cc918 commit 6eac5df

File tree

9 files changed

+17
-13
lines changed

9 files changed

+17
-13
lines changed

native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/EmptyAppNotifier.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package rs.wordpress.api.kotlin
33
import uniffi.wp_api.WpAppNotifier
44

55
class EmptyAppNotifier : WpAppNotifier {
6-
override suspend fun requestedWithInvalidAuthentication() {
6+
override suspend fun requestedWithInvalidAuthentication(requestUrl: String) {
77
// no-op
88
}
99
}

native/swift/Sources/wordpress-api/AppNotifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import WordPressAPIInternal
33

44
class EmptyAppNotifier: @unchecked Sendable, WpAppNotifier {
5-
func requestedWithInvalidAuthentication() async {
5+
func requestedWithInvalidAuthentication(requestUrl: String) async {
66
// no-op
77
}
88
}

native/swift/Tests/wordpress-api/Support/Mocks.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import WordPressAPI
33

44
final class MockAppNotifier: WpAppNotifier {
5-
func requestedWithInvalidAuthentication() async {
5+
func requestedWithInvalidAuthentication(requestUrl: String) async {
66
// no-op
77
}
88
}

wp_api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ pub enum IntegerOrString {
201201
#[uniffi::export(with_foreign)]
202202
#[async_trait::async_trait]
203203
pub trait WpAppNotifier: Send + Sync + std::fmt::Debug {
204-
async fn requested_with_invalid_authentication(&self);
204+
async fn requested_with_invalid_authentication(&self, request_url: String);
205205
}
206206

207207
#[macro_export]

wp_api_integration_tests/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub struct EmptyAppNotifier;
235235

236236
#[async_trait]
237237
impl WpAppNotifier for EmptyAppNotifier {
238-
async fn requested_with_invalid_authentication(&self) {
238+
async fn requested_with_invalid_authentication(&self, _request_url: String) {
239239
// no-op
240240
}
241241
}

wp_api_integration_tests/tests/test_app_notifier_immut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl FooAppNotifier {
8484

8585
#[async_trait]
8686
impl WpAppNotifier for FooAppNotifier {
87-
async fn requested_with_invalid_authentication(&self) {
87+
async fn requested_with_invalid_authentication(&self, _request_url: String) {
8888
let result = (self.requested_with_invalid_authentication_fn)();
8989
self.has_triggered_notification
9090
.store(result, Ordering::Relaxed);

wp_com_e2e/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct EmptyAppNotifier;
2828

2929
#[async_trait]
3030
impl WpAppNotifier for EmptyAppNotifier {
31-
async fn requested_with_invalid_authentication(&self) {
31+
async fn requested_with_invalid_authentication(&self, _request_url: String) {
3232
// no-op
3333
}
3434
}

wp_derive_request_builder/src/generate.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,34 @@ fn generate_async_request_executor(
7474
pub async #fn_signature -> Result<#response_type_ident, #error_type> {
7575
use #crate_ident::api_error::MaybeWpError;
7676
use #crate_ident::middleware::PerformsRequests;
77+
use #crate_ident::request::NetworkRequestAccessor;
7778
let perform_request = async || {
7879
#request_from_request_builder
80+
let request_url: String = request.url().into();
7981
let response = self.perform(std::sync::Arc::new(request)).await?;
8082
let response_status_code = response.status_code;
8183
let parsed_response = response.parse();
8284
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());
8385

84-
Ok((parsed_response, unauthorized))
86+
Ok((parsed_response, unauthorized, request_url))
8587
};
8688

8789
let mut parsed_response;
8890
let mut unauthorized;
91+
let mut request_url;
8992

9093
// The Rust compiler has trouble figuring out the return type. The statement below helps it.
9194
let result: Result<_, #error_type> = perform_request().await;
92-
(parsed_response, unauthorized) = result?;
95+
(parsed_response, unauthorized, request_url) = result?;
9396

9497
// If the auth provider successfully refreshed the authentication, we can retry the request.
9598
if unauthorized && self.delegate.auth_provider.refresh().await {
9699
// The response of the retried request will be returned instead of the original one.
97-
(parsed_response, unauthorized) = perform_request().await?;
100+
(parsed_response, unauthorized, request_url) = perform_request().await?;
98101
}
99102

100103
if unauthorized {
101-
self.delegate.app_notifier.requested_with_invalid_authentication().await;
104+
self.delegate.app_notifier.requested_with_invalid_authentication(request_url).await;
102105
}
103106

104107
parsed_response

wp_rs_cli/src/bin/wp_rs_cli/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,10 @@ async fn build_api_client(args: &AuthArgs, url: &Option<String>) -> Result<WpApi
410410
struct CliAppNotifier;
411411
#[async_trait::async_trait]
412412
impl WpAppNotifier for CliAppNotifier {
413-
async fn requested_with_invalid_authentication(&self) {
413+
async fn requested_with_invalid_authentication(&self, request_url: String) {
414414
eprintln!(
415-
"Authentication failed. Please verify your credentials or token and try again."
415+
"Authentication failed for request to: {}. Please verify your credentials or token and try again.",
416+
request_url
416417
);
417418
std::process::exit(1);
418419
}

0 commit comments

Comments
 (0)