twapi_v2/api/
get_2_users_id_owned_lists.rs

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