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