twapi_v2/api/
post_2_users_id_muting.rs1use crate::{
2 api::{Authentication, TwapiOptions, apply_options, execute_twitter, make_url},
3 error::Error,
4 headers::Headers,
5};
6use reqwest::RequestBuilder;
7use serde::{Deserialize, Serialize};
8
9const URL: &str = "/2/users/:id/muting";
10
11#[derive(Serialize, Deserialize, Debug, Default, Clone)]
12pub struct Body {
13 pub target_user_id: String,
14}
15
16#[derive(Debug, Clone, Default)]
17pub struct Api {
18 id: String,
19 body: Body,
20 twapi_options: Option<TwapiOptions>,
21}
22
23impl Api {
24 pub fn new(id: &str, body: Body) -> Self {
25 Self {
26 id: id.to_owned(),
27 body,
28 ..Default::default()
29 }
30 }
31
32 pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
33 self.twapi_options = Some(value);
34 self
35 }
36
37 pub fn build(self, authentication: &impl Authentication) -> RequestBuilder {
38 let client = reqwest::Client::new();
39 let url = make_url(&self.twapi_options, &URL.replace(":id", &self.id));
40 let builder = client.post(&url).json(&self.body);
41 authentication.execute(
42 apply_options(builder, &self.twapi_options),
43 "POST",
44 &url,
45 &[],
46 )
47 }
48
49 pub async fn execute(
50 self,
51 authentication: &impl Authentication,
52 ) -> Result<(Response, Headers), Error> {
53 execute_twitter(self.build(authentication)).await
54 }
55}
56
57#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
58pub struct Response {
59 #[serde(skip_serializing_if = "Option::is_none")]
60 pub data: Option<Data>,
61 #[serde(flatten)]
62 pub extra: std::collections::HashMap<String, serde_json::Value>,
63}
64
65impl Response {
66 pub fn is_empty_extra(&self) -> bool {
67 let res = self.extra.is_empty()
68 && self
69 .data
70 .as_ref()
71 .map(|it| it.is_empty_extra())
72 .unwrap_or(true);
73 if !res {
74 println!("Response {:?}", self.extra);
75 }
76 res
77 }
78}
79
80#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
81pub struct Data {
82 #[serde(skip_serializing_if = "Option::is_none")]
83 pub muting: Option<bool>,
84 #[serde(flatten)]
85 pub extra: std::collections::HashMap<String, serde_json::Value>,
86}
87
88impl Data {
89 pub fn is_empty_extra(&self) -> bool {
90 let res = self.extra.is_empty();
91 if !res {
92 println!("Data {:?}", self.extra);
93 }
94 res
95 }
96}