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