Skip to content

Commit 9731a24

Browse files
authored
Export parse_as_response_type functions (#395)
* Export `parse_as_response_type` functions * Implement testUserListManualRequestAndParsing Kotlin test
1 parent f6fb3eb commit 9731a24

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

native/kotlin/api/kotlin/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ testing {
3939
dependencies {
4040
implementation(project())
4141

42+
implementation(libs.okhttp)
4243
implementation(rootProject.libs.kotlin.test)
4344
implementation(rootProject.libs.kotlinx.coroutines.test)
4445
implementation(libs.kotlinx.serialization)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package rs.wordpress.api.kotlin
2+
3+
import kotlinx.coroutines.test.runTest
4+
import org.junit.jupiter.api.Test
5+
import uniffi.wp_api.UniffiWpApiRequestBuilder
6+
import uniffi.wp_api.UserListParams
7+
import uniffi.wp_api.parseAsUsersRequestListWithEditContextResponse
8+
import uniffi.wp_api.wpAuthenticationFromUsernameAndPassword
9+
import kotlin.test.assertEquals
10+
11+
class ManualParserTest {
12+
private val testCredentials = TestCredentials.INSTANCE
13+
private val siteUrl = testCredentials.parsedSiteUrl
14+
private val authentication = wpAuthenticationFromUsernameAndPassword(
15+
username = testCredentials.adminUsername, password = testCredentials.adminPassword
16+
)
17+
private val requestExecutor by lazy { WpRequestExecutor() }
18+
19+
@Test
20+
fun testUserListManualRequestAndParsing() = runTest {
21+
val requestBuilder = UniffiWpApiRequestBuilder(siteUrl, authentication)
22+
val userListRequest = requestBuilder.users().listWithEditContext(UserListParams())
23+
val userListResponse = requestExecutor.execute(userListRequest)
24+
val userList = parseAsUsersRequestListWithEditContextResponse(userListResponse).data
25+
assertEquals(NUMBER_OF_USERS, userList.count())
26+
}
27+
}

wp_derive_request_builder/src/generate.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ fn generate_async_request_executor(
3333
parsed_enum: &ParsedEnum,
3434
crate_config: &CrateConfig,
3535
) -> TokenStream {
36+
let crate_ident = &config.crate_ident;
3637
let static_api_base_url_type = &config.static_types.api_base_url;
3738
let static_wp_authentication_type = &config.static_types.wp_authentication;
3839
let static_request_executor_type = &crate_config.request_executor;
@@ -94,6 +95,7 @@ fn generate_async_request_executor(
9495
&variant.variant_ident,
9596
&context_and_filter_handler,
9697
);
98+
let fn_parse_as_response_type_ident = ident_fn_parse_as_response_type(&response_type_ident);
9799
let response_params_type = response_params_type(variant.attr.params.as_ref(), variant.attr.request_type);
98100
let response_pagination_params_fields = response_params_type.as_ref().map(|p| {
99101
quote! {
@@ -126,10 +128,10 @@ fn generate_async_request_executor(
126128
pub struct #response_type_ident {
127129
pub data: #output_type,
128130
#[serde(skip)]
129-
pub header_map: std::sync::Arc<crate::request::WpNetworkHeaderMap>,
131+
pub header_map: std::sync::Arc<#crate_ident::request::WpNetworkHeaderMap>,
130132
#response_pagination_params_fields
131133
}
132-
impl From<#response_type_ident> for crate::request::ParsedResponse<#output_type, #parsed_response_params_type> {
134+
impl From<#response_type_ident> for #crate_ident::request::ParsedResponse<#output_type, #parsed_response_params_type> {
133135
fn from(value: #response_type_ident) -> Self {
134136
Self {
135137
data: value.data,
@@ -138,15 +140,20 @@ fn generate_async_request_executor(
138140
}
139141
}
140142
}
141-
impl From<crate::request::ParsedResponse<#output_type, #parsed_response_params_type>> for #response_type_ident {
142-
fn from(value: crate::request::ParsedResponse<#output_type, #parsed_response_params_type>) -> Self {
143+
impl From<#crate_ident::request::ParsedResponse<#output_type, #parsed_response_params_type>> for #response_type_ident {
144+
fn from(value: #crate_ident::request::ParsedResponse<#output_type, #parsed_response_params_type>) -> Self {
143145
Self {
144146
data: value.data,
145147
header_map: value.header_map,
146148
#from_parsed_response_impl_for_pagination_params
147149
}
148150
}
149151
}
152+
153+
#[uniffi::export]
154+
fn #fn_parse_as_response_type_ident(response: #crate_ident::request::WpNetworkResponse) -> Result<#response_type_ident, #error_type> {
155+
response.parse()
156+
}
150157
}
151158
})
152159
.collect::<TokenStream>()

wp_derive_request_builder/src/generate/helpers_to_generate_tokens.rs

+7
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,13 @@ pub fn ident_response_type(
410410
)
411411
}
412412

413+
pub fn ident_fn_parse_as_response_type(response_type_ident: &Ident) -> Ident {
414+
format_ident!(
415+
"parse_as_{}",
416+
response_type_ident.to_string().to_case(Case::Snake)
417+
)
418+
}
419+
413420
pub fn response_params_type(
414421
params_type: Option<&ParamsType>,
415422
request_type: RequestType,

0 commit comments

Comments
 (0)