Skip to content
42 changes: 26 additions & 16 deletions wp_api/src/request/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub(crate) mod application_passwords_endpoint;
pub(crate) mod plugins_endpoint;
pub(crate) mod users_endpoint;

const WP_JSON_PATH_SEGMENTS: [&str; 3] = ["wp-json", "wp", "v2"];
const WP_JSON_PATH_SEGMENTS: [&str; 1] = ["wp-json"];

uniffi::custom_newtype!(WpEndpointUrl, String);
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -83,17 +83,6 @@ impl ApiBaseUrl {
.expect("ApiBaseUrl is already parsed, so this can't result in an error")
}

fn by_extending<I>(&self, segments: I) -> Url
where
I: IntoIterator,
I::Item: AsRef<str>,
{
self.url
.clone()
.extend(segments)
.expect("ApiBaseUrl is already parsed, so this can't result in an error")
}

pub fn by_extending_and_splitting_by_forward_slash<I>(&self, segments: I) -> Url
where
I: IntoIterator,
Expand All @@ -104,7 +93,10 @@ impl ApiBaseUrl {
.extend(segments.into_iter().flat_map(|s| {
s.as_ref()
.split('/')
.map(str::to_string)
.filter_map(|x| match x.trim() {
"" => None,
y => Some(y.to_string()),
})
.collect::<Vec<String>>()
}))
.expect("ApiBaseUrl is already parsed, so this can't result in an error")
Expand Down Expand Up @@ -197,9 +189,23 @@ mod tests {
format!("{}/bar", expected_wp_json_url)
);
assert_eq!(
api_base_url.by_extending(["bar", "baz"]).as_str(),
api_base_url
.by_extending_and_splitting_by_forward_slash(["bar", "baz"])
.as_str(),
format!("{}/bar/baz", expected_wp_json_url)
);
assert_eq!(
api_base_url
.by_extending_and_splitting_by_forward_slash(["bar", "baz/quox"])
.as_str(),
format!("{}/bar/baz/quox", expected_wp_json_url)
);
assert_eq!(
api_base_url
.by_extending_and_splitting_by_forward_slash(["/bar", "/baz/quox"])
.as_str(),
format!("{}/bar/baz/quox", expected_wp_json_url)
);
}

