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