Skip to main content

twapi_v2/api/
get_2_users_id_pinned_lists.rs

1use crate::fields::{list_fields::ListFields, user_fields::UserFields};
2use crate::responses::{errors::Errors, includes::Includes, memberships::Memberships, meta::Meta};
3use crate::{
4    api::{Authentication, TwapiOptions, execute_twitter, make_url},
5    error::Error,
6    headers::Headers,
7};
8use itertools::Itertools;
9use reqwest::RequestBuilder;
10use serde::{Deserialize, Serialize};
11use std::collections::HashSet;
12
13const URL: &str = "/2/users/:id/pinned_lists";
14
15#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Clone, Default)]
16pub enum Expansions {
17    #[serde(rename = "owner_id")]
18    #[default]
19    OwnerId,
20}
21
22impl Expansions {
23    pub fn all() -> HashSet<Self> {
24        let mut result = HashSet::new();
25        result.insert(Self::OwnerId);
26        result
27    }
28}
29
30impl std::fmt::Display for Expansions {
31    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
32        match self {
33            Self::OwnerId => write!(f, "owner_id"),
34        }
35    }
36}
37
38#[derive(Debug, Clone, Default)]
39pub struct Api {
40    id: String,
41    expansions: Option<HashSet<Expansions>>,
42    list_fields: Option<HashSet<ListFields>>,
43    user_fields: Option<HashSet<UserFields>>,
44    twapi_options: Option<TwapiOptions>,
45}
46
47impl Api {
48    pub fn new(id: &str) -> Self {
49        Self {
50            id: id.to_owned(),
51            ..Default::default()
52        }
53    }
54
55    pub fn all(id: &str) -> Self {
56        Self {
57            id: id.to_owned(),
58            expansions: Some(Expansions::all()),
59            list_fields: Some(ListFields::all()),
60            user_fields: Some(UserFields::all()),
61            ..Default::default()
62        }
63    }
64
65    pub fn expansions(mut self, value: HashSet<Expansions>) -> Self {
66        self.expansions = Some(value);
67        self
68    }
69
70    pub fn list_fields(mut self, value: HashSet<ListFields>) -> Self {
71        self.list_fields = Some(value);
72        self
73    }
74
75    pub fn user_fields(mut self, value: HashSet<UserFields>) -> Self {
76        self.user_fields = Some(value);
77        self
78    }
79
80    pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
81        self.twapi_options = Some(value);
82        self
83    }
84
85    pub fn build(&self, authentication: &impl Authentication) -> RequestBuilder {
86        let mut query_parameters = vec![];
87        if let Some(expansions) = self.expansions.as_ref() {
88            query_parameters.push(("expansions", expansions.iter().join(",")));
89        }
90        if let Some(list_fields) = self.list_fields.as_ref() {
91            query_parameters.push(("list.fields", list_fields.iter().join(",")));
92        }
93        if let Some(user_fields) = self.user_fields.as_ref() {
94            query_parameters.push(("user.fields", user_fields.iter().join(",")));
95        }
96        let client = reqwest::Client::new();
97        let url = make_url(&self.twapi_options, &URL.replace(":id", &self.id));
98        let builder = client.get(&url).query(&query_parameters);
99        authentication.execute(
100            builder,
101            "GET",
102            &url,
103            &query_parameters
104                .iter()
105                .map(|it| (it.0, it.1.as_str()))
106                .collect::<Vec<_>>(),
107        )
108    }
109
110    pub async fn execute(
111        &self,
112        authentication: &impl Authentication,
113    ) -> Result<(Response, Headers), Error> {
114        execute_twitter(|| self.build(authentication), &self.twapi_options).await
115    }
116}
117
118#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
119pub struct Response {
120    #[serde(skip_serializing_if = "Option::is_none")]
121    pub data: Option<Vec<Memberships>>,
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub errors: Option<Vec<Errors>>,
124    #[serde(skip_serializing_if = "Option::is_none")]
125    pub includes: Option<Includes>,
126    #[serde(skip_serializing_if = "Option::is_none")]
127    pub meta: Option<Meta>,
128    #[serde(flatten)]
129    pub extra: std::collections::HashMap<String, serde_json::Value>,
130}
131
132impl Response {
133    pub fn is_empty_extra(&self) -> bool {
134        let res = self.extra.is_empty()
135            && self
136                .data
137                .as_ref()
138                .map(|it| it.iter().all(|item| item.is_empty_extra()))
139                .unwrap_or(true)
140            && self
141                .errors
142                .as_ref()
143                .map(|it| it.iter().all(|item| item.is_empty_extra()))
144                .unwrap_or(true)
145            && self
146                .includes
147                .as_ref()
148                .map(|it| it.is_empty_extra())
149                .unwrap_or(true)
150            && self
151                .meta
152                .as_ref()
153                .map(|it| it.is_empty_extra())
154                .unwrap_or(true);
155        if !res {
156            println!("Response {:?}", self.extra);
157        }
158        res
159    }
160}