twitter_v2/requests/
stream_rule.rs

1use crate::api::TwitterApi;
2use crate::api_result::ApiResult;
3use crate::authorization::Authorization;
4use crate::data::StreamRule;
5use crate::id::{IntoNumericId, NumericId};
6use crate::meta::StreamRuleMeta;
7use crate::query::UrlQueryExt;
8use reqwest::Method;
9use serde::{Deserialize, Serialize};
10use url::Url;
11
12#[derive(Clone, Debug, Serialize, Deserialize)]
13struct DraftStreamRuleAdd {
14    value: String,
15    tag: Option<String>,
16}
17#[derive(Clone, Debug, Serialize, Deserialize, Default)]
18struct DraftStreamRuleDelete {
19    ids: Vec<NumericId>,
20}
21
22#[derive(Clone, Debug, Serialize, Deserialize, Default)]
23struct DraftStreamRule {
24    add: Option<Vec<DraftStreamRuleAdd>>,
25    delete: Option<DraftStreamRuleDelete>,
26}
27
28#[derive(Debug)]
29pub struct StreamRuleBuilder<A> {
30    client: TwitterApi<A>,
31    url: Url,
32    stream_rule: DraftStreamRule,
33}
34impl<A> StreamRuleBuilder<A>
35where
36    A: Authorization,
37{
38    pub(crate) fn new(client: &TwitterApi<A>, url: Url) -> Self {
39        Self {
40            client: client.clone(),
41            url,
42            stream_rule: Default::default(),
43        }
44    }
45    pub fn dry_run(&mut self) -> &mut Self {
46        self.url.append_query_val("dry_run", true);
47        self
48    }
49    pub fn add(&mut self, value: impl ToString) -> &mut Self {
50        if let Some(add) = self.stream_rule.add.as_mut() {
51            add.push(DraftStreamRuleAdd {
52                value: value.to_string(),
53                tag: None,
54            });
55        } else {
56            self.stream_rule.add = Some(vec![DraftStreamRuleAdd {
57                value: value.to_string(),
58                tag: None,
59            }]);
60        }
61        self
62    }
63    pub fn add_tagged(&mut self, value: impl ToString, tag: impl ToString) -> &mut Self {
64        if let Some(add) = self.stream_rule.add.as_mut() {
65            add.push(DraftStreamRuleAdd {
66                value: value.to_string(),
67                tag: Some(tag.to_string()),
68            });
69        } else {
70            self.stream_rule.add = Some(vec![DraftStreamRuleAdd {
71                value: value.to_string(),
72                tag: Some(tag.to_string()),
73            }]);
74        }
75        self
76    }
77    pub fn delete_id(&mut self, id: impl IntoNumericId) -> &mut Self {
78        self.delete_ids([id]);
79        self
80    }
81    pub fn delete_ids(&mut self, ids: impl IntoIterator<Item = impl IntoNumericId>) -> &mut Self {
82        if let Some(delete) = self.stream_rule.delete.as_mut() {
83            for id in ids {
84                delete.ids.push(id.into_id())
85            }
86        } else {
87            self.stream_rule.delete = Some(DraftStreamRuleDelete {
88                ids: ids.into_iter().map(|id| id.into_id()).collect(),
89            });
90        }
91        self
92    }
93    pub async fn send(&self) -> ApiResult<A, Vec<StreamRule>, StreamRuleMeta> {
94        self.client
95            .send(
96                self.client
97                    .request(Method::POST, self.url.clone())
98                    .json(&self.stream_rule),
99            )
100            .await
101    }
102}
103
104impl<A> Clone for StreamRuleBuilder<A> {
105    fn clone(&self) -> Self {
106        Self {
107            client: self.client.clone(),
108            url: self.url.clone(),
109            stream_rule: self.stream_rule.clone(),
110        }
111    }
112}