Skip to content

Commit fce2199

Browse files
committed
Add can_resolve function to prevent panic
1 parent 1b87696 commit fce2199

3 files changed

Lines changed: 29 additions & 17 deletions

File tree

wp_api/src/request.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
use base64::Engine;
1111
use chrono::{DateTime, Utc};
1212
use endpoint::{
13-
ApiEndpointUrl, ApiUrlResolver,
13+
ApiEndpointUrl, ApiUrlResolver, AsNamespace, WpNamespace,
1414
application_passwords_endpoint::{
1515
ApplicationPasswordsRequestBuilder,
1616
ApplicationPasswordsRequestRetrieveCurrentWithEditContextResponse,
@@ -943,6 +943,10 @@ pub async fn fetch_authentication_state(
943943
api_url_resolver: Arc<dyn ApiUrlResolver>,
944944
authentication_provider: Arc<WpAuthenticationProvider>,
945945
) -> Result<AuthenticationState, WpApiError> {
946+
if !api_url_resolver.can_resolve(WpNamespace::WpV2.namespace_value().to_string()) {
947+
return Ok(AuthenticationState::Unauthorized);
948+
}
949+
946950
let request =
947951
ApplicationPasswordsRequestBuilder::new(api_url_resolver, authentication_provider)
948952
.retrieve_current_with_edit_context()

wp_api/src/request/endpoint.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ impl AsNamespace for WpNamespace {
118118

119119
#[uniffi::export(with_foreign)]
120120
pub trait ApiUrlResolver: Send + Sync {
121+
fn can_resolve(&self, namespace: String) -> bool;
122+
121123
fn resolve(&self, namespace: String, endpoint_segments: Vec<String>) -> Arc<ParsedUrl>;
122124
}
123125

@@ -140,6 +142,10 @@ impl WpOrgSiteApiUrlResolver {
140142

141143
#[uniffi::export]
142144
impl ApiUrlResolver for WpOrgSiteApiUrlResolver {
145+
fn can_resolve(&self, _namespace: String) -> bool {
146+
true
147+
}
148+
143149
fn resolve(&self, namespace: String, endpoint_segments: Vec<String>) -> Arc<ParsedUrl> {
144150
Arc::new(
145151
self.api_root_url

wp_api/src/wp_com/endpoint.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@ impl WpComDotOrgApiUrlResolver {
3737

3838
#[uniffi::export]
3939
impl ApiUrlResolver for WpComDotOrgApiUrlResolver {
40+
fn can_resolve(&self, namespace: String) -> bool {
41+
WpNamespace::iter().any(|n| n.namespace_value() == namespace)
42+
}
43+
4044
fn resolve(&self, namespace: String, endpoint_segments: Vec<String>) -> Arc<ParsedUrl> {
41-
{
42-
if !WpNamespace::iter().any(|n| n.namespace_value() == namespace) {
43-
panic!(
44-
"`WpComDotOrgApiUrlResolver` doesn't support the namespace `{}`. The supported namespaces are: {:?}",
45-
namespace,
46-
WpNamespace::iter()
47-
);
48-
}
49-
}
45+
assert!(
46+
self.can_resolve(namespace.clone()),
47+
"`WpComDotOrgApiUrlResolver` doesn't support the namespace `{}`. The supported namespaces are: {:?}",
48+
namespace,
49+
WpNamespace::iter()
50+
);
5051

5152
// The API root endpoint needs special handling for WordPress.com
5253
if namespace == WpNamespace::None.namespace_value() && endpoint_segments.is_empty() {
@@ -91,14 +92,15 @@ impl Default for WpComApiClientInternalUrlResolver {
9192
}
9293

9394
impl ApiUrlResolver for WpComApiClientInternalUrlResolver {
95+
fn can_resolve(&self, namespace: String) -> bool {
96+
!WpNamespace::iter().any(|n| n.namespace_value() == namespace)
97+
}
98+
9499
fn resolve(&self, namespace: String, endpoint_segments: Vec<String>) -> Arc<ParsedUrl> {
95-
{
96-
if WpNamespace::iter().any(|n| n.namespace_value() == namespace) {
97-
panic!(
98-
"`WpComApiClient` doesn't support the namespace `{namespace}`. Try using `WpApiClient` instead.",
99-
);
100-
}
101-
}
100+
assert!(
101+
self.can_resolve(namespace.clone()),
102+
"`WpComApiClient` doesn't support the namespace `{namespace}`. Try using `WpApiClient` instead.",
103+
);
102104
Arc::new(
103105
self.base_url
104106
.by_extending_and_splitting_by_forward_slash(

0 commit comments

Comments
 (0)