Skip to main content

twapi_v2/api/
delete_2_tweets_id.rs

1use crate::responses::errors::Errors;
2use crate::{
3    api::{Authentication, TwapiOptions, apply_options, execute_twitter, make_url},
4    error::Error,
5    headers::Headers,
6};
7use reqwest::RequestBuilder;
8use serde::{Deserialize, Serialize};
9
10const URL: &str = "/2/tweets/: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(
36            apply_options(builder, &self.twapi_options),
37            "DELETE",
38            &url,
39            &[],
40        )
41    }
42
43    pub async fn execute(
44        self,
45        authentication: &impl Authentication,
46    ) -> Result<(Response, Headers), Error> {
47        execute_twitter(self.build(authentication)).await
48    }
49}
50
51#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
52pub struct Response {
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub data: Option<Data>,
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub errors: Option<Vec<Errors>>,
57    #[serde(flatten)]
58    pub extra: std::collections::HashMap<String, serde_json::Value>,
59}
60
61impl Response {
62    pub fn is_empty_extra(&self) -> bool {
63        let res = self.extra.is_empty()
64            && self
65                .data
66                .as_ref()
67                .map(|it| it.is_empty_extra())
68                .unwrap_or(true)
69            && self
70                .errors
71                .as_ref()
72                .map(|it| it.iter().all(|item| item.is_empty_extra()))
73                .unwrap_or(true);
74        if !res {
75            println!("Response {:?}", self.extra);
76        }
77        res
78    }
79}
80
81#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
82pub struct Data {
83    #[serde(skip_serializing_if = "Option::is_none")]
84    pub deleted: Option<bool>,
85    #[serde(flatten)]
86    pub extra: std::collections::HashMap<String, serde_json::Value>,
87}
88
89impl Data {
90    pub fn is_empty_extra(&self) -> bool {
91        let res = self.extra.is_empty();
92        if !res {
93            println!("Data {:?}", self.extra);
94        }
95        res
96    }
97}