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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#![doc(alias = "TMI")]
use crate::types;
use serde::{Deserialize, Serialize};
#[cfg(all(feature = "client", feature = "tmi"))]
#[cfg_attr(nightly, doc(cfg(all(feature = "client", feature = "tmi"))))]
#[derive(Default, Clone)]
pub struct TmiClient<'a, C: crate::HttpClient<'a>> {
client: C,
_pd: std::marker::PhantomData<&'a ()>,
}
#[cfg(all(feature = "tmi", feature = "client"))]
impl<'a, C: crate::HttpClient<'a>> TmiClient<'a, C> {
pub fn new() -> TmiClient<'a, C>
where C: Default {
TmiClient::with_client(C::default())
}
pub fn with_client(client: C) -> TmiClient<'a, C> {
TmiClient {
client,
_pd: std::marker::PhantomData::default(),
}
}
pub fn clone_client(&self) -> C
where C: Clone {
self.client.clone()
}
pub async fn get_chatters(
&'a self,
broadcaster: &str,
) -> Result<GetChatters, RequestError<<C as crate::HttpClient<'a>>::Error>> {
let url = format!(
"{}{}{}{}",
crate::TWITCH_TMI_URL,
"group/user/",
broadcaster.replace('#', "").to_ascii_lowercase(),
"/chatters"
);
let req = http::Request::builder()
.uri(url)
.body(Vec::with_capacity(0))?;
let req = self
.client
.req(req)
.await
.map_err(|e| RequestError::RequestError(Box::new(e)))?;
let text = std::str::from_utf8(&req.body())
.map_err(|e| RequestError::Utf8Error(req.body().clone(), e))?;
serde_json::from_str(text).map_err(Into::into)
}
pub async fn get_hosts(
&'a self,
include_logins: bool,
channel_id: HostsRequestId,
) -> Result<GetHosts, RequestError<<C as crate::HttpClient<'a>>::Error>> {
let url = format!(
"{}{}{}{}",
crate::TWITCH_TMI_URL,
"hosts?",
if include_logins {
"include_logins=1&"
} else {
""
},
match channel_id {
HostsRequestId::Host(id) => format!("host={}", id),
HostsRequestId::Target(id) => format!("target={}", id),
}
);
let req = http::Request::builder()
.uri(url)
.body(Vec::with_capacity(0))?;
let req = self
.client
.req(req)
.await
.map_err(|e| RequestError::RequestError(Box::new(e)))?;
let text = std::str::from_utf8(&req.body())
.map_err(|e| RequestError::Utf8Error(req.body().clone(), e))?;
serde_json::from_str(text).map_err(Into::into)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GetChatters {
pub chatter_count: u64,
pub chatters: Chatters,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Chatters {
pub broadcaster: Vec<types::Nickname>,
pub vips: Vec<types::Nickname>,
pub moderators: Vec<types::Nickname>,
pub staff: Vec<types::Nickname>,
pub admins: Vec<types::Nickname>,
pub global_mods: Vec<types::Nickname>,
pub viewers: Vec<types::Nickname>,
}
#[derive(Debug)]
pub enum HostsRequestId {
Host(UserId),
Target(UserId),
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GetHosts {
pub hosts: Vec<Host>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Host {
pub host_id: UserId,
pub target_id: Option<UserId>,
pub host_login: Option<types::Nickname>,
pub target_login: Option<types::Nickname>,
pub host_display_name: Option<types::Nickname>,
pub target_display_name: Option<types::Nickname>,
}
pub type UserId = u64;
#[derive(thiserror::Error, Debug, displaydoc::Display)]
pub enum RequestError<RE: std::error::Error + Send + Sync + 'static> {
HttpError(#[from] http::Error),
DeserializeError(#[from] serde_json::Error),
RequestError(#[from] Box<RE>),
Utf8Error(Vec<u8>, std::str::Utf8Error),
}