twapi_v2/api/
get_2_users_compliance_stream.rs1use crate::responses::compliance::Compliance;
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/users/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.as_ref() {
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 builder,
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), &self.twapi_options).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 user_delete: Option<Compliance>,
92 #[serde(skip_serializing_if = "Option::is_none")]
93 pub user_undelete: Option<Compliance>,
94 #[serde(skip_serializing_if = "Option::is_none")]
95 pub user_withheld: Option<Compliance>,
96 #[serde(skip_serializing_if = "Option::is_none")]
97 pub user_protect: Option<Compliance>,
98 #[serde(skip_serializing_if = "Option::is_none")]
99 pub user_unprotect: Option<Compliance>,
100 #[serde(skip_serializing_if = "Option::is_none")]
101 pub user_suspend: Option<Compliance>,
102 #[serde(skip_serializing_if = "Option::is_none")]
103 pub user_unsuspend: Option<Compliance>,
104 #[serde(skip_serializing_if = "Option::is_none")]
105 pub scrub_geo: Option<Compliance>,
106 #[serde(skip_serializing_if = "Option::is_none")]
107 pub user_profile_modification: Option<Compliance>,
108 #[serde(flatten)]
109 pub extra: std::collections::HashMap<String, serde_json::Value>,
110}
111
112impl Data {
113 pub fn is_empty_extra(&self) -> bool {
114 let res = self.extra.is_empty()
115 && self
116 .user_delete
117 .as_ref()
118 .map(|it| it.is_empty_extra())
119 .unwrap_or(true)
120 && self
121 .user_undelete
122 .as_ref()
123 .map(|it| it.is_empty_extra())
124 .unwrap_or(true)
125 && self
126 .user_withheld
127 .as_ref()
128 .map(|it| it.is_empty_extra())
129 .unwrap_or(true)
130 && self
131 .user_protect
132 .as_ref()
133 .map(|it| it.is_empty_extra())
134 .unwrap_or(true)
135 && self
136 .user_unprotect
137 .as_ref()
138 .map(|it| it.is_empty_extra())
139 .unwrap_or(true)
140 && self
141 .user_suspend
142 .as_ref()
143 .map(|it| it.is_empty_extra())
144 .unwrap_or(true)
145 && self
146 .user_unsuspend
147 .as_ref()
148 .map(|it| it.is_empty_extra())
149 .unwrap_or(true)
150 && self
151 .scrub_geo
152 .as_ref()
153 .map(|it| it.is_empty_extra())
154 .unwrap_or(true)
155 && self
156 .user_profile_modification
157 .as_ref()
158 .map(|it| it.is_empty_extra())
159 .unwrap_or(true);
160 if !res {
161 println!("Data {:?}", self.extra);
162 }
163 res
164 }
165}