twapi_v2/api/
get_2_users_by.rs

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