Skip to content

Commit 44cfe64

Browse files
authored
Adds delete follower & delete email follower .com endpoints (#789)
1 parent 776b0c9 commit 44cfe64

File tree

10 files changed

+70
-9
lines changed

10 files changed

+70
-9
lines changed

wp_api/src/request.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl InnerRequestBuilder {
8686
}
8787
}
8888

89-
pub fn post<T>(&self, url: ApiEndpointUrl, json_body: &T) -> WpNetworkRequest
89+
pub fn post<T>(&self, url: ApiEndpointUrl, json_body: Option<&T>) -> WpNetworkRequest
9090
where
9191
T: ?Sized + Serialize,
9292
{
@@ -96,9 +96,11 @@ impl InnerRequestBuilder {
9696
method: RequestMethod::POST,
9797
url: url.into(),
9898
header_map: self.header_map_for_post_request().into(),
99-
body: serde_json::to_vec(json_body)
100-
.ok()
101-
.map(|b| Arc::new(WpNetworkRequestBody::new(b))),
99+
body: json_body.and_then(|j| {
100+
serde_json::to_vec(j)
101+
.ok()
102+
.map(|b| Arc::new(WpNetworkRequestBody::new(b)))
103+
}),
102104
}
103105
}
104106

wp_api/src/users.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,12 @@ impl std::fmt::Display for UserId {
422422
}
423423
}
424424

