twapi_v2/api/
get_2_users_id.rs1use 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/:id";
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 id: 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(id: &str) -> Self {
62 Self {
63 id: id.to_owned(),
64 ..Default::default()
65 }
66 }
67
68 pub fn all(id: &str) -> Self {
69 Self {
70 id: id.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(id: &str) -> Self {
79 Self {
80 id: id.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 if let Some(expansions) = self.expansions {
111 query_parameters.push(("expansions", expansions.iter().join(",")));
112 }
113 if let Some(tweet_fields) = self.tweet_fields {
114 query_parameters.push(("tweet.fields", tweet_fields.iter().join(",")));
115 }
116 if let Some(user_fields) = self.user_fields {
117 query_parameters.push(("user.fields", user_fields.iter().join(",")));
118 }
119 let client = reqwest::Client::new();
120 let url = make_url(&self.twapi_options, &URL.replace(":id", &self.id));
121 let builder = client.get(&url).query(&query_parameters);
122 authentication.execute(
123 apply_options(builder, &self.twapi_options),
124 "GET",
125 &url,
126 &query_parameters
127 .iter()
128 .map(|it| (it.0, it.1.as_str()))
129 .collect::<Vec<_>>(),
130 )
131 }
132
133 pub async fn execute(
134 self,
135 authentication: &impl Authentication,
136 ) -> Result<(Response, Headers), Error> {
137 execute_twitter(self.build(authentication)).await
138 }
139}
140
141#[derive(Serialize, Deserialize, Debug, Clone, Default)]
142pub struct Response {
143 #[serde(skip_serializing_if = "Option::is_none")]
144 pub data: Option<Users>,
145 #[serde(skip_serializing_if = "Option::is_none")]
146 pub errors: Option<Vec<Errors>>,
147 #[serde(skip_serializing_if = "Option::is_none")]
148 pub includes: Option<Includes>,
149 #[serde(flatten)]
150 pub extra: std::collections::HashMap<String, serde_json::Value>,
151}
152
153impl Response {
154 pub fn is_empty_extra(&self) -> bool {
155 let res = self.extra.is_empty()
156 && self
157 .data
158 .as_ref()
159 .map(|it| it.is_empty_extra())
160 .unwrap_or(true)
161 && self
162 .errors
163 .as_ref()
164 .map(|it| it.iter().all(|item| item.is_empty_extra()))
165 .unwrap_or(true)
166 && self
167 .includes
168 .as_ref()
169 .map(|it| it.is_empty_extra())
170 .unwrap_or(true);
171 if !res {
172 println!("Response {:?}", self.extra);
173 }
174 res
175 }
176}