Skip to main content

twapi_v2/api/
post_2_lists.rs

1use crate::{
2    api::{Authentication, TwapiOptions, execute_twitter, make_url},
3    error::Error,
4    headers::Headers,
5};
6use reqwest::RequestBuilder;
7use serde::{Deserialize, Serialize};
8
9const URL: &str = "/2/lists";
10
11#[derive(Serialize, Deserialize, Debug, Default, Clone)]
12pub struct Body {
13    pub name: String,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub description: Option<String>,
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub private: Option<bool>,
18}
19
20#[derive(Debug, Clone, Default)]
21pub struct Api {
22    body: Body,
23    twapi_options: Option<TwapiOptions>,
24}
25
26impl Api {
27    pub fn new(body: Body) -> Self {
28        Self {
29            body,
30            ..Default::default()
31        }
32    }
33
34    pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
35        self.twapi_options = Some(value);
36        self
37    }
38
39    pub fn build(&self, authentication: &impl Authentication) -> RequestBuilder {
40        let client = reqwest::Client::new();
41        let url = make_url(&self.twapi_options, URL);
42        let builder = client.post(&url).json(&self.body);
43        authentication.execute(builder, "POST", &url, &[])
44    }
45
46    pub async fn execute(
47        &self,
48        authentication: &impl Authentication,
49    ) -> Result<(Response, Headers), Error> {
50        execute_twitter(|| self.build(authentication), &self.twapi_options).await
51    }
52}
53
54#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
55pub struct Response {
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub data: Option<Data>,
58    #[serde(flatten)]
59    pub extra: std::collections::HashMap<String, serde_json::Value>,
60}
61
62impl Response {
63    pub fn is_empty_extra(&self) -> bool {
64        let res = self.extra.is_empty()
65            && self
66                .data
67                .as_ref()
68                .map(|it| it.is_empty_extra())
69                .unwrap_or(true);
70        if !res {
71            println!("Response {:?}", self.extra);
72        }
73        res
74    }
75}
76
77#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
78pub struct Data {
79    #[serde(skip_serializing_if = "Option::is_none")]
80    pub id: Option<String>,
81    #[serde(skip_serializing_if = "Option::is_none")]
82    pub name: Option<String>,
83    #[serde(flatten)]
84    pub extra: std::collections::HashMap<String, serde_json::Value>,
85}
86
87impl Data {
88    pub fn is_empty_extra(&self) -> bool {
89        let res = self.extra.is_empty();
90        if !res {
91            println!("Data {:?}", self.extra);
92        }
93        res
94    }
95}