-
Notifications
You must be signed in to change notification settings - Fork 3
Introduces RequestBuilder
#114
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9261668
Add User & Plugin request builders and move all requests into them
oguzkocer a9378b1
Shorten names in user & plugin request builder
oguzkocer 1cf9e00
Move users & plugin request builders to request module
oguzkocer 8d977af
Use users & plugins request in WPApiHelper
oguzkocer a7a66d7
Remove ApiEndpoint
oguzkocer 2e4ea26
Introduce RequestBuilder and use it in UsersRequest
oguzkocer 08335bc
Use request builder in plugin request builder
oguzkocer 6a01007
Convert ApiBaseUrl to Arc<ApiBaseUrl>
oguzkocer a3f3944
Use request_builder for raw_request
oguzkocer fe13c62
Add Builder suffix to UsersRequest & PluginsRequest
oguzkocer ba44802
Move header map helpers inside RequestBuilder
oguzkocer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
#![allow(dead_code, unused_variables)] | ||
|
||
use request::{ | ||
endpoint::{ApiEndpoint, ApiEndpointUrl}, | ||
endpoint::{ApiBaseUrl, ApiEndpointUrl}, | ||
plugins_request_builder::PluginsRequestBuilder, | ||
users_request_builder::UsersRequestBuilder, | ||
RequestMethod, WPNetworkRequest, WPNetworkResponse, | ||
}; | ||
use std::collections::HashMap; | ||
use serde::Serialize; | ||
use std::{collections::HashMap, sync::Arc}; | ||
use url::Url; | ||
|
||
pub use api_error::{WPApiError, WPRestError, WPRestErrorCode, WPRestErrorWrapper}; | ||
|
@@ -25,8 +28,9 @@ const CONTENT_TYPE_JSON: &str = "application/json"; | |
|
||
#[derive(Debug, uniffi::Object)] | ||
pub struct WPApiHelper { | ||
api_endpoint: ApiEndpoint, | ||
authentication: WPAuthentication, | ||
request_builder: Arc<RequestBuilder>, | ||
users_request: UsersRequestBuilder, | ||
plugins_request: PluginsRequestBuilder, | ||
} | ||
|
||
#[uniffi::export] | ||
|
@@ -43,39 +47,33 @@ impl WPApiHelper { | |
pub fn new(site_url: String, authentication: WPAuthentication) -> Self { | ||
let url = Url::parse(site_url.as_str()).unwrap(); | ||
// TODO: Handle the url parse error | ||
let api_endpoint = ApiEndpoint::new_from_str(site_url.as_str()).unwrap(); | ||
let api_base_url = Arc::new(ApiBaseUrl::new(site_url.as_str()).unwrap()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about using |
||
let request_builder = Arc::new(RequestBuilder { | ||
authentication: authentication.clone(), | ||
}); | ||
|
||
Self { | ||
api_endpoint, | ||
authentication, | ||
request_builder: request_builder.clone(), | ||
users_request: UsersRequestBuilder::new(api_base_url.clone(), request_builder.clone()), | ||
plugins_request: PluginsRequestBuilder::new( | ||
api_base_url.clone(), | ||
request_builder.clone(), | ||
), | ||
} | ||
} | ||
|
||
// TODO: Remove this because we want to build all requests within the crate | ||
pub fn raw_request(&self, url: String) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::GET, | ||
url: ApiEndpointUrl::new(Url::parse(url.as_str()).unwrap()).into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
self.request_builder | ||
.get(ApiEndpointUrl::new(Url::parse(url.as_str()).unwrap())) | ||
} | ||
|
||
pub fn list_users_request( | ||
&self, | ||
context: WPContext, | ||
params: &Option<UserListParams>, // UniFFI doesn't support Option<&T> | ||
) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::GET, | ||
url: self | ||
.api_endpoint | ||
.users | ||
.list(context, params.as_ref()) | ||
.into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
self.users_request.list(context, params) | ||
} | ||
|
||
pub fn filter_list_users_request( | ||
|
@@ -84,25 +82,11 @@ impl WPApiHelper { | |
params: &Option<UserListParams>, // UniFFI doesn't support Option<&T> | ||
fields: &[SparseUserField], | ||
) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::GET, | ||
url: self | ||
.api_endpoint | ||
.users | ||
.filter_list(context, params.as_ref(), fields) | ||
.into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
self.users_request.filter_list(context, params, fields) | ||
} | ||
|
||
pub fn retrieve_user_request(&self, user_id: UserId, context: WPContext) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::GET, | ||
url: self.api_endpoint.users.retrieve(user_id, context).into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
self.users_request.retrieve(user_id, context) | ||
} | ||
|
||
pub fn filter_retrieve_user_request( | ||
|
@@ -111,112 +95,55 @@ impl WPApiHelper { | |
context: WPContext, | ||
fields: &[SparseUserField], | ||
) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::GET, | ||
url: self | ||
.api_endpoint | ||
.users | ||
.filter_retrieve(user_id, context, fields) | ||
.into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
self.users_request.filter_retrieve(user_id, context, fields) | ||
} | ||
|
||
pub fn retrieve_current_user_request(&self, context: WPContext) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::GET, | ||
url: self.api_endpoint.users.retrieve_me(context).into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
self.users_request.retrieve_me(context) | ||
} | ||
|
||
pub fn filter_retrieve_current_user_request( | ||
&self, | ||
context: WPContext, | ||
fields: &[SparseUserField], | ||
) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::GET, | ||
url: self | ||
.api_endpoint | ||
.users | ||
.filter_retrieve_me(context, fields) | ||
.into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
self.users_request.filter_retrieve_me(context, fields) | ||
} | ||
|
||
pub fn create_user_request(&self, params: &UserCreateParams) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::POST, | ||
url: self.api_endpoint.users.create().into(), | ||
header_map: self.header_map_for_post_request(), | ||
body: serde_json::to_vec(¶ms).ok(), | ||
} | ||
self.users_request.create(params) | ||
} | ||
|
||
pub fn update_user_request( | ||
&self, | ||
user_id: UserId, | ||
params: &UserUpdateParams, | ||
) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::POST, | ||
url: self.api_endpoint.users.update(user_id).into(), | ||
header_map: self.header_map_for_post_request(), | ||
body: serde_json::to_vec(¶ms).ok(), | ||
} | ||
self.users_request.update(user_id, params) | ||
} | ||
|
||
pub fn update_current_user_request(&self, params: &UserUpdateParams) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::POST, | ||
url: self.api_endpoint.users.update_me().into(), | ||
header_map: self.header_map_for_post_request(), | ||
body: serde_json::to_vec(¶ms).ok(), | ||
} | ||
self.users_request.update_me(params) | ||
} | ||
|
||
pub fn delete_user_request( | ||
&self, | ||
user_id: UserId, | ||
params: &UserDeleteParams, | ||
) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::DELETE, | ||
url: self.api_endpoint.users.delete(user_id, params).into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
self.users_request.delete(user_id, params) | ||
} | ||
|
||
pub fn delete_current_user_request(&self, params: &UserDeleteParams) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::DELETE, | ||
url: self.api_endpoint.users.delete_me(params).into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
self.users_request.delete_me(params) | ||
} | ||
|
||
pub fn list_plugins_request( | ||
&self, | ||
context: WPContext, | ||
params: &Option<PluginListParams>, // UniFFI doesn't support Option<&T> | ||
) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::GET, | ||
url: self | ||
.api_endpoint | ||
.plugins | ||
.list(context, params.as_ref()) | ||
.into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
self.plugins_request.list(context, params) | ||
} | ||
|
||
pub fn filter_list_plugins_request( | ||
|
@@ -225,38 +152,19 @@ impl WPApiHelper { | |
params: &Option<PluginListParams>, // UniFFI doesn't support Option<&T> | ||
fields: &[SparsePluginField], | ||
) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::GET, | ||
url: self | ||
.api_endpoint | ||
.plugins | ||
.filter_list(context, params.as_ref(), fields) | ||
.into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
self.plugins_request.filter_list(context, params, fields) | ||
} | ||
|
||
pub fn create_plugin_request(&self, params: &PluginCreateParams) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::POST, | ||
url: self.api_endpoint.plugins.create().into(), | ||
header_map: self.header_map_for_post_request(), | ||
body: serde_json::to_vec(¶ms).ok(), | ||
} | ||
self.plugins_request.create(params) | ||
} | ||
|
||
pub fn retrieve_plugin_request( | ||
&self, | ||
context: WPContext, | ||
plugin: &PluginSlug, | ||
) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::GET, | ||
url: self.api_endpoint.plugins.retrieve(context, plugin).into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
self.plugins_request.retrieve(context, plugin) | ||
} | ||
|
||
pub fn filter_retrieve_plugin_request( | ||
|
@@ -265,35 +173,54 @@ impl WPApiHelper { | |
plugin: &PluginSlug, | ||
fields: &[SparsePluginField], | ||
) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::GET, | ||
url: self | ||
.api_endpoint | ||
.plugins | ||
.filter_retrieve(context, plugin, fields) | ||
.into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
self.plugins_request | ||
.filter_retrieve(context, plugin, fields) | ||
} | ||
|
||
pub fn update_plugin_request( | ||
&self, | ||
plugin: &PluginSlug, | ||
params: &PluginUpdateParams, | ||
) -> WPNetworkRequest { | ||
self.plugins_request.update(plugin, params) | ||
} | ||
|
||
pub fn delete_plugin_request(&self, plugin: &PluginSlug) -> WPNetworkRequest { | ||
self.plugins_request.delete(plugin) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct RequestBuilder { | ||
authentication: WPAuthentication, | ||
} | ||
|
||
impl RequestBuilder { | ||
fn get(&self, url: ApiEndpointUrl) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::GET, | ||
url: url.into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
} | ||
|
||
fn post<T>(&self, url: ApiEndpointUrl, json_body: &T) -> WPNetworkRequest | ||
where | ||
T: ?Sized + Serialize, | ||
{ | ||
WPNetworkRequest { | ||
method: RequestMethod::POST, | ||
url: self.api_endpoint.plugins.update(plugin).into(), | ||
url: url.into(), | ||
header_map: self.header_map_for_post_request(), | ||
body: serde_json::to_vec(¶ms).ok(), | ||
body: serde_json::to_vec(json_body).ok(), | ||
} | ||
} | ||
|
||
pub fn delete_plugin_request(&self, plugin: &PluginSlug) -> WPNetworkRequest { | ||
fn delete(&self, url: ApiEndpointUrl) -> WPNetworkRequest { | ||
WPNetworkRequest { | ||
method: RequestMethod::DELETE, | ||
url: self.api_endpoint.plugins.delete(plugin).into(), | ||
url: url.into(), | ||
header_map: self.header_map(), | ||
body: None, | ||
} | ||
|
@@ -305,9 +232,9 @@ impl WPApiHelper { | |
http::header::ACCEPT.to_string(), | ||
CONTENT_TYPE_JSON.to_string(), | ||
); | ||
match &self.authentication { | ||
match self.authentication { | ||
WPAuthentication::None => None, | ||
WPAuthentication::AuthorizationHeader { token } => { | ||
WPAuthentication::AuthorizationHeader { ref token } => { | ||
header_map.insert("Authorization".to_string(), format!("Basic {}", token)) | ||
} | ||
}; | ||
|
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
Oops, something went wrong.
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.
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.
users_request
is a bit of an awkward name, but I'm assume it's to make things nice for a caller like:If not, we might want to consider just calling it
users_request_builder
, as lengthy as that is? WDYT?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 forgot to add the
_builder
suffix after fe13c62. I'll do that in the next iteration ofWPApiHelper
.