twapi_v2/api/
get_2_users_me.rs1use crate::fields::{tweet_fields::TweetFields, user_fields::UserFields};
2use crate::responses::{errors::Errors, includes::Includes, 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/me";
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 expansions: Option<HashSet<Expansions>>,
49 tweet_fields: Option<HashSet<TweetFields>>,
50 user_fields: Option<HashSet<UserFields>>,
51 twapi_options: Option<TwapiOptions>,
52}
53
54impl Api {
55 pub fn new() -> Self {
56 Self {
57 ..Default::default()
58 }
59 }
60
61 pub fn all() -> Self {
62 Self {
63 expansions: Some(Expansions::all()),
64 tweet_fields: Some(TweetFields::organic()),
65 user_fields: Some(UserFields::all()),
66 ..Default::default()
67 }
68 }
69
70 pub fn open() -> Self {
71 Self {
72 expansions: Some(Expansions::all()),
73 tweet_fields: Some(TweetFields::open()),
74 user_fields: Some(UserFields::all()),
75 ..Default::default()
76 }
77 }
78
79 pub fn expansions(mut self, value: HashSet<Expansions>) -> Self {
80 self.expansions = Some(value);
81 self
82 }
83
84 pub fn tweet_fields(mut self, value: HashSet<TweetFields>) -> Self {
85 self.tweet_fields = Some(value);
86 self
87 }
88
89 pub fn user_fields(mut self, value: HashSet<UserFields>) -> Self {
90 self.user_fields = Some(value);
91 self
92 }
93
94 pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
95 self.twapi_options = Some(value);
96 self
97 }
98
99 pub fn build(self, authentication: &impl Authentication) -> RequestBuilder {
100 let mut query_parameters = vec![];
101 if let Some(expansions) = self.expansions {
102 query_parameters.push(("expansions", expansions.iter().join(",")));
103 }
104 if let Some(tweet_fields) = self.tweet_fields {
105 query_parameters.push(("tweet.fields", tweet_fields.iter().join(",")));
106 }
107 if let Some(user_fields) = self.user_fields {
108 query_parameters.push(("user.fields", user_fields.iter().join(",")));
109 }
110 let client = reqwest::Client::new();
111 let url = make_url(&self.twapi_options, URL);
112 let builder = client.get(&url).query(&query_parameters);
113 authentication.execute(
114 apply_options(builder, &self.twapi_options),
115 "GET",
116 &url,
117 &query_parameters
118 .iter()
119 .map(|it| (it.0, it.1.as_str()))
120 .collect::<Vec<_>>(),
121 )
122 }
123
124 pub async fn execute(
125 self,
126 authentication: &impl Authentication,
127 ) -> Result<(Response, Headers), Error> {
128 execute_twitter(self.build(authentication)).await
129 }
130}
131
132#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
133pub struct Response {
134 #[serde(skip_serializing_if = "Option::is_none")]
135 pub data: Option<Users>,
136 #[serde(skip_serializing_if = "Option::is_none")]
137 pub errors: Option<Vec<Errors>>,
138 #[serde(skip_serializing_if = "Option::is_none")]
139 pub includes: Option<Includes>,
140 #[serde(flatten)]
141 pub extra: std::collections::HashMap<String, serde_json::Value>,
142}
143
144impl Response {
145 pub fn is_empty_extra(&self) -> bool {
146 let res = self.extra.is_empty()
147 && self
148 .data
149 .as_ref()
150 .map(|it| it.is_empty_extra())
151 .unwrap_or(true)
152 && self
153 .errors
154 .as_ref()
155 .map(|it| it.iter().all(|item| item.is_empty_extra()))
156 .unwrap_or(true)
157 && self
158 .includes
159 .as_ref()
160 .map(|it| it.is_empty_extra())
161 .unwrap_or(true);
162 if !res {
163 println!("Response {:?}", self.extra);
164 }
165 res
166 }
167}