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