Skip to content
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

Export parse_as_response_type functions #395

Merged
merged 2 commits into from
Nov 20, 2024
Merged

Conversation

oguzkocer
Copy link
Contributor

@oguzkocer oguzkocer commented Nov 14, 2024

We allow two different ways to work with this library.

The main way is to use the WpApiClient type which exposes endpoint specific request executors which executes the request and parses the response as such:

let api_client = WpApiClient::new(/*_arguments_*/);
let result: Result<UsersRequestListWithEditContextResponse, WpApiError> = api_client
    .users()
    .list_with_edit_context(&UserListParams::default())
    .await;

We also provide an alternative way where building & executing the request as well as parsing it has to be done in individual steps. We always allowed building the request with the WpApiRequestBuilder, however one had to manually parse it. This is relatively easy to do in Rust by using the WpNetworkResponse::parse function, but native wrappers had to implement a parser of their own.

This PR generates these individual parsers, so native wrappers can use the Rust response parser. Here is how that looks in Kotlin:

val requestBuilder = UniffiWpApiRequestBuilder(siteUrl, authentication)
val userListRequest = requestBuilder.users().listWithEditContext(UserListParams())
val userListResponse = requestExecutor.execute(userListRequest)
val userList = parseAsUsersRequestListWithEditContextResponse(userListResponse).data

This looks very similar to the WpApiClient approach, but here we execute the given request ourselves and then pick the correct parser. Note that this approach isn't recommended unless you need the extra flexibility because it's both easier and safer to use the WpApiClient since it'll choose the correct parser for you.

@oguzkocer oguzkocer added the Rust label Nov 14, 2024
@oguzkocer oguzkocer added this to the 0.2 milestone Nov 14, 2024
@oguzkocer oguzkocer requested a review from jkmassel November 18, 2024 23:11
@oguzkocer oguzkocer marked this pull request as ready for review November 18, 2024 23:58
@oguzkocer oguzkocer enabled auto-merge (squash) November 18, 2024 23:58
Copy link
Contributor

@jkmassel jkmassel left a comment

Choose a reason for hiding this comment

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

This works great (as tested by #396)

@oguzkocer oguzkocer merged commit 9731a24 into trunk Nov 20, 2024
22 checks passed
@oguzkocer oguzkocer deleted the parse_as_response_type branch November 20, 2024 01:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants