-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
412 additions
and
17 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
native/kotlin/api/kotlin/src/integrationTest/kotlin/TagsEndpointTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package rs.wordpress.api.kotlin | ||
|
||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Test | ||
import uniffi.wp_api.TagListParams | ||
import uniffi.wp_api.wpAuthenticationFromUsernameAndPassword | ||
|
||
class TagsEndpointTest { | ||
private val testCredentials = TestCredentials.INSTANCE | ||
private val siteUrl = testCredentials.parsedSiteUrl | ||
private val authentication = wpAuthenticationFromUsernameAndPassword( | ||
username = testCredentials.adminUsername, password = testCredentials.adminPassword | ||
) | ||
private val client = WpApiClient(siteUrl, authentication) | ||
|
||
@Test | ||
fun testTagListRequest() = runTest { | ||
val tagList = client.request { requestBuilder -> | ||
requestBuilder.tags().listWithEditContext(params = TagListParams()) | ||
}.assertSuccessAndRetrieveData().data | ||
assert(tagList.isNotEmpty()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use super::{AsNamespace, DerivedRequest, WpNamespace}; | ||
use crate::{ | ||
tags::{ | ||
SparseTagFieldWithEditContext, SparseTagFieldWithEmbedContext, | ||
SparseTagFieldWithViewContext, TagListParams, | ||
}, | ||
SparseField, | ||
}; | ||
use wp_derive_request_builder::WpDerivedRequest; | ||
|
||
#[derive(WpDerivedRequest)] | ||
enum TagsRequest { | ||
#[contextual_paged(url = "/tags", params = &TagListParams, output = Vec<crate::tags::SparseTag>, filter_by = crate::tags::SparseTagField)] | ||
List, | ||
} | ||
|
||
impl DerivedRequest for TagsRequest { | ||
fn namespace() -> impl AsNamespace { | ||
WpNamespace::WpV2 | ||
} | ||
} | ||
|
||
super::macros::default_sparse_field_implementation_from_field_name!(SparseTagFieldWithEditContext); | ||
super::macros::default_sparse_field_implementation_from_field_name!(SparseTagFieldWithEmbedContext); | ||
super::macros::default_sparse_field_implementation_from_field_name!(SparseTagFieldWithViewContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
use std::{num::ParseIntError, str::FromStr}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use strum_macros::IntoStaticStr; | ||
use wp_contextual::WpContextual; | ||
|
||
use crate::{ | ||
impl_as_query_value_for_new_type, impl_as_query_value_from_as_str, | ||
posts::PostId, | ||
taxonomies::TaxonomyType, | ||
url_query::{ | ||
AppendUrlQueryPairs, AsQueryValue, FromUrlQueryPairs, QueryPairs, QueryPairsExtension, | ||
UrlQueryPairsMap, | ||
}, | ||
EnumFromStrParsingError, WpApiParamOrder, | ||
}; | ||
|
||
impl_as_query_value_for_new_type!(TagId); | ||
uniffi::custom_newtype!(TagId, i64); | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] | ||
pub struct TagId(pub i64); | ||
|
||
impl FromStr for TagId { | ||
type Err = ParseIntError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
s.parse().map(Self) | ||
} | ||
} | ||
|
||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, uniffi::Enum)] | ||
pub enum WpApiParamTagsOrderBy { | ||
Id, | ||
Include, | ||
#[default] | ||
Name, | ||
Slug, | ||
IncludeSlugs, | ||
TermGroup, | ||
Description, | ||
Count, | ||
} | ||
|
||
impl_as_query_value_from_as_str!(WpApiParamTagsOrderBy); | ||
|
||
impl WpApiParamTagsOrderBy { | ||
fn as_str(&self) -> &str { | ||
match self { | ||
Self::Id => "id", | ||
Self::Include => "include", | ||
Self::Name => "name", | ||
Self::Slug => "slug", | ||
Self::IncludeSlugs => "include_slugs", | ||
Self::TermGroup => "term_group", | ||
Self::Description => "description", | ||
Self::Count => "count", | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for WpApiParamTagsOrderBy { | ||
type Err = EnumFromStrParsingError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"id" => Ok(Self::Id), | ||
"include" => Ok(Self::Include), | ||
"name" => Ok(Self::Name), | ||
"slug" => Ok(Self::Slug), | ||
"include_slugs" => Ok(Self::IncludeSlugs), | ||
"term_group" => Ok(Self::TermGroup), | ||
"description" => Ok(Self::Description), | ||
"count" => Ok(Self::Count), | ||
value => Err(EnumFromStrParsingError::UnknownVariant { | ||
value: value.to_string(), | ||
}), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Default, PartialEq, Eq, uniffi::Record)] | ||
pub struct TagListParams { | ||
/// Current page of the collection. | ||
/// Default: `1` | ||
#[uniffi(default = None)] | ||
pub page: Option<u32>, | ||
/// Maximum number of items to be returned in result set. | ||
/// Default: `10` | ||
#[uniffi(default = None)] | ||
pub per_page: Option<u32>, | ||
/// Limit results to those matching a string. | ||
#[uniffi(default = None)] | ||
pub search: Option<String>, | ||
/// Ensure result set excludes specific IDs. | ||
#[uniffi(default = [])] | ||
pub exclude: Vec<TagId>, | ||
/// Limit result set to specific IDs. | ||
#[uniffi(default = [])] | ||
pub include: Vec<TagId>, | ||
/// Offset the result set by a specific number of items. | ||
#[uniffi(default = None)] | ||
pub offset: Option<u32>, | ||
/// Order sort attribute ascending or descending. | ||
/// Default: `asc` | ||
/// One of: `asc`, `desc` | ||
#[uniffi(default = None)] | ||
pub order: Option<WpApiParamOrder>, | ||
/// Sort collection by user attribute. | ||
/// Default: `name` | ||
/// One of: `id`, `include`, `name`, `slug`, `include_slugs`, `term_group`, `description`, `count` | ||
#[uniffi(default = None)] | ||
pub orderby: Option<WpApiParamTagsOrderBy>, | ||
/// Whether to hide terms not assigned to any posts. | ||
#[uniffi(default = None)] | ||
pub hide_empty: Option<bool>, | ||
/// Limit result set to terms assigned to a specific post. | ||
#[uniffi(default = None)] | ||
pub post: Option<PostId>, | ||
/// Limit result set to users with one or more specific slugs. | ||
#[uniffi(default = [])] | ||
pub slug: Vec<String>, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, IntoStaticStr)] | ||
enum TagListParamsField { | ||
#[strum(serialize = "page")] | ||
Page, | ||
#[strum(serialize = "per_page")] | ||
PerPage, | ||
#[strum(serialize = "search")] | ||
Search, | ||
#[strum(serialize = "exclude")] | ||
Exclude, | ||
#[strum(serialize = "include")] | ||
Include, | ||
#[strum(serialize = "offset")] | ||
Offset, | ||
#[strum(serialize = "order")] | ||
Order, | ||
#[strum(serialize = "orderby")] | ||
Orderby, | ||
#[strum(serialize = "hide_empty")] | ||
HideEmpty, | ||
#[strum(serialize = "post")] | ||
Post, | ||
#[strum(serialize = "slug")] | ||
Slug, | ||
} | ||
|
||
impl AppendUrlQueryPairs for TagListParams { | ||
fn append_query_pairs(&self, query_pairs_mut: &mut QueryPairs) { | ||
query_pairs_mut | ||
.append_option_query_value_pair(TagListParamsField::Page, self.page.as_ref()) | ||
.append_option_query_value_pair(TagListParamsField::PerPage, self.per_page.as_ref()) | ||
.append_option_query_value_pair(TagListParamsField::Search, self.search.as_ref()) | ||
.append_vec_query_value_pair(TagListParamsField::Exclude, &self.exclude) | ||
.append_vec_query_value_pair(TagListParamsField::Include, &self.include) | ||
.append_option_query_value_pair(TagListParamsField::Offset, self.offset.as_ref()) | ||
.append_option_query_value_pair(TagListParamsField::Order, self.order.as_ref()) | ||
.append_option_query_value_pair(TagListParamsField::Orderby, self.orderby.as_ref()) | ||
.append_option_query_value_pair(TagListParamsField::HideEmpty, self.hide_empty.as_ref()) | ||
.append_option_query_value_pair(TagListParamsField::Post, self.post.as_ref()) | ||
.append_vec_query_value_pair(TagListParamsField::Slug, &self.slug); | ||
} | ||
} | ||
|
||
impl FromUrlQueryPairs for TagListParams { | ||
fn from_url_query_pairs(query_pairs: UrlQueryPairsMap) -> Option<Self> { | ||
Some(Self { | ||
page: query_pairs.get(TagListParamsField::Page), | ||
per_page: query_pairs.get(TagListParamsField::PerPage), | ||
search: query_pairs.get(TagListParamsField::Search), | ||
exclude: query_pairs.get_csv(TagListParamsField::Exclude), | ||
include: query_pairs.get_csv(TagListParamsField::Include), | ||
offset: query_pairs.get(TagListParamsField::Offset), | ||
order: query_pairs.get(TagListParamsField::Order), | ||
orderby: query_pairs.get(TagListParamsField::Orderby), | ||
hide_empty: query_pairs.get(TagListParamsField::HideEmpty), | ||
post: query_pairs.get(TagListParamsField::Post), | ||
slug: query_pairs.get_csv(TagListParamsField::Slug), | ||
}) | ||
} | ||
|
||
fn supports_pagination() -> bool { | ||
true | ||
} | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, uniffi::Record, WpContextual)] | ||
pub struct SparseTag { | ||
#[WpContext(edit, embed, view)] | ||
pub id: Option<TagId>, | ||
#[WpContext(edit, view)] | ||
pub count: Option<i64>, | ||
#[WpContext(edit, view)] | ||
pub description: Option<String>, | ||
#[WpContext(edit, embed, view)] | ||
pub link: Option<String>, | ||
#[WpContext(edit, embed, view)] | ||
pub name: Option<String>, | ||
#[WpContext(edit, embed, view)] | ||
pub slug: Option<String>, | ||
#[WpContext(edit, embed, view)] | ||
pub taxonomy: Option<TaxonomyType>, | ||
// meta field is omitted for now: https://github.com/Automattic/wordpress-rs/issues/463 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.