Skip to main content

twapi_v2/api/
post_2_lists.rs

1use crate::{
2    api::{Authentication, TwapiOptions, apply_options, 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(
44            apply_options(builder, &self.twapi_options),
45            "POST",
46            &url,
47            &[],
48        )
49    }
50
51    pub async fn execute(
52        self,
53        authentication: &impl Authentication,
54    ) -> Result<(Response, Headers), Error> {
55        execute_twitter(self.build(authentication)).await
56    }
57}
58
59#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
60pub struct Response {
61    #[serde(skip_serializing_if = "Option::is_none")]
62    pub data: Option<Data>,
63    #[serde(flatten)]
64    pub extra: std::collections::HashMap<String, serde_json::Value>,
65}
66
67impl Response {
68    pub fn is_empty_extra(&self) -> bool {
69        let res = self.extra.is_empty()
70            && self
71                .data
72                .as_ref()
73                .map(|it| it.is_empty_extra())
74                .unwrap_or(true);
75        if !res {
76            println!("Response {:?}", self.extra);
77        }
78        res
79    }
80}
81
82#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
83pub struct Data {
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub id: Option<String>,
86    #[serde(skip_serializing_if = "Option::is_none")]
87    pub name: Option<String>,
88    #[serde(flatten)]
89    pub extra: std::collections::HashMap<String, serde_json::Value>,
90}
91
92impl Data {
93    pub fn is_empty_extra(&self) -> bool {
94        let res = self.extra.is_empty();
95        if !res {
96            println!("Data {:?}", self.extra);
97        }
98        res
99    }
100}