Skip to content

Adds PluginSlug & PluginWpOrgDirectorySlug #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions wp_api/src/endpoint/plugins_endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use url::Url;

use crate::{plugins::PluginListParams, ApiBaseUrl, WPContext};
use crate::{plugins::PluginListParams, ApiBaseUrl, PluginSlug, WPContext};

pub struct PluginsEndpoint {
api_base_url: ApiBaseUrl,
Expand All @@ -15,7 +15,7 @@ impl PluginsEndpoint {
self.plugins_base_url()
}

pub fn delete(&self, plugin: &str) -> Url {
pub fn delete(&self, plugin: &PluginSlug) -> Url {
self.plugins_url_with_slug(plugin)
}

Expand All @@ -29,25 +29,25 @@ impl PluginsEndpoint {
url
}

pub fn retrieve(&self, context: WPContext, plugin: &str) -> Url {
pub fn retrieve(&self, context: WPContext, plugin: &PluginSlug) -> Url {
let mut url = self.plugins_url_with_slug(plugin);
url.query_pairs_mut()
.append_pair("context", context.as_str());
url
}

pub fn update(&self, plugin: &str) -> Url {
pub fn update(&self, plugin: &PluginSlug) -> Url {
self.plugins_url_with_slug(plugin)
}

fn plugins_base_url(&self) -> Url {
self.api_base_url.by_appending("plugins")
}

fn plugins_url_with_slug(&self, plugin: &str) -> Url {
fn plugins_url_with_slug(&self, plugin: &PluginSlug) -> Url {
self.api_base_url
// The '/' character has to be preserved and not get encoded
.by_extending(["plugins"].into_iter().chain(plugin.split('/')))
.by_extending(["plugins"].into_iter().chain(plugin.slug.split('/')))
}
}

Expand All @@ -66,19 +66,19 @@ mod tests {
}

#[rstest]
#[case("hello-dolly/hello", "/plugins/hello-dolly/hello")]
#[case("hello-dolly/hello".into(), "/plugins/hello-dolly/hello")]
#[case(
"classic-editor/classic-editor",
"classic-editor/classic-editor".into(),
"/plugins/classic-editor/classic-editor"
)]
#[case("foo/bar%baz", "/plugins/foo/bar%25baz")]
#[case("foo/です", "/plugins/foo/%E3%81%A7%E3%81%99")]
#[case("foo/bar%baz".into(), "/plugins/foo/bar%25baz")]
#[case("foo/です".into(), "/plugins/foo/%E3%81%A7%E3%81%99")]
fn delete_plugin(
plugins_endpoint: PluginsEndpoint,
#[case] plugin_slug: &str,
#[case] plugin_slug: PluginSlug,
#[case] expected_path: &str,
) {
validate_endpoint(plugins_endpoint.delete(plugin_slug), expected_path);
validate_endpoint(plugins_endpoint.delete(&plugin_slug), expected_path);
}

#[rstest]
Expand All @@ -96,47 +96,47 @@ mod tests {

#[rstest]
#[case(
"hello-dolly/hello",
"hello-dolly/hello".into(),
WPContext::View,
"/plugins/hello-dolly/hello?context=view"
)]
#[case(
"classic-editor/classic-editor",
"classic-editor/classic-editor".into(),
WPContext::Embed,
"/plugins/classic-editor/classic-editor?context=embed"
)]
#[case("foo/bar%baz", WPContext::Edit, "/plugins/foo/bar%25baz?context=edit")]
#[case("foo/bar%baz".into(), WPContext::Edit, "/plugins/foo/bar%25baz?context=edit")]
#[case(
"foo/です",
"foo/です".into(),
WPContext::View,
"/plugins/foo/%E3%81%A7%E3%81%99?context=view"
)]
fn retrieve_plugin(
plugins_endpoint: PluginsEndpoint,
#[case] plugin_slug: &str,
#[case] plugin_slug: PluginSlug,
#[case] context: WPContext,
#[case] expected_path: &str,
) {
validate_endpoint(
plugins_endpoint.retrieve(context, plugin_slug),
plugins_endpoint.retrieve(context, &plugin_slug),
expected_path,
);
}

