1use crate::fields::{
2 media_fields::MediaFields, place_fields::PlaceFields, poll_fields::PollFields,
3 tweet_fields::TweetFields, user_fields::UserFields,
4};
5use crate::responses::{errors::Errors, includes::Includes, tweets::Tweets};
6use crate::{
7 api::{apply_options, execute_twitter, make_url, Authentication, TwapiOptions},
8 error::Error,
9 headers::Headers,
10};
11use itertools::Itertools;
12use reqwest::RequestBuilder;
13use serde::{Deserialize, Serialize};
14use std::collections::HashSet;
15
16const URL: &str = "/2/tweets";
17
18#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Clone)]
19pub enum Expansions {
20 #[serde(rename = "article.cover_media")]
21 ArticleCoverMedia,
22 #[serde(rename = "article.media_entities")]
23 ArticleMediaEntities,
24 #[serde(rename = "attachments.media_keys")]
25 AttachmentsMediaKeys,
26 #[serde(rename = "attachments.media_source_tweet")]
27 AttachmentsMediaSourceTweet,
28 #[serde(rename = "attachments.poll_ids")]
29 AttachmentsPollIds,
30 #[serde(rename = "author_id")]
31 AuthorId,
32 #[serde(rename = "edit_history_tweet_ids")]
33 EditHistoryTweetIds,
34 #[serde(rename = "entities.mentions.username")]
35 EntitiesMentionsUsername,
36 #[serde(rename = "geo.place_id")]
37 GeoPlaceId,
38 #[serde(rename = "in_reply_to_user_id")]
39 InReplyToUserId,
40 #[serde(rename = "entities.note.mentions.username")]
41 EntitiesNoteMentionsUsername,
42 #[serde(rename = "referenced_tweets.id")]
43 ReferencedTweetsId,
44 #[serde(rename = "referenced_tweets.id.author_id")]
45 ReferencedTweetsIdAuthorId,
46}
47
48impl Expansions {
49 pub fn all() -> HashSet<Self> {
50 let mut result = HashSet::new();
51 result.insert(Self::ArticleCoverMedia);
52 result.insert(Self::ArticleMediaEntities);
53 result.insert(Self::AttachmentsMediaKeys);
54 result.insert(Self::AttachmentsMediaSourceTweet);
55 result.insert(Self::AttachmentsPollIds);
56 result.insert(Self::AuthorId);
57 result.insert(Self::EditHistoryTweetIds);
58 result.insert(Self::EntitiesMentionsUsername);
59 result.insert(Self::GeoPlaceId);
60 result.insert(Self::InReplyToUserId);
61 result.insert(Self::EntitiesNoteMentionsUsername);
62 result.insert(Self::ReferencedTweetsId);
63 result.insert(Self::ReferencedTweetsIdAuthorId);
64 result
65 }
66}
67
68impl std::fmt::Display for Expansions {
69 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
70 match self {
71 Self::ArticleCoverMedia => write!(f, "article.cover_media"),
72 Self::ArticleMediaEntities => write!(f, "article.media_entities"),
73 Self::AttachmentsMediaKeys => write!(f, "attachments.media_keys"),
74 Self::AttachmentsMediaSourceTweet => write!(f, "attachments.media_source_tweet"),
75 Self::AttachmentsPollIds => write!(f, "attachments.poll_ids"),
76 Self::AuthorId => write!(f, "author_id"),
77 Self::EditHistoryTweetIds => write!(f, "edit_history_tweet_ids"),
78 Self::EntitiesMentionsUsername => write!(f, "entities.mentions.username"),
79 Self::GeoPlaceId => write!(f, "geo.place_id"),
80 Self::InReplyToUserId => write!(f, "in_reply_to_user_id"),
81 Self::EntitiesNoteMentionsUsername => write!(f, "entities.note.mentions.username"),
82 Self::ReferencedTweetsId => write!(f, "referenced_tweets.id"),
83 Self::ReferencedTweetsIdAuthorId => write!(f, "referenced_tweets.id.author_id"),
84 }
85 }
86}
87
88impl Default for Expansions {
89 fn default() -> Self {
90 Self::ArticleCoverMedia
91 }
92}
93
94#[derive(Debug, Clone, Default)]
95pub struct Api {
96 ids: String,
97 expansions: Option<HashSet<Expansions>>,
98 media_fields: Option<HashSet<MediaFields>>,
99 place_fields: Option<HashSet<PlaceFields>>,
100 poll_fields: Option<HashSet<PollFields>>,
101 tweet_fields: Option<HashSet<TweetFields>>,
102 user_fields: Option<HashSet<UserFields>>,
103 twapi_options: Option<TwapiOptions>,
104}
105
106impl Api {
107 pub fn new(ids: &str) -> Self {
108 Self {
109 ids: ids.to_owned(),
110 ..Default::default()
111 }
112 }
113
114 pub fn all(ids: &str) -> Self {
115 Self {
116 ids: ids.to_owned(),
117 expansions: Some(Expansions::all()),
118 media_fields: Some(MediaFields::all()),
119 place_fields: Some(PlaceFields::all()),
120 poll_fields: Some(PollFields::all()),
121 tweet_fields: Some(TweetFields::organic()),
122 user_fields: Some(UserFields::all()),
123 ..Default::default()
124 }
125 }
126
127 pub fn open(ids: &str) -> Self {
128 Self {
129 ids: ids.to_owned(),
130 expansions: Some(Expansions::all()),
131 media_fields: Some(MediaFields::open()),
132 place_fields: Some(PlaceFields::all()),
133 poll_fields: Some(PollFields::all()),
134 tweet_fields: Some(TweetFields::open()),
135 user_fields: Some(UserFields::all()),
136 ..Default::default()
137 }
138 }
139
140 pub fn expansions(mut self, value: HashSet<Expansions>) -> Self {
141 self.expansions = Some(value);
142 self
143 }
144
145 pub fn media_fields(mut self, value: HashSet<MediaFields>) -> Self {
146 self.media_fields = Some(value);
147 self
148 }
149
150 pub fn place_fields(mut self, value: HashSet<PlaceFields>) -> Self {
151 self.place_fields = Some(value);
152 self
153 }
154
155 pub fn poll_fields(mut self, value: HashSet<PollFields>) -> Self {
156 self.poll_fields = Some(value);
157 self
158 }
159
160 pub fn tweet_fields(mut self, value: HashSet<TweetFields>) -> Self {
161 self.tweet_fields = Some(value);
162 self
163 }
164
165 pub fn user_fields(mut self, value: HashSet<UserFields>) -> Self {
166 self.user_fields = Some(value);
167 self
168 }
169
170 pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
171 self.twapi_options = Some(value);
172 self
173 }
174
175 pub fn build(self, authentication: &impl Authentication) -> RequestBuilder {
176 let mut query_parameters = vec![];
177 query_parameters.push(("ids", self.ids));
178 if let Some(expansions) = self.expansions {
179 query_parameters.push(("expansions", expansions.iter().join(",")));
180 }
181 if let Some(media_fields) = self.media_fields {
182 query_parameters.push(("media.fields", media_fields.iter().join(",")));
183 }
184 if let Some(place_fields) = self.place_fields {
185 query_parameters.push(("place.fields", place_fields.iter().join(",")));
186 }
187 if let Some(poll_fields) = self.poll_fields {
188 query_parameters.push(("poll.fields", poll_fields.iter().join(",")));
189 }
190 if let Some(tweet_fields) = self.tweet_fields {
191 query_parameters.push(("tweet.fields", tweet_fields.iter().join(",")));
192 }
193 if let Some(user_fields) = self.user_fields {
194 query_parameters.push(("user.fields", user_fields.iter().join(",")));
195 }
196 let client = reqwest::Client::new();
197 let url = make_url(&self.twapi_options, URL);
198 let builder = client.get(&url).query(&query_parameters);
199 authentication.execute(
200 apply_options(builder, &self.twapi_options),
201 "GET",
202 &url,
203 &query_parameters
204 .iter()
205 .map(|it| (it.0, it.1.as_str()))
206 .collect::<Vec<_>>(),
207 )
208 }
209
210 pub async fn execute(
211 self,
212 authentication: &impl Authentication,
213 ) -> Result<(Response, Headers), Error> {
214 execute_twitter(self.build(authentication)).await
215 }
216}
217
218#[derive(Serialize, Deserialize, Debug, Clone, Default)]
219pub struct Response {
220 #[serde(skip_serializing_if = "Option::is_none")]
221 pub data: Option<Vec<Tweets>>,
222 #[serde(skip_serializing_if = "Option::is_none")]
223 pub errors: Option<Vec<Errors>>,
224 #[serde(skip_serializing_if = "Option::is_none")]
225 pub includes: Option<Includes>,
226 #[serde(flatten)]
227 pub extra: std::collections::HashMap<String, serde_json::Value>,
228}
229
230impl Response {
231 pub fn is_empty_extra(&self) -> bool {
232 let res = self.extra.is_empty()
233 && self
234 .data
235 .as_ref()
236 .map(|it| it.iter().all(|item| item.is_empty_extra()))
237 .unwrap_or(true)
238 && self
239 .errors
240 .as_ref()
241 .map(|it| it.iter().all(|item| item.is_empty_extra()))
242 .unwrap_or(true)
243 && self
244 .includes
245 .as_ref()
246 .map(|it| it.is_empty_extra())
247 .unwrap_or(true);
248 if !res {
249 println!("Response {:?}", self.extra);
250 }
251 res
252 }
253}