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