Skip to main content

twapi_v2/api/
get_2_spaces_search.rs

1use crate::fields::{
2    space_fields::SpaceFields, topic_fields::TopicFields, user_fields::UserFields,
3};
4use crate::responses::{errors::Errors, includes::Includes, meta::Meta, spaces::Spaces};
5use crate::{
6    api::{Authentication, TwapiOptions, execute_twitter, make_url},
7    error::Error,
8    headers::Headers,
9};
10use itertools::Itertools;
11use reqwest::RequestBuilder;
12use serde::{Deserialize, Serialize};
13use std::collections::HashSet;
14
15const URL: &str = "/2/spaces/search";
16
17#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Clone, Default)]
18pub enum Expansions {
19    #[serde(rename = "invited_user_ids")]
20    #[default]
21    InvitedUserIds,
22    #[serde(rename = "speaker_ids")]
23    SpeakerIds,
24    #[serde(rename = "creator_id")]
25    CreatorId,
26    #[serde(rename = "host_ids")]
27    HostIds,
28    #[serde(rename = "topics_ids")]
29    TopicsIds,
30}
31
32impl Expansions {
33    pub fn all() -> HashSet<Self> {
34        let mut result = HashSet::new();
35        result.insert(Self::InvitedUserIds);
36        result.insert(Self::SpeakerIds);
37        result.insert(Self::CreatorId);
38        result.insert(Self::HostIds);
39        result.insert(Self::TopicsIds);
40        result
41    }
42}
43
44impl std::fmt::Display for Expansions {
45    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
46        match self {
47            Self::InvitedUserIds => write!(f, "invited_user_ids"),
48            Self::SpeakerIds => write!(f, "speaker_ids"),
49            Self::CreatorId => write!(f, "creator_id"),
50            Self::HostIds => write!(f, "host_ids"),
51            Self::TopicsIds => write!(f, "topics_ids"),
52        }
53    }
54}
55
56#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Clone, Default)]
57pub enum State {
58    #[serde(rename = "all")]
59    #[default]
60    All,
61    #[serde(rename = "live")]
62    Live,
63    #[serde(rename = "scheduled")]
64    Scheduled,
65}
66
67impl std::fmt::Display for State {
68    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
69        match self {
70            Self::All => write!(f, "all"),
71            Self::Live => write!(f, "live"),
72            Self::Scheduled => write!(f, "scheduled"),
73        }
74    }
75}
76
77#[derive(Debug, Clone, Default)]
78pub struct Api {
79    query: String,
80    expansions: Option<HashSet<Expansions>>,
81    space_fields: Option<HashSet<SpaceFields>>,
82    state: Option<State>,
83    topic_fields: Option<HashSet<TopicFields>>,
84    user_fields: Option<HashSet<UserFields>>,
85    twapi_options: Option<TwapiOptions>,
86}
87
88impl Api {
89    pub fn new(query: &str) -> Self {
90        Self {
91            query: query.to_owned(),
92            ..Default::default()
93        }
94    }
95
96    pub fn all(query: &str) -> Self {
97        Self {
98            query: query.to_owned(),
99            expansions: Some(Expansions::all()),
100            space_fields: Some(SpaceFields::all()),
101            topic_fields: Some(TopicFields::all()),
102            user_fields: Some(UserFields::all()),
103            ..Default::default()
104        }
105    }
106
107    pub fn expansions(mut self, value: HashSet<Expansions>) -> Self {
108        self.expansions = Some(value);
109        self
110    }
111
112    pub fn space_fields(mut self, value: HashSet<SpaceFields>) -> Self {
113        self.space_fields = Some(value);
114        self
115    }
116
117    pub fn state(mut self, value: State) -> Self {
118        self.state = Some(value);
119        self
120    }
121
122    pub fn topic_fields(mut self, value: HashSet<TopicFields>) -> Self {
123        self.topic_fields = Some(value);
124        self
125    }
126
127    pub fn user_fields(mut self, value: HashSet<UserFields>) -> Self {
128        self.user_fields = Some(value);
129        self
130    }
131
132    pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
133        self.twapi_options = Some(value);
134        self
135    }
136
137    pub fn build(&self, authentication: &impl Authentication) -> RequestBuilder {
138        let mut query_parameters = vec![];
139        query_parameters.push(("query", self.query.to_string()));
140        if let Some(expansions) = self.expansions.as_ref() {
141            query_parameters.push(("expansions", expansions.iter().join(",")));
142        }
143        if let Some(space_fields) = self.space_fields.as_ref() {
144            query_parameters.push(("space.fields", space_fields.iter().join(",")));
145        }
146        if let Some(state) = self.state.as_ref() {
147            query_parameters.push(("state", state.to_string()));
148        }
149        if let Some(topic_fields) = self.topic_fields.as_ref() {
150            query_parameters.push(("topic.fields", topic_fields.iter().join(",")));
151        }
152        if let Some(user_fields) = self.user_fields.as_ref() {
153            query_parameters.push(("user.fields", user_fields.iter().join(",")));
154        }
155        let client = reqwest::Client::new();
156        let url = make_url(&self.twapi_options, URL);
157        let builder = client.get(&url).query(&query_parameters);
158        authentication.execute(
159            builder,
160            "GET",
161            &url,
162            &query_parameters
163                .iter()
164                .map(|it| (it.0, it.1.as_str()))
165                .collect::<Vec<_>>(),
166        )
167    }
168
169    pub async fn execute(
170        &self,
171        authentication: &impl Authentication,
172    ) -> Result<(Response, Headers), Error> {
173        execute_twitter(|| self.build(authentication), &self.twapi_options).await
174    }
175}
176
177#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
178pub struct Response {
179    #[serde(skip_serializing_if = "Option::is_none")]
180    pub data: Option<Vec<Spaces>>,
181    #[serde(skip_serializing_if = "Option::is_none")]
182    pub errors: Option<Vec<Errors>>,
183    #[serde(skip_serializing_if = "Option::is_none")]
184    pub includes: Option<Includes>,
185    #[serde(skip_serializing_if = "Option::is_none")]
186    pub meta: Option<Meta>,
187    #[serde(flatten)]
188    pub extra: std::collections::HashMap<String, serde_json::Value>,
189}
190
191impl Response {
192    pub fn is_empty_extra(&self) -> bool {
193        let res = self.extra.is_empty()
194            && self
195                .data
196                .as_ref()
197                .map(|it| it.iter().all(|item| item.is_empty_extra()))
198                .unwrap_or(true)
199            && self
200                .errors
201                .as_ref()
202                .map(|it| it.iter().all(|item| item.is_empty_extra()))
203                .unwrap_or(true)
204            && self
205                .includes
206                .as_ref()
207                .map(|it| it.is_empty_extra())
208                .unwrap_or(true)
209            && self
210                .meta
211                .as_ref()
212                .map(|it| it.is_empty_extra())
213                .unwrap_or(true);
214        if !res {
215            println!("Response {:?}", self.extra);
216        }
217        res
218    }
219}