Skip to main content

twapi_v2/api/
get_2_users_id_muting.rs

1use crate::fields::{tweet_fields::TweetFields, user_fields::UserFields};
2use crate::responses::{errors::Errors, includes::Includes, meta::Meta, users::Users};
3use crate::{
4    api::{Authentication, TwapiOptions, apply_options, execute_twitter, make_url},
5    error::Error,
6    headers::Headers,
7};
8use itertools::Itertools;
9use reqwest::RequestBuilder;
10use serde::{Deserialize, Serialize};
11use std::collections::HashSet;
12
13const URL: &str = "/2/users/:id/muting";
14
15#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Clone, Default)]
16pub enum Expansions {
17    #[serde(rename = "affiliation.user_id")]
18    #[default]
19    AffiliationUserId,
20    #[serde(rename = "most_recent_tweet_id")]
21    MostRecentTweetId,
22    #[serde(rename = "pinned_tweet_id")]
23    PinnedTweetId,
24}
25
26impl Expansions {
27    pub fn all() -> HashSet<Self> {
28        let mut result = HashSet::new();
29        result.insert(Self::AffiliationUserId);
30        result.insert(Self::MostRecentTweetId);
31        result.insert(Self::PinnedTweetId);
32        result
33    }
34}
35
36impl std::fmt::Display for Expansions {
37    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
38        match self {
39            Self::AffiliationUserId => write!(f, "affiliation.user_id"),
40            Self::MostRecentTweetId => write!(f, "most_recent_tweet_id"),
41            Self::PinnedTweetId => write!(f, "pinned_tweet_id"),
42        }
43    }
44}
45
46#[derive(Debug, Clone, Default)]
47pub struct Api {
48    id: String,
49    expansions: Option<HashSet<Expansions>>,
50    max_results: Option<usize>,
51    pagination_token: Option<String>,
52    tweet_fields: Option<HashSet<TweetFields>>,
53    user_fields: Option<HashSet<UserFields>>,
54    twapi_options: Option<TwapiOptions>,
55}
56
57impl Api {
58    pub fn new(id: &str) -> Self {
59        Self {
60            id: id.to_owned(),
61            ..Default::default()
62        }
63    }
64
65    pub fn all(id: &str) -> Self {
66        Self {
67            id: id.to_owned(),
68            expansions: Some(Expansions::all()),
69            tweet_fields: Some(TweetFields::organic()),
70            user_fields: Some(UserFields::all()),
71            max_results: Some(1000),
72            ..Default::default()
73        }
74    }
75
76    pub fn open(id: &str) -> Self {
77        Self {
78            id: id.to_owned(),
79            expansions: Some(Expansions::all()),
80            tweet_fields: Some(TweetFields::open()),
81            user_fields: Some(UserFields::all()),
82            max_results: Some(1000),
83            ..Default::default()
84        }
85    }
86
87    pub fn expansions(mut self, value: HashSet<Expansions>) -> Self {
88        self.expansions = Some(value);
89        self
90    }
91
92    pub fn max_results(mut self, value: usize) -> Self {
93        self.max_results = Some(value);
94        self
95    }
96
97    pub fn pagination_token(mut self, value: &str) -> Self {
98        self.pagination_token = Some(value.to_owned());
99        self
100    }
101
102    pub fn tweet_fields(mut self, value: HashSet<TweetFields>) -> Self {
103        self.tweet_fields = Some(value);
104        self
105    }
106
107    pub fn user_fields(mut self, value: HashSet<UserFields>) -> Self {
108        self.user_fields = Some(value);
109        self
110    }
111
112    pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
113        self.twapi_options = Some(value);
114        self
115    }
116
117    pub fn build(self, authentication: &impl Authentication) -> RequestBuilder {
118        let mut query_parameters = vec![];
119        if let Some(expansions) = self.expansions {
120            query_parameters.push(("expansions", expansions.iter().join(",")));
121        }
122        if let Some(max_results) = self.max_results {
123            query_parameters.push(("max_results", max_results.to_string()));
124        }
125        if let Some(pagination_token) = self.pagination_token {
126            query_parameters.push(("pagination_token", pagination_token));
127        }
128        if let Some(tweet_fields) = self.tweet_fields {
129            query_parameters.push(("tweet.fields", tweet_fields.iter().join(",")));
130        }
131        if let Some(user_fields) = self.user_fields {
132            query_parameters.push(("user.fields", user_fields.iter().join(",")));
133        }
134        let client = reqwest::Client::new();
135        let url = make_url(&self.twapi_options, &URL.replace(":id", &self.id));
136        let builder = client.get(&url).query(&query_parameters);
137        authentication.execute(
138            apply_options(builder, &self.twapi_options),
139            "GET",
140            &url,
141            &query_parameters
142                .iter()
143                .map(|it| (it.0, it.1.as_str()))
144                .collect::<Vec<_>>(),
145        )
146    }
147
148    pub async fn execute(
149        self,
150        authentication: &impl Authentication,
151    ) -> Result<(Response, Headers), Error> {
152        execute_twitter(self.build(authentication)).await
153    }
154}
155
156#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
157pub struct Response {
158    #[serde(skip_serializing_if = "Option::is_none")]
159    pub data: Option<Vec<Users>>,
160    #[serde(skip_serializing_if = "Option::is_none")]
161    pub errors: Option<Vec<Errors>>,
162    #[serde(skip_serializing_if = "Option::is_none")]
163    pub includes: Option<Includes>,
164    #[serde(skip_serializing_if = "Option::is_none")]
165    pub meta: Option<Meta>,
166    #[serde(flatten)]
167    pub extra: std::collections::HashMap<String, serde_json::Value>,
168}
169
170impl Response {
171    pub fn is_empty_extra(&self) -> bool {
172        let res = self.extra.is_empty()
173            && self
174                .data
175                .as_ref()
176                .map(|it| it.iter().all(|item| item.is_empty_extra()))
177                .unwrap_or(true)
178            && self
179                .errors
180                .as_ref()
181                .map(|it| it.iter().all(|item| item.is_empty_extra()))
182                .unwrap_or(true)
183            && self
184                .includes
185                .as_ref()
186                .map(|it| it.is_empty_extra())
187                .unwrap_or(true)
188            && self
189                .meta
190                .as_ref()
191                .map(|it| it.is_empty_extra())
192                .unwrap_or(true);
193        if !res {
194            println!("Response {:?}", self.extra);
195        }
196        res
197    }
198}