Skip to main content

twapi_v2/api/
delete_2_lists_id.rs

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