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/search";
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 query: String,
49 expansions: Option<HashSet<Expansions>>,
50 max_results: Option<usize>,
51 next_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(query: &str) -> Self {
59 Self {
60 query: query.to_owned(),
61 ..Default::default()
62 }
63 }
64
65 pub fn all(query: &str) -> Self {
66 Self {
67 query: query.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(query: &str) -> Self {
77 Self {
78 query: query.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 next_token(mut self, value: &str) -> Self {
98 self.next_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 query_parameters.push(("query", self.query));
120 if let Some(expansions) = self.expansions {
121 query_parameters.push(("expansions", expansions.iter().join(",")));
122 }
123 if let Some(max_results) = self.max_results {
124 query_parameters.push(("max_results", max_results.to_string()));
125 }
126 if let Some(next_token) = self.next_token {
127 query_parameters.push(("next_token", next_token));
128 }
129 if let Some(tweet_fields) = self.tweet_fields {
130 query_parameters.push(("tweet.fields", tweet_fields.iter().join(",")));
131 }
132 if let Some(user_fields) = self.user_fields {
133 query_parameters.push(("user.fields", user_fields.iter().join(",")));
134 }
135 let client = reqwest::Client::new();
136 let url = make_url(&self.twapi_options, URL);
137 let builder = client.get(&url).query(&query_parameters);
138 authentication.execute(
139 apply_options(builder, &self.twapi_options),
140 "GET",
141 &url,
142 &query_parameters
143 .iter()
144 .map(|it| (it.0, it.1.as_str()))
145 .collect::<Vec<_>>(),
146 )
147 }
148
149 pub async fn execute(
150 self,
151 authentication: &impl Authentication,
152 ) -> Result<(Response, Headers), Error> {
153 execute_twitter(self.build(authentication)).await
154 }
155}
156
157#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
158pub struct Response {
159 #[serde(skip_serializing_if = "Option::is_none")]
160 pub data: Option<Vec<Users>>,
161 #[serde(skip_serializing_if = "Option::is_none")]
162 pub errors: Option<Vec<Errors>>,
163 #[serde(skip_serializing_if = "Option::is_none")]
164 pub includes: Option<Includes>,
165 #[serde(skip_serializing_if = "Option::is_none")]
166 pub meta: Option<Meta>,
167 #[serde(flatten)]
168 pub extra: std::collections::HashMap<String, serde_json::Value>,
169}
170
171impl Response {
172 pub fn is_empty_extra(&self) -> bool {
173 let res = self.extra.is_empty()
174 && self
175 .data
176 .as_ref()
177 .map(|it| it.iter().all(|item| item.is_empty_extra()))
178 .unwrap_or(true)
179 && self
180 .errors
181 .as_ref()
182 .map(|it| it.iter().all(|item| item.is_empty_extra()))
183 .unwrap_or(true)
184 && self
185 .includes
186 .as_ref()
187 .map(|it| it.is_empty_extra())
188 .unwrap_or(true)
189 && self
190 .meta
191 .as_ref()
192 .map(|it| it.is_empty_extra())
193 .unwrap_or(true);
194 if !res {
195 println!("Response {:?}", self.extra);
196 }
197 res
198 }
199}