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