twapi_v2/api/
get_2_spaces_id.rs

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::{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/:id";
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    id: 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(id: &str) -> Self {
73        Self {
74            id: id.to_owned(),
75            ..Default::default()
76        }
77    }
78
79    pub fn all(id: &str) -> Self {
80        Self {
81            id: id.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        if let Some(expansions) = self.expansions {
118            query_parameters.push(("expansions", expansions.iter().join(",")));
119        }
120        if let Some(space_fields) = self.space_fields {
121            query_parameters.push(("space.fields", space_fields.iter().join(",")));
122        }
123        if let Some(topic_fields) = self.topic_fields {
124            query_parameters.push(("topic.fields", topic_fields.iter().join(",")));
125        }
126        if let Some(user_fields) = self.user_fields {
127            query_parameters.push(("user.fields", user_fields.iter().join(",")));
128        }
129        let client = reqwest::Client::new();
130        let url = make_url(&self.twapi_options, &URL.replace(":id", &self.id));
131        let builder = client.get(&url).query(&query_parameters);
132        authentication.execute(
133            apply_options(builder, &self.twapi_options),
134            "GET",
135            &url,
136            &query_parameters
137                .iter()
138                .map(|it| (it.0, it.1.as_str()))
139                .collect::<Vec<_>>(),
140        )
141    }
142
143    pub async fn execute(
144        self,
145        authentication: &impl Authentication,
146    ) -> Result<(Response, Headers), Error> {
147        execute_twitter(self.build(authentication)).await
148    }
149}
150
151#[derive(Serialize, Deserialize, Debug, Clone, Default)]
152pub struct Response {
153    #[serde(skip_serializing_if = "Option::is_none")]
154    pub data: Option<Spaces>,
155    #[serde(skip_serializing_if = "Option::is_none")]
156    pub errors: Option<Vec<Errors>>,
157    #[serde(skip_serializing_if = "Option::is_none")]
158    pub includes: Option<Includes>,
159    #[serde(flatten)]
160    pub extra: std::collections::HashMap<String, serde_json::Value>,
161}
162
163impl Response {
164    pub fn is_empty_extra(&self) -> bool {
165        let res = self.extra.is_empty()
166            && self
167                .data
168                .as_ref()
169                .map(|it| it.is_empty_extra())
170                .unwrap_or(true)
171            && self
172                .errors
173                .as_ref()
174                .map(|it| it.iter().all(|item| item.is_empty_extra()))
175                .unwrap_or(true)
176            && self
177                .includes
178                .as_ref()
179                .map(|it| it.is_empty_extra())
180                .unwrap_or(true);
181        if !res {
182            println!("Response {:?}", self.extra);
183        }
184        res
185    }
186}