Skip to content
1 change: 0 additions & 1 deletion fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ lane :release do |options|
build_xcframework
update_swift_package
publish_github_release
# TODO: publish android library
end

lane :validate do
Expand Down
44 changes: 13 additions & 31 deletions wp_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

use request::{
endpoint::{
application_passwords_endpoint::{
ApplicationPasswordsRequestBuilder2, ApplicationPasswordsRequestExecutor,
},
plugins_endpoint::{PluginsRequestBuilder2, PluginsRequestExecutor},
users_endpoint::{UsersRequestBuilder2, UsersRequestExecutor},
application_passwords_endpoint::ApplicationPasswordsRequestExecutor,
plugins_endpoint::PluginsRequestExecutor,
users_endpoint::{UsersRequestBuilder, UsersRequestExecutor},
ApiBaseUrl,
},
RequestExecutor, WpNetworkResponse,
Expand All @@ -30,39 +28,27 @@ pub mod users;
#[cfg(test)]
mod unit_test_common;

// TODO: This is a temporary type that allows building a request type
// Although we'll have a type that does that, it's unlikely that it'll look like this.
// It still does its job for now to prove that `UsersRequestBuilder2` (temporary) type is
// properly generated and utilized in `test_manual_request_builder_immut` integration tests
#[derive(Debug, uniffi::Object)]
pub struct WpApiRequestBuilder {
users: Arc<UsersRequestBuilder2>,
users: Arc<UsersRequestBuilder>,
}

#[uniffi::export]
impl WpApiRequestBuilder {
#[uniffi::constructor]
pub fn new(
site_url: String,
authentication: WpAuthentication,
request_executor: Arc<dyn RequestExecutor>,
) -> Result<Self, WpApiError> {
pub fn new(site_url: String, authentication: WpAuthentication) -> Result<Self, WpApiError> {
let api_base_url: Arc<ApiBaseUrl> = ApiBaseUrl::try_from(site_url.as_str())
.map_err(|err| WpApiError::SiteUrlParsingError {
reason: err.to_string(),
})?
.into();
let request_builder = Arc::new(request::RequestBuilder::new(
request_executor,
authentication.clone(),
));

Ok(Self {
users: UsersRequestBuilder2::new(api_base_url.clone(), request_builder.clone()).into(),
users: UsersRequestBuilder::new(api_base_url.clone(), authentication).into(),
})
}

pub fn users(&self) -> Arc<UsersRequestBuilder2> {
pub fn users(&self) -> Arc<UsersRequestBuilder> {
self.users.clone()
}
}
Expand All @@ -87,27 +73,23 @@ impl WpRequestBuilder {
reason: err.to_string(),
})?
.into();
let request_builder = Arc::new(request::RequestBuilder::new(
request_executor.clone(),
authentication.clone(),
));

Ok(Self {
application_passwords: ApplicationPasswordsRequestExecutor::new(
ApplicationPasswordsRequestBuilder2::new(
api_base_url.clone(),
request_builder.clone(),
),
api_base_url.clone(),
authentication.clone(),
request_executor.clone(),
)
.into(),
users: UsersRequestExecutor::new(
UsersRequestBuilder2::new(api_base_url.clone(), request_builder.clone()),
api_base_url.clone(),
authentication.clone(),
request_executor.clone(),
)
.into(),
plugins: PluginsRequestExecutor::new(
PluginsRequestBuilder2::new(api_base_url.clone(), request_builder.clone()),
api_base_url.clone(),
authentication.clone(),
request_executor.clone(),
)
.into(),
Expand Down
50 changes: 9 additions & 41 deletions wp_api/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::HashMap, fmt::Debug, sync::Arc};
use std::{collections::HashMap, fmt::Debug};

use endpoint::ApiEndpointUrl;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use url::Url;

use crate::{api_error::RequestExecutionError, WpApiError, WpAuthentication};
Expand All @@ -14,23 +14,16 @@ const CONTENT_TYPE_JSON: &str = "application/json";
const LINK_HEADER_KEY: &str = "Link";

#[derive(Debug)]
pub(crate) struct RequestBuilder {
executor: Arc<dyn RequestExecutor>,
struct InnerRequestBuilder {
authentication: WpAuthentication,
}

impl RequestBuilder {
pub(crate) fn new(
executor: Arc<dyn RequestExecutor>,
authentication: WpAuthentication,
) -> Self {
Self {
executor,
authentication,
}
impl InnerRequestBuilder {
fn new(authentication: WpAuthentication) -> Self {
Self { authentication }
}

fn build_get_request(&self, url: ApiEndpointUrl) -> WpNetworkRequest {
fn get(&self, url: ApiEndpointUrl) -> WpNetworkRequest {
WpNetworkRequest {
method: RequestMethod::GET,
url: url.into(),
Expand All @@ -39,14 +32,7 @@ impl RequestBuilder {
}
}

async fn get<T: DeserializeOwned>(&self, url: ApiEndpointUrl) -> Result<T, WpApiError> {
self.executor
.execute(self.build_get_request(url))
.await?
.parse()
}

fn build_post_request<T>(&self, url: ApiEndpointUrl, json_body: &T) -> WpNetworkRequest
fn post<T>(&self, url: ApiEndpointUrl, json_body: &T) -> WpNetworkRequest
where
T: ?Sized + Serialize,
{
Expand All @@ -58,18 +44,7 @@ impl RequestBuilder {
}
}

async fn post<T, R>(&self, url: ApiEndpointUrl, json_body: &T) -> Result<R, WpApiError>
where
T: ?Sized + Serialize,
R: DeserializeOwned,
{
self.executor
.execute(self.build_post_request(url, json_body))
.await?
.parse()
}

fn build_delete_request(&self, url: ApiEndpointUrl) -> WpNetworkRequest {
fn delete(&self, url: ApiEndpointUrl) -> WpNetworkRequest {
WpNetworkRequest {
method: RequestMethod::DELETE,
url: url.into(),
Expand All @@ -78,13 +53,6 @@ impl RequestBuilder {
}
}

async fn delete<T: DeserializeOwned>(&self, url: ApiEndpointUrl) -> Result<T, WpApiError> {
self.executor
.execute(self.build_delete_request(url))
.await?
.parse()
}

fn header_map(&self) -> HashMap<String, String> {
let mut header_map = HashMap::new();
header_map.insert(
Expand Down
14 changes: 4 additions & 10 deletions wp_api/tests/test_manual_request_builder_immut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use integration_test_common::{
use reusable_test_cases::list_users_cases;
use rstest::*;
use rstest_reuse::{self, apply};
use std::sync::Arc;
use wp_api::{
generate,
users::UserWithEditContext,
Expand All @@ -26,16 +25,11 @@ async fn list_users_with_edit_context(#[case] params: UserListParams) {
TEST_CREDENTIALS_ADMIN_USERNAME.to_string(),
TEST_CREDENTIALS_ADMIN_PASSWORD.to_string(),
);
let async_wp_networking = Arc::new(AsyncWpNetworking::default());
let async_wp_networking = AsyncWpNetworking::default();

let request_builder = WpApiRequestBuilder::new(
TEST_CREDENTIALS_SITE_URL.to_string(),
authentication,
// TODO: A request executor shouldn't be necessary, but we don't have a standalone request
// builder yet
async_wp_networking.clone(),
)
.expect("Site url is generated by our tooling");
let request_builder =
WpApiRequestBuilder::new(TEST_CREDENTIALS_SITE_URL.to_string(), authentication)
.expect("Site url is generated by our tooling");
let wp_request = request_builder.users().list_with_edit_context(&params);
let response = async_wp_networking.async_request(wp_request).await;
let result = response.unwrap().parse::<Vec<UserWithEditContext>>();
Expand Down
Loading