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
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
1 change: 1 addition & 0 deletions native/kotlin/api/kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ testing {
dependencies {
implementation(project())

implementation(libs.okhttp)
implementation(rootProject.libs.kotlin.test)
implementation(rootProject.libs.kotlinx.coroutines.test)
implementation(libs.kotlinx.serialization)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package rs.wordpress.api.kotlin

import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import uniffi.wp_api.UniffiWpApiRequestBuilder
import uniffi.wp_api.UserListParams
import uniffi.wp_api.parseAsUsersRequestListWithEditContextResponse
import uniffi.wp_api.wpAuthenticationFromUsernameAndPassword
import kotlin.test.assertEquals

class ManualParserTest {
private val testCredentials = TestCredentials.INSTANCE
private val siteUrl = testCredentials.parsedSiteUrl
private val authentication = wpAuthenticationFromUsernameAndPassword(
username = testCredentials.adminUsername, password = testCredentials.adminPassword
)
private val requestExecutor by lazy { WpRequestExecutor() }

@Test
fun testUserListManualRequestAndParsing() = runTest {
val requestBuilder = UniffiWpApiRequestBuilder(siteUrl, authentication)
val userListRequest = requestBuilder.users().listWithEditContext(UserListParams())
val userListResponse = requestExecutor.execute(userListRequest)
val userList = parseAsUsersRequestListWithEditContextResponse(userListResponse).data
assertEquals(NUMBER_OF_USERS, userList.count())
}
}
15 changes: 11 additions & 4 deletions wp_derive_request_builder/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn generate_async_request_executor(
parsed_enum: &ParsedEnum,
crate_config: &CrateConfig,
) -> TokenStream {
let crate_ident = &config.crate_ident;
let static_api_base_url_type = &config.static_types.api_base_url;
let static_wp_authentication_type = &config.static_types.wp_authentication;
let static_request_executor_type = &crate_config.request_executor;
Expand Down Expand Up @@ -94,6 +95,7 @@ fn generate_async_request_executor(
&variant.variant_ident,
&context_and_filter_handler,
);
let fn_parse_as_response_type_ident = ident_fn_parse_as_response_type(&response_type_ident);
let response_params_type = response_params_type(variant.attr.params.as_ref(), variant.attr.request_type);
let response_pagination_params_fields = response_params_type.as_ref().map(|p| {
quote! {
Expand Down Expand Up @@ -126,10 +128,10 @@ fn generate_async_request_executor(
pub struct #response_type_ident {
pub data: #output_type,
#[serde(skip)]
pub header_map: std::sync::Arc<crate::request::WpNetworkHeaderMap>,
pub header_map: std::sync::Arc<#crate_ident::request::WpNetworkHeaderMap>,
#response_pagination_params_fields
}
impl From<#response_type_ident> for crate::request::ParsedResponse<#output_type, #parsed_response_params_type> {
impl From<#response_type_ident> for #crate_ident::request::ParsedResponse<#output_type, #parsed_response_params_type> {
fn from(value: #response_type_ident) -> Self {
Self {
data: value.data,
Expand All @@ -138,15 +140,20 @@ fn generate_async_request_executor(
}
}
}
impl From<crate::request::ParsedResponse<#output_type, #parsed_response_params_type>> for #response_type_ident {
fn from(value: crate::request::ParsedResponse<#output_type, #parsed_response_params_type>) -> Self {
impl From<#crate_ident::request::ParsedResponse<#output_type, #parsed_response_params_type>> for #response_type_ident {
fn from(value: #crate_ident::request::ParsedResponse<#output_type, #parsed_response_params_type>) -> Self {
Self {
data: value.data,
header_map: value.header_map,
#from_parsed_response_impl_for_pagination_params
}
}
}

#[uniffi::export]
fn #fn_parse_as_response_type_ident(response: #crate_ident::request::WpNetworkResponse) -> Result<#response_type_ident, #error_type> {
response.parse()
}
}
})
.collect::<TokenStream>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ pub fn ident_response_type(
)
}

pub fn ident_fn_parse_as_response_type(response_type_ident: &Ident) -> Ident {
format_ident!(
"parse_as_{}",
response_type_ident.to_string().to_case(Case::Snake)
)
}

pub fn response_params_type(
params_type: Option<&ParamsType>,
request_type: RequestType,
Expand Down