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