fn wp_json_endpoint(base_url: &str) -> String {
Expand All @@ -215,10 +221,14 @@ mod tests {
ApiBaseUrl::try_from("https://example.com").unwrap().into()
}

pub fn validate_endpoint(endpoint_url: ApiEndpointUrl, path: &str) {
pub fn validate_wp_v2_endpoint(endpoint_url: ApiEndpointUrl, path: &str) {
validate_endpoint("/wp/v2", endpoint_url, path);
}

fn validate_endpoint(namespace: &str, endpoint_url: ApiEndpointUrl, path: &str) {
assert_eq!(
endpoint_url.as_str(),
format!("{}{}", fixture_api_base_url().as_str(), path)
format!("{}{}{}", fixture_api_base_url().as_str(), namespace, path)
);
}
}
27 changes: 14 additions & 13 deletions wp_api/src/request/endpoint/application_passwords_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::application_passwords::{
use crate::users::UserId;

#[derive(WpDerivedRequest)]
#[Namespace("/wp/v2")]
#[SparseField(SparseApplicationPasswordField)]
enum ApplicationPasswordsRequest {
#[post(url = "/users/<user_id>/application-passwords", params = &ApplicationPasswordCreateParams, output = ApplicationPasswordWithEditContext)]
Expand All @@ -32,7 +33,7 @@ mod tests {
use super::*;
use crate::{
request::endpoint::{
tests::{fixture_api_base_url, validate_endpoint},
tests::{fixture_api_base_url, validate_wp_v2_endpoint},
ApiBaseUrl,
},
WpContext,
Expand All @@ -42,15 +43,15 @@ mod tests {

#[rstest]
fn create_application_password(endpoint: ApplicationPasswordsRequestEndpoint) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.create(&UserId(1)),
"/users/1/application-passwords",
);
}

#[rstest]
fn delete_single_application_password(endpoint: ApplicationPasswordsRequestEndpoint) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.delete(
&UserId(2),
&ApplicationPasswordUuid {
Expand All @@ -63,15 +64,15 @@ mod tests {

#[rstest]
fn delete_all_application_passwords(endpoint: ApplicationPasswordsRequestEndpoint) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.delete_all(&UserId(1)),
"/users/1/application-passwords",
);
}

#[rstest]
fn list_application_passwords_with_edit_context(endpoint: ApplicationPasswordsRequestEndpoint) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.list_with_edit_context(&UserId(2)),
"/users/2/application-passwords?context=edit",
);
Expand All @@ -81,15 +82,15 @@ mod tests {
fn list_application_passwords_with_embed_context(
endpoint: ApplicationPasswordsRequestEndpoint,
) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.list_with_embed_context(&UserId(71)),
"/users/71/application-passwords?context=embed",
);
}

#[rstest]
fn list_application_passwords_with_view_context(endpoint: ApplicationPasswordsRequestEndpoint) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.list_with_view_context(&UserId(9999)),
"/users/9999/application-passwords?context=view",
);
Expand All @@ -104,7 +105,7 @@ mod tests {
#[case] fields: &[SparseApplicationPasswordField],
#[case] expected_path: &str,
) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.filter_list(&UserId(2), context, fields),
expected_path,
);
Expand All @@ -114,7 +115,7 @@ mod tests {
fn retrieve_current_application_passwords_with_edit_context(
endpoint: ApplicationPasswordsRequestEndpoint,
) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.retrieve_current_with_edit_context(&UserId(2)),
"/users/2/application-passwords/introspect?context=edit",
);
Expand All @@ -129,7 +130,7 @@ mod tests {
#[case] fields: &[SparseApplicationPasswordField],
#[case] expected_path: &str,
) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.filter_retrieve_current(&UserId(2), context, fields),
expected_path,
);
Expand All @@ -139,7 +140,7 @@ mod tests {
fn retrieve_application_passwords_with_embed_context(
endpoint: ApplicationPasswordsRequestEndpoint,
) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.retrieve_with_embed_context(
&UserId(2),
&ApplicationPasswordUuid {
Expand All @@ -162,15 +163,15 @@ mod tests {
let uuid = ApplicationPasswordUuid {
uuid: "584a87d5-4f18-4c33-a315-4c05ed1fc485".to_string(),
};
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.filter_retrieve(&UserId(2), &uuid, context, fields),
expected_path,
);
}

#[rstest]
fn update_application_password(endpoint: ApplicationPasswordsRequestEndpoint) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.update(
&UserId(2),
&ApplicationPasswordUuid {
Expand Down
21 changes: 11 additions & 10 deletions wp_api/src/request/endpoint/plugins_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use wp_derive_request_builder::WpDerivedRequest;

#[derive(WpDerivedRequest)]
#[Namespace("/wp/v2")]
#[SparseField(SparsePluginField)]
enum PluginsRequest {
#[post(url = "/plugins", params = &PluginCreateParams, output = PluginWithEditContext)]
Expand All @@ -26,7 +27,7 @@ mod tests {
use crate::{
generate,
request::endpoint::{
tests::{fixture_api_base_url, validate_endpoint},
tests::{fixture_api_base_url, validate_wp_v2_endpoint},
ApiBaseUrl,
},
PluginStatus, WpContext,
Expand All @@ -36,7 +37,7 @@ mod tests {

#[rstest]
fn create_plugin(endpoint: PluginsRequestEndpoint) {
validate_endpoint(endpoint.create(), "/plugins");
validate_wp_v2_endpoint(endpoint.create(), "/plugins");
}

#[rstest]
Expand All @@ -52,7 +53,7 @@ mod tests {
#[case] plugin_slug: PluginSlug,
#[case] expected_path: &str,
) {
validate_endpoint(endpoint.delete(&plugin_slug), expected_path);
validate_wp_v2_endpoint(endpoint.delete(&plugin_slug), expected_path);
}

#[rstest]
Expand All @@ -65,7 +66,7 @@ mod tests {
#[case] params: PluginListParams,
#[case] expected_path: &str,
) {
validate_endpoint(endpoint.list_with_edit_context(&params), expected_path);
validate_wp_v2_endpoint(endpoint.list_with_edit_context(&params), expected_path);
}

#[rstest]
Expand All @@ -76,7 +77,7 @@ mod tests {
#[case] params: PluginListParams,
#[case] expected_path: &str,
) {
validate_endpoint(endpoint.list_with_embed_context(&params), expected_path);
validate_wp_v2_endpoint(endpoint.list_with_embed_context(&params), expected_path);
}

#[rstest]
Expand All @@ -87,7 +88,7 @@ mod tests {
#[case] params: PluginListParams,
#[case] expected_path: &str,
) {
validate_endpoint(endpoint.list_with_view_context(&params), expected_path);
validate_wp_v2_endpoint(endpoint.list_with_view_context(&params), expected_path);
}

#[rstest]
Expand Down Expand Up @@ -123,7 +124,7 @@ mod tests {
#[case] fields: &[SparsePluginField],
#[case] expected_path: &str,
) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.filter_list(context, &params, fields),
expected_path,
);
Expand All @@ -148,7 +149,7 @@ mod tests {
#[case] plugin_slug: PluginSlug,
#[case] expected_path: &str,
) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.retrieve_with_view_context(&plugin_slug),
expected_path,
);
Expand Down Expand Up @@ -186,7 +187,7 @@ mod tests {
#[case] fields: &[SparsePluginField],
#[case] expected_path: &str,
) {
validate_endpoint(
validate_wp_v2_endpoint(
endpoint.filter_retrieve(&plugin_slug, context, fields),
expected_path,
);
Expand All @@ -205,7 +206,7 @@ mod tests {
#[case] plugin_slug: PluginSlug,
#[case] expected_path: &str,
) {
validate_endpoint(endpoint.update(&plugin_slug), expected_path);
validate_wp_v2_endpoint(endpoint.update(&plugin_slug), expected_path);
}

#[fixture]
Expand Down
Loading