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
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use serde::de::DeserializeOwned;
use crate::builder::types::{ConfQueryInner, Resp, RoomQueryInner};
use crate::config::StreamConfig;
use crate::errors::BuildError;
#[cfg(test)]
mod tests;
mod types;
type BoxedError = Box<dyn std::error::Error>;
#[cfg(feature = "not-send")]
pub trait Requester {
fn get_json<T: DeserializeOwned>(
&self,
url: &str,
) -> Pin<Box<dyn Future<Output = Result<T, BoxedError>> + '_>>;
}
#[cfg(not(feature = "not-send"))]
pub trait Requester: Send + Sync {
fn get_json<T: DeserializeOwned>(
&self,
url: &str,
) -> Pin<Box<dyn Future<Output = Result<T, BoxedError>> + Send + '_>>;
}
#[doc(hidden)]
pub enum BF {}
#[doc(hidden)]
pub enum BN {}
#[derive(Debug)]
pub struct ConfigBuilder<H, R, U, T, S> {
http: H,
room_id: Option<u64>,
uid: Option<u64>,
token: Option<String>,
servers: Option<Vec<String>>,
__marker: PhantomData<(R, U, T, S)>,
}
impl<H: Default> ConfigBuilder<H, BN, BN, BN, BN> {
#[allow(clippy::new_without_default)]
#[must_use]
pub fn new() -> Self {
Self::new_with_client(H::default())
}
}
impl<H> ConfigBuilder<H, BN, BN, BN, BN> {
#[must_use]
pub const fn new_with_client(client: H) -> Self {
Self {
http: client,
room_id: None,
uid: None,
token: None,
servers: None,
__marker: PhantomData,
}
}
}
impl<H, R, U, T, S> ConfigBuilder<H, R, U, T, S> {
#[allow(clippy::missing_const_for_fn)]
fn cast<R2, U2, T2, S2>(self) -> ConfigBuilder<H, R2, U2, T2, S2> {
ConfigBuilder {
http: self.http,
room_id: self.room_id,
uid: self.uid,
token: self.token,
servers: self.servers,
__marker: PhantomData,
}
}
}
impl<H, R, U, T, S> ConfigBuilder<H, R, U, T, S> {
#[must_use]
pub fn room_id(mut self, room_id: u64) -> ConfigBuilder<H, BF, U, T, S> {
self.room_id = Some(room_id);
self.cast()
}
#[must_use]
pub fn uid(mut self, uid: u64) -> ConfigBuilder<H, R, BF, T, S> {
self.uid = Some(uid);
self.cast()
}
#[must_use]
pub fn token(mut self, token: &str) -> ConfigBuilder<H, R, U, BF, S> {
self.token = Some(token.to_string());
self.cast()
}
#[must_use]
pub fn servers(mut self, servers: &[String]) -> ConfigBuilder<H, R, U, T, BF> {
self.servers = Some(servers.to_vec());
self.cast()
}
}
impl<H, R, U, T, S> ConfigBuilder<H, R, U, T, S>
where
H: Requester,
R: Send + Sync,
U: Send + Sync,
T: Send + Sync,
S: Send + Sync,
{
pub async fn by_uid(mut self, uid: u64) -> Result<ConfigBuilder<H, BF, BF, T, S>, BuildError> {
let resp: Resp<RoomQueryInner> = self
.http
.get_json(&*format!(
"https://api.live.bilibili.com/bili/living_v2/{}",
uid
))
.await
.map_err(BuildError)?;
let room_id = resp.room_id();
self.room_id = Some(room_id);
self.uid = Some(uid);
Ok(self.cast())
}
pub async fn fetch_conf(mut self) -> Result<ConfigBuilder<H, R, U, BF, BF>, BuildError> {
let resp: Resp<ConfQueryInner> = self
.http
.get_json("https://api.live.bilibili.com/room/v1/Danmu/getConf")
.await
.map_err(BuildError)?;
self.token = Some(resp.token().to_string());
self.servers = Some(resp.servers());
Ok(self.cast())
}
}
impl<H> ConfigBuilder<H, BF, BF, BF, BF> {
#[allow(clippy::missing_panics_doc)]
pub fn build(self) -> StreamConfig {
StreamConfig::new(
self.room_id.unwrap(),
self.uid.unwrap(),
self.token.unwrap(),
self.servers.unwrap(),
)
}
}