Skip to main content

twapi_v2/api/
get_2_tweets_search_stream_rules.rs

1use crate::responses::{errors::Errors, streams::Streams};
2use crate::{
3    api::{Authentication, TwapiOptions, execute_twitter, make_url},
4    error::Error,
5    headers::Headers,
6};
7use chrono::prelude::*;
8use reqwest::RequestBuilder;
9use serde::{Deserialize, Serialize};
10
11const URL: &str = "/2/tweets/search/stream/rules";
12
13#[derive(Debug, Clone, Default)]
14pub struct Api {
15    ids: Option<String>,
16    twapi_options: Option<TwapiOptions>,
17}
18
19impl Api {
20    pub fn new() -> Self {
21        Self {
22            ..Default::default()
23        }
24    }
25
26    pub fn ids(mut self, value: &str) -> Self {
27        self.ids = Some(value.to_owned());
28        self
29    }
30
31    pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
32        self.twapi_options = Some(value);
33        self
34    }
35
36    pub fn build(&self, authentication: &impl Authentication) -> RequestBuilder {
37        let mut query_parameters = vec![];
38        if let Some(ids) = self.ids.as_ref() {
39            query_parameters.push(("ids", ids.to_string()));
40        }
41        let client = reqwest::Client::new();
42        let url = make_url(&self.twapi_options, URL);
43        let builder = client.get(&url).query(&query_parameters);
44        authentication.execute(
45            builder,
46            "GET",
47            &url,
48            &query_parameters
49                .iter()
50                .map(|it| (it.0, it.1.as_str()))
51                .collect::<Vec<_>>(),
52        )
53    }
54
55    pub async fn execute(
56        &self,
57        authentication: &impl Authentication,
58    ) -> Result<(Response, Headers), Error> {
59        execute_twitter(|| self.build(authentication), &self.twapi_options).await
60    }
61}
62
63#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
64pub struct Response {
65    #[serde(skip_serializing_if = "Option::is_none")]
66    pub data: Option<Vec<Streams>>,
67    #[serde(skip_serializing_if = "Option::is_none")]
68    pub errors: Option<Vec<Errors>>,
69    pub meta: Meta,
70    #[serde(flatten)]
71    pub extra: std::collections::HashMap<String, serde_json::Value>,
72}
73
74impl Response {
75    pub fn is_empty_extra(&self) -> bool {
76        let res = self.extra.is_empty()
77            && self
78                .data
79                .as_ref()
80                .map(|it| it.iter().all(|item| item.is_empty_extra()))
81                .unwrap_or(true)
82            && self
83                .errors
84                .as_ref()
85                .map(|it| it.iter().all(|item| item.is_empty_extra()))
86                .unwrap_or(true)
87            && self.meta.is_empty_extra();
88        if !res {
89            println!("Response {:?}", self.extra);
90        }
91        res
92    }
93}
94
95#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
96pub struct Meta {
97    #[serde(skip_serializing_if = "Option::is_none")]
98    pub result_count: Option<i64>,
99    pub sent: DateTime<Utc>,
100    #[serde(flatten)]
101    pub extra: std::collections::HashMap<String, serde_json::Value>,
102}
103
104impl Meta {
105    pub fn is_empty_extra(&self) -> bool {
106        let res = self.extra.is_empty();
107        if !res {
108            println!("Meta {:?}", self.extra);
109        }
110        res
111    }
112}