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