425+
impl From<i64> for UserId {
426+
fn from(value: i64) -> Self {
427+
Self(value)
428+
}
429+
}
430+
425431
#[derive(Debug, Serialize, Deserialize, uniffi::Record, WpContextual)]
426432
pub struct SparseUser {
427433
#[WpContext(edit, embed, view)]

wp_api/src/wp_com/client.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::endpoint::{
2+
followers_endpoint::{FollowersRequestBuilder, FollowersRequestExecutor},
23
jetpack_connection_endpoint::{
34
JetpackConnectionRequestBuilder, JetpackConnectionRequestExecutor,
45
},
@@ -30,6 +31,7 @@ impl UniffiWpComApiRequestBuilder {
3031
}
3132

3233
pub struct WpComApiRequestBuilder {
34+
followers: Arc<FollowersRequestBuilder>,
3335
jetpack_connection: Arc<JetpackConnectionRequestBuilder>,
3436
oauth2: Arc<Oauth2RequestBuilder>,
3537
subscribers: Arc<SubscribersRequestBuilder>,
@@ -43,6 +45,7 @@ impl WpComApiRequestBuilder {
4345
api_client_generate_request_builder!(
4446
api_url_resolver,
4547
auth_provider;
48+
followers,
4649
jetpack_connection,
4750
oauth2,
4851
subscribers,
@@ -67,6 +70,7 @@ impl UniffiWpComApiClient {
6770
}
6871

6972
pub struct WpComApiClient {
73+
followers: Arc<FollowersRequestExecutor>,
7074
jetpack_connection: Arc<JetpackConnectionRequestExecutor>,
7175
oauth2: Arc<Oauth2RequestExecutor>,
7276
subscribers: Arc<SubscribersRequestExecutor>,
@@ -81,13 +85,15 @@ impl WpComApiClient {
8185
api_client_generate_api_client!(
8286
api_url_resolver,
8387
delegate;
88+
followers,
8489
jetpack_connection,
8590
oauth2,
8691
subscribers,
8792
support_bots
8893
)
8994
}
9095
}
96+
api_client_generate_endpoint_impl!(WpComApi, followers);
9197
api_client_generate_endpoint_impl!(WpComApi, jetpack_connection);
9298
api_client_generate_endpoint_impl!(WpComApi, oauth2);
9399
api_client_generate_endpoint_impl!(WpComApi, subscribers);

wp_api/src/wp_com/endpoint.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
use std::sync::Arc;
77
use strum::IntoEnumIterator;
88

9+
pub mod followers_endpoint;
910
pub mod jetpack_connection_endpoint;
1011
pub mod oauth2;
1112
pub mod subscribers_endpoint;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::{
2+
request::endpoint::{AsNamespace, DerivedRequest},
3+
users::UserId,
4+
wp_com::{
5+
WpComNamespace, WpComSiteId, followers::DeleteFollowerResponse, subscribers::SubscriptionId,
6+
},
7+
};
8+
use wp_derive_request_builder::WpDerivedRequest;
9+
10+
#[derive(WpDerivedRequest)]
11+
enum FollowersRequest {
12+
#[post(url = "/sites/<wp_com_site_id>/followers/<user_id>/delete", output = DeleteFollowerResponse)]
13+
DeleteFollower,
14+
#[post(url = "/sites/<wp_com_site_id>/email-followers/<subscription_id>/delete", output = DeleteFollowerResponse)]
15+
DeleteEmailFollower,
16+
}
17+
18+
impl DerivedRequest for FollowersRequest {
19+
fn namespace() -> impl AsNamespace {
20+
WpComNamespace::RestV1_1
21+
}
22+
}

wp_api/src/wp_com/followers.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use crate::users::UserId;
2+
use serde::{Deserialize, Serialize};
3+
use wp_serde_helper::deserialize_i64_or_string_as_t;
4+
5+
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
6+
pub struct DeleteFollowerResponse {
7+
#[serde(deserialize_with = "deserialize_i64_or_string_as_t")]
8+
follower_id: UserId,
9+
deleted: bool,
10+
}

wp_api/src/wp_com/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{num::ParseIntError, str::FromStr, sync::Arc};
55

66
pub mod client;
77
pub mod endpoint;
8+
pub mod followers;
89
pub mod jetpack_connection;
910
pub mod oauth2;
1011
pub mod subscribers;
@@ -31,13 +32,15 @@ impl std::fmt::Display for WpComSiteId {
3132

3233
pub(crate) enum WpComNamespace {
3334
Oauth2,
35+
RestV1_1,
3436
V2,
3537
}
3638

3739
impl AsNamespace for WpComNamespace {
3840
fn namespace_value(&self) -> &'static str {
3941
match self {
4042
WpComNamespace::Oauth2 => "/oauth2",
43+
WpComNamespace::RestV1_1 => "/rest/v1.1",
4144
WpComNamespace::V2 => "/wpcom/v2",
4245
}
4346
}

wp_api/src/wp_com/subscribers.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::{
55
url_query::{AppendUrlQueryPairs, QueryPairs, QueryPairsExtension},
66
users::UserId,
77
};
8-
98
use serde::{Deserialize, Serialize};
109
use std::{collections::HashMap, ops::Not};
1110
use wp_serde_helper::deserialize_u64_or_string;

wp_derive_request_builder/src/generate/helpers_to_generate_tokens.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,11 @@ pub fn fn_body_build_request_from_url(
370370
RequestType::Post => {
371371
if params_type.is_some() {
372372
quote! {
373-
self.inner.post(url, params)
373+
self.inner.post(url, Some(params))
374374
}
375375
} else {
376376
quote! {
377-
self.inner.post(url)
377+
self.inner.post(url, None::<&()>)
378378
}
379379
}
380380
}
@@ -1053,11 +1053,15 @@ mod tests {
10531053
RequestType::Delete,
10541054
"self . inner . delete (url)"
10551055
)]
1056-
#[case(None, RequestType::Post, "self . inner . post (url)")]
1056+
#[case(
1057+
None,
1058+
RequestType::Post,
1059+
"self . inner . post (url , None :: < & () >)"
1060+
)]
10571061
#[case(
10581062
referenced_params_type("UserListParams"),
10591063
RequestType::Post,
1060-
"self . inner . post (url , params)"
1064+
"self . inner . post (url , Some (params))"
10611065
)]
10621066
fn test_fn_body_build_request_from_url(
10631067
#[case] params: Option<ParamsType>,

wp_serde_helper/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ where
9696
deserializer.deserialize_any(DeserializeU64OrStringVisitor)
9797
}
9898

99+
pub fn deserialize_i64_or_string_as_t<'de, D, T>(deserializer: D) -> Result<T, D::Error>
100+
where
101+
D: Deserializer<'de>,
102+
T: From<i64>,
103+
{
104+
deserialize_i64_or_string(deserializer).map(Into::into)
105+
}
106+
99107
struct DeserializeU64OrStringVisitor;
100108

101109
impl de::Visitor<'_> for DeserializeU64OrStringVisitor {

0 commit comments

Comments
 (0)