Skip to content
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
16 changes: 16 additions & 0 deletions wp_api/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use url::Url;
pub use plugins_endpoint::*;
pub use users_endpoint::*;

use crate::SparseField;

mod plugins_endpoint;
mod users_endpoint;

Expand Down Expand Up @@ -72,6 +74,7 @@ trait UrlExtension {
where
I: IntoIterator,
I::Item: AsRef<str>;
fn append_filter_fields(self, fields: &[impl SparseField]) -> Url;
}

impl UrlExtension for Url {
Expand All @@ -88,6 +91,19 @@ impl UrlExtension for Url {
self.path_segments_mut()?.extend(segments);
Ok(self)
}

fn append_filter_fields(mut self, fields: &[impl SparseField]) -> Url {
self.query_pairs_mut().append_pair(
"_fields",
fields
.iter()
.map(|f| f.as_str())
.collect::<Vec<&str>>()
.join(",")
.as_str(),
);
self
}
}

#[cfg(test)]
Expand Down
19 changes: 4 additions & 15 deletions wp_api/src/endpoint/plugins_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use url::Url;

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

use super::UrlExtension;

pub struct PluginsEndpoint {
api_base_url: ApiBaseUrl,
}
Expand Down Expand Up @@ -35,7 +37,7 @@ impl PluginsEndpoint {
params: Option<&PluginListParams>,
fields: &[SparsePluginField],
) -> Url {
self.append_filter_fields(self.list(context, params), fields)
self.list(context, params).append_filter_fields(fields)
}

pub fn retrieve(&self, context: WPContext, plugin: &PluginSlug) -> Url {
Expand All @@ -51,7 +53,7 @@ impl PluginsEndpoint {
plugin: &PluginSlug,
fields: &[SparsePluginField],
) -> Url {
self.append_filter_fields(self.retrieve(context, plugin), fields)
self.retrieve(context, plugin).append_filter_fields(fields)
}

pub fn update(&self, plugin: &PluginSlug) -> Url {
Expand All @@ -67,19 +69,6 @@ impl PluginsEndpoint {
// The '/' character has to be preserved and not get encoded
.by_extending(["plugins"].into_iter().chain(plugin.slug.split('/')))
}

fn append_filter_fields(&self, mut url: Url, fields: &[SparsePluginField]) -> Url {
url.query_pairs_mut().append_pair(
"_fields",
fields
.iter()
.map(|f| f.as_str())
.collect::<Vec<&str>>()
.join(",")
.as_str(),
);
url
}
}

#[cfg(test)]
Expand Down
21 changes: 5 additions & 16 deletions wp_api/src/endpoint/users_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use url::Url;

use crate::{ApiBaseUrl, SparseUserField, UserDeleteParams, UserId, UserListParams, WPContext};

use super::UrlExtension;

pub struct UsersEndpoint {
api_base_url: ApiBaseUrl,
}
Expand Down Expand Up @@ -45,7 +47,7 @@ impl UsersEndpoint {
params: Option<&UserListParams>,
fields: &[SparseUserField],
) -> Url {
self.append_filter_fields(self.list(context, params), fields)
self.list(context, params).append_filter_fields(fields)
}

pub fn retrieve(&self, user_id: UserId, context: WPContext) -> Url {
Expand All @@ -63,7 +65,7 @@ impl UsersEndpoint {
context: WPContext,
fields: &[SparseUserField],
) -> Url {
self.append_filter_fields(self.retrieve(user_id, context), fields)
self.retrieve(user_id, context).append_filter_fields(fields)
}

pub fn retrieve_me(&self, context: WPContext) -> Url {
Expand All @@ -74,7 +76,7 @@ impl UsersEndpoint {
}

pub fn filter_retrieve_me(&self, context: WPContext, fields: &[SparseUserField]) -> Url {
self.append_filter_fields(self.retrieve_me(context), fields)
self.retrieve_me(context).append_filter_fields(fields)
}

pub fn update(&self, user_id: UserId) -> Url {
Expand All @@ -85,19 +87,6 @@ impl UsersEndpoint {
pub fn update_me(&self) -> Url {
self.api_base_url.by_extending(["users", "me"])
}

fn append_filter_fields(&self, mut url: Url, fields: &[SparseUserField]) -> Url {
url.query_pairs_mut().append_pair(
"_fields",
fields
.iter()
.map(|f| f.as_str())
.collect::<Vec<&str>>()
.join(",")
.as_str(),
);
url
}
}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions wp_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ pub fn get_link_header(response: &WPNetworkResponse, name: &str) -> Option<WPRes
None
}

trait SparseField {
fn as_str(&self) -> &str;
}

#[macro_export]
macro_rules! add_uniffi_exported_parser {
($fn_name:ident, $return_type: ty) => {
Expand Down
8 changes: 5 additions & 3 deletions wp_api/src/plugins.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use serde::{Deserialize, Serialize};
use wp_derive::WPContextual;

use crate::{add_uniffi_exported_parser, parse_wp_response, WPApiError, WPNetworkResponse};
use crate::{
add_uniffi_exported_parser, parse_wp_response, SparseField, WPApiError, WPNetworkResponse,
};

add_uniffi_exported_parser!(parse_filter_plugins_response, Vec<SparsePlugin>);
add_uniffi_exported_parser!(parse_filter_retrieve_plugin_response, SparsePlugin);
Expand Down Expand Up @@ -109,8 +111,8 @@ pub enum SparsePluginField {
Version,
}

impl SparsePluginField {
pub fn as_str(&self) -> &str {
impl SparseField for SparsePluginField {
fn as_str(&self) -> &str {
match self {
Self::Author => "author",
Self::Description => "description",
Expand Down
7 changes: 4 additions & 3 deletions wp_api/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use serde::{Deserialize, Serialize};
use wp_derive::WPContextual;

use crate::{
add_uniffi_exported_parser, parse_wp_response, WPApiError, WPApiParamOrder, WPNetworkResponse,
add_uniffi_exported_parser, parse_wp_response, SparseField, WPApiError, WPApiParamOrder,
WPNetworkResponse,
};

add_uniffi_exported_parser!(parse_filter_users_response, Vec<SparseUser>);
Expand Down Expand Up @@ -394,8 +395,8 @@ pub enum SparseUserField {
// meta field is omitted for now: https://github.com/Automattic/wordpress-rs/issues/57
}

impl SparseUserField {
pub fn as_str(&self) -> &str {
impl SparseField for SparseUserField {
fn as_str(&self) -> &str {
match self {
Self::Id => "id",
Self::Username => "username",
Expand Down