-
Notifications
You must be signed in to change notification settings - Fork 3
Pagination support for /users
#375
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
Conversation
This file contains hidden or 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
Contributor
jkmassel
left a comment
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
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_paramsfields 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_paramsfields, but the#[contextual_get]doesn't.Also notice that the type of
next_page_params&prev_page_paramsareOption<UserListParams>. This is theparamsfield that's passed in the#[contextual_paged]attribute.WordPress endpoints that support pagination will have a header link with
next&prevheaders when it's appropriate. As far as I can tell,/usersendpoint will include these headers if theper_pageparameter is provided. So, at this time, the response for the defaultUserListParamswill not include them. This issue will be handled in a separate PR.The way this implementation works is by checking the
nextandprevheader links for params types that support it. For a params type to support pagination, it needs to returntruefrom itsFromUrlQueryPairs::supports_pagination()implementation. All params type that implements this trait should returntruefor this function, except for the()type which is used as a marker to indicate pagination is not supported.FromUrlQueryPairstrait is how we parse the header links into params type - using itsfrom_url_query_pairsfunction 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::FromStrorwp_api::OptionFromStrtraits. For example, sinceUserListParamshaspub who: Option<WpApiParamUsersWho>field,WpApiParamUsersWhohas to implement thestd::str::FromStrtrait.We now have
AppendUrlQueryPairs&FromUrlQueryPairstraits 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 UserListParamsFieldis added. This enum converts the field names forUserListParamsinto&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
UserListParamsto query pairs and then parsing it back toUserListParamsand compare the two. It also includes pagination Rust & Kotlin integration tests for/usersendpoint.There are also other unit & integration tests to validate the behavior changes to
#[WpDerivedRequest].