Skip to main content

twapi_v2/api/
get_2_tweets_compliance_stream.rs

1use crate::responses::compliance::Compliance;
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/compliance/stream";
11
12#[derive(Debug, Clone, Default)]
13pub struct Api {
14    partition: usize,
15    backfill_minutes: Option<usize>,
16    twapi_options: Option<TwapiOptions>,
17}
18
19impl Api {
20    pub fn new(partition: usize) -> Self {
21        Self {
22            partition,
23            ..Default::default()
24        }
25    }
26
27    pub fn backfill_minutes(mut self, value: usize) -> Self {
28        self.backfill_minutes = Some(value);
29        self
30    }
31
32    pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
33        self.twapi_options = Some(value);
34        self
35    }
36
37    pub fn build(self, authentication: &impl Authentication) -> RequestBuilder {
38        let mut query_parameters = vec![];
39        query_parameters.push(("partition", self.partition.to_string()));
40        if let Some(backfill_minutes) = self.backfill_minutes {
41            query_parameters.push(("backfill_minutes", backfill_minutes.to_string()));
42        }
43        let client = reqwest::Client::new();
44        let url = make_url(&self.twapi_options, URL);
45        let builder = client.get(&url).query(&query_parameters);
46        authentication.execute(
47            apply_options(builder, &self.twapi_options),
48            "GET",
49            &url,
50            &query_parameters
51                .iter()
52                .map(|it| (it.0, it.1.as_str()))
53                .collect::<Vec<_>>(),
54        )
55    }
56
57    pub async fn execute(
58        self,
59        authentication: &impl Authentication,
60    ) -> Result<(Response, Headers), Error> {
61        execute_twitter(self.build(authentication)).await
62    }
63}
64
65#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
66pub struct Response {
67    #[serde(skip_serializing_if = "Option::is_none")]
68    pub data: Option<Data>,
69    #[serde(flatten)]
70    pub extra: std::collections::HashMap<String, serde_json::Value>,
71}
72
73impl Response {
74    pub fn is_empty_extra(&self) -> bool {
75        let res = self.extra.is_empty()
76            && self
77                .data
78                .as_ref()
79                .map(|it| it.is_empty_extra())
80                .unwrap_or(true);
81        if !res {
82            println!("Response {:?}", self.extra);
83        }
84        res
85    }
86}
87
88#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
89pub struct Data {
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub delete: Option<Compliance>,
92    #[serde(skip_serializing_if = "Option::is_none")]
93    pub withheld: Option<Compliance>,
94    #[serde(skip_serializing_if = "Option::is_none")]
95    pub drop: Option<Compliance>,
96    #[serde(skip_serializing_if = "Option::is_none")]
97    pub undrop: Option<Compliance>,
98    #[serde(flatten)]
99    pub extra: std::collections::HashMap<String, serde_json::Value>,
100}
101
102impl Data {
103    pub fn is_empty_extra(&self) -> bool {
104        let res = self.extra.is_empty()
105            && self
106                .delete
107                .as_ref()
108                .map(|it| it.is_empty_extra())
109                .unwrap_or(true)
110            && self
111                .withheld
112                .as_ref()
113                .map(|it| it.is_empty_extra())
114                .unwrap_or(true)
115            && self
116                .drop
117                .as_ref()
118                .map(|it| it.is_empty_extra())
119                .unwrap_or(true)
120            && self
121                .undrop
122                .as_ref()
123                .map(|it| it.is_empty_extra())
124                .unwrap_or(true);
125        if !res {
126            println!("Data {:?}", self.extra);
127        }
128        res
129    }
130}