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