-
Notifications
You must be signed in to change notification settings - Fork 0
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
Pagination support for /users
#375
Merged
Merged
Conversation
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
jkmassel
approved these changes
Nov 12, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this from Swift and it worked great – the test coverage here is good, so I'm confident in the codegen stuff
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds strongly typed
next_page_params
&prev_page_params
fields to the response types that supports pagination. The response types are generated by#[derive(WpDerivedRequest)]
, so here is a shortened example of how that looks:The above implementation will result in the following generated code:
Notice that there is now a new
#[derive(WpDerivedRequest)]
attribute:#[contextual_paged]
. This attribute works exactly the same as#[contextual_get]
. The only difference is#[contextual_paged]
response types will havenext_page_params
&prev_page_params
fields, but the#[contextual_get]
doesn't.Also notice that the type of
next_page_params
&prev_page_params
areOption<UserListParams>
. This is theparams
field that's passed in the#[contextual_paged]
attribute.WordPress endpoints that support pagination will have a header link with
next
&prev
headers when it's appropriate. As far as I can tell,/users
endpoint will include these headers if theper_page
parameter is provided. So, at this time, the response for the defaultUserListParams
will not include them. This issue will be handled in a separate PR.The way this implementation works is by checking the
next
andprev
header links for params types that support it. For a params type to support pagination, it needs to returntrue
from itsFromUrlQueryPairs::supports_pagination()
implementation. All params type that implements this trait should returntrue
for this function, except for the()
type which is used as a marker to indicate pagination is not supported.FromUrlQueryPairs
trait is how we parse the header links into params type - using itsfrom_url_query_pairs
function implementation. There are a few helpers to do this, so its implementation for a params type is mostly straightforward. However, these helpers require the types used in a params type to implementstd::str::FromStr
orwp_api::OptionFromStr
traits. For example, sinceUserListParams
haspub who: Option<WpApiParamUsersWho>
field,WpApiParamUsersWho
has to implement thestd::str::FromStr
trait.We now have
AppendUrlQueryPairs
&FromUrlQueryPairs
traits to convert the params types to and from url query pairs. Since both of these implementations should use the same keys for the query pairs, a newenum UserListParamsField
is added. This enum converts the field names forUserListParams
into&str
. It'd be easy to generate these field types from a proc macro, but for now they'll be hand rolled.The PR includes unit tests to validate going from a
UserListParams
to query pairs and then parsing it back toUserListParams
and compare the two. It also includes pagination Rust & Kotlin integration tests for/users
endpoint.There are also other unit & integration tests to validate the behavior changes to
#[WpDerivedRequest]
.