#[rstest]
#[case("hello-dolly/hello", "/plugins/hello-dolly/hello")]
#[case("hello-dolly/hello".into(), "/plugins/hello-dolly/hello")]
#[case(
"classic-editor/classic-editor",
"classic-editor/classic-editor".into(),
"/plugins/classic-editor/classic-editor"
)]
#[case("foo/bar%baz", "/plugins/foo/bar%25baz")]
#[case("foo/です", "/plugins/foo/%E3%81%A7%E3%81%99")]
#[case("foo/bar%baz".into(), "/plugins/foo/bar%25baz")]
#[case("foo/です".into(), "/plugins/foo/%E3%81%A7%E3%81%99")]
fn update_plugin(
plugins_endpoint: PluginsEndpoint,
#[case] plugin_slug: &str,
#[case] plugin_slug: PluginSlug,
#[case] expected_path: &str,
) {
validate_endpoint(plugins_endpoint.update(plugin_slug), expected_path);
validate_endpoint(plugins_endpoint.update(&plugin_slug), expected_path);
}

#[fixture]
Expand Down
10 changes: 7 additions & 3 deletions wp_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ impl WPApiHelper {
}
}

pub fn retrieve_plugin_request(&self, context: WPContext, plugin: &str) -> WPNetworkRequest {
pub fn retrieve_plugin_request(
&self,
context: WPContext,
plugin: &PluginSlug,
) -> WPNetworkRequest {
WPNetworkRequest {
method: RequestMethod::GET,
url: self.api_endpoint.plugins.retrieve(context, plugin).into(),
Expand All @@ -262,7 +266,7 @@ impl WPApiHelper {

pub fn update_plugin_request(
&self,
plugin: &str,
plugin: &PluginSlug,
params: PluginUpdateParams,
) -> WPNetworkRequest {
WPNetworkRequest {
Expand All @@ -273,7 +277,7 @@ impl WPApiHelper {
}
}

pub fn delete_plugin_request(&self, plugin: &str) -> WPNetworkRequest {
pub fn delete_plugin_request(&self, plugin: &PluginSlug) -> WPNetworkRequest {
WPNetworkRequest {
method: RequestMethod::DELETE,
url: self.api_endpoint.plugins.delete(plugin).into(),
Expand Down
32 changes: 30 additions & 2 deletions wp_api/src/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl PluginListParams {
#[derive(Debug, Serialize, uniffi::Record)]
pub struct PluginCreateParams {
/// WordPress.org plugin directory slug.
pub slug: String,
pub slug: PluginWpOrgDirectorySlug,
/// The plugin activation status.
pub status: PluginStatus,
}
Expand All @@ -72,7 +72,7 @@ pub struct PluginUpdateParams {
#[derive(Debug, Serialize, Deserialize, uniffi::Record, WPContextual)]
pub struct SparsePlugin {
#[WPContext(edit, embed, view)]
pub plugin: Option<String>,
pub plugin: Option<PluginSlug>,
#[WPContext(edit, embed, view)]
pub status: Option<PluginStatus>,
#[WPContext(edit, embed, view)]
Expand All @@ -99,6 +99,34 @@ pub struct PluginDeleteResponse {
pub previous: PluginWithEditContext,
}

#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
#[serde(transparent)]
pub struct PluginSlug {
pub slug: String,
}

impl From<&str> for PluginSlug {
fn from(value: &str) -> Self {
Self {
slug: value.to_string(),
}
}
}

#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
#[serde(transparent)]
pub struct PluginWpOrgDirectorySlug {
pub slug: String,
}

impl From<&str> for PluginWpOrgDirectorySlug {
fn from(value: &str) -> Self {
Self {
slug: value.to_string(),
}
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding some docs (similar to your PR description) to these two types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at all, but do you mind if that happens as part of the general documentation effort that's scheduled in the next milestone-ish?

I am not sure about the format yet, so I don't want to add a random one at the moment. However, be assured that everything public will have documentation soon 😅

#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, uniffi::Enum)]
pub enum PluginStatus {
#[serde(rename = "active")]
Expand Down
14 changes: 7 additions & 7 deletions wp_networking/tests/test_plugins_immut.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rstest::*;
use wp_api::{generate, plugins::PluginListParams, plugins::PluginStatus, WPContext};
use wp_api::{generate, plugins::PluginListParams, plugins::PluginStatus, PluginSlug, WPContext};

use crate::test_helpers::{
api, WPNetworkRequestExecutor, WPNetworkResponseParser, CLASSIC_EDITOR_PLUGIN_SLUG,
Expand Down Expand Up @@ -53,24 +53,24 @@ async fn test_plugin_list_params_parametrized(
}

#[rstest]
#[case(CLASSIC_EDITOR_PLUGIN_SLUG, "WordPress Contributors")]
#[case(HELLO_DOLLY_PLUGIN_SLUG, "Matt Mullenweg")]
#[case(CLASSIC_EDITOR_PLUGIN_SLUG.into(), "WordPress Contributors")]
#[case(HELLO_DOLLY_PLUGIN_SLUG.into(), "Matt Mullenweg")]
#[trace]
#[tokio::test]
async fn retrieve_plugin(
#[case] plugin_slug: &str,
async fn retrieve_plugin_with_edit_context(
#[case] plugin_slug: PluginSlug,
#[case] expected_author: &str,
#[values(WPContext::Edit, WPContext::Embed, WPContext::View)] context: WPContext,
) {
let parsed_response = api()
.retrieve_plugin_request(context, plugin_slug)
.retrieve_plugin_request(context, &plugin_slug)
.execute()
.await
.unwrap()
.parse(wp_api::parse_retrieve_plugin_response_with_edit_context);
assert!(
parsed_response.is_ok(),
"Retrieve plugin failed!\nContext: {:?}\nPlugin: {}\nResponse was: '{:?}'",
"Retrieve plugin failed!\nContext: {:?}\nPlugin: {:?}\nResponse was: '{:?}'",
context,
plugin_slug,
parsed_response
Expand Down
9 changes: 6 additions & 3 deletions wp_networking/tests/test_plugins_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async fn create_plugin() {
wp_db::run_and_restore(|mut _db| async move {
let status = PluginStatus::Active;
let params = PluginCreateParams {
slug: WP_ORG_PLUGIN_SLUG_CLASSIC_WIDGETS.to_string(),
slug: WP_ORG_PLUGIN_SLUG_CLASSIC_WIDGETS.into(),
status,
};
let created_plugin = api()
Expand All @@ -36,7 +36,10 @@ async fn update_plugin() {
wp_db::run_and_restore(|mut _db| async move {
let status = PluginStatus::Active;
let updated_plugin = api()
.update_plugin_request(HELLO_DOLLY_PLUGIN_SLUG, PluginUpdateParams { status })
.update_plugin_request(
&HELLO_DOLLY_PLUGIN_SLUG.into(),
PluginUpdateParams { status },
)
.execute()
.await
.unwrap()
Expand All @@ -53,7 +56,7 @@ async fn delete_plugin() {
run_and_restore_wp_content_plugins(|| {
wp_db::run_and_restore(|mut _db| async move {
let deleted_plugin = api()
.delete_plugin_request(CLASSIC_EDITOR_PLUGIN_SLUG)
.delete_plugin_request(&CLASSIC_EDITOR_PLUGIN_SLUG.into())
.execute()
.await
.unwrap()
Expand Down