lavalink_rs/
http.rs

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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use crate::error::LavalinkResult;
use crate::model::*;

use std::sync::Arc;

use ::http::{uri::InvalidUri, Method, Uri};
use http_body_util::BodyExt;
use hyper::{body::Buf, Request};
use std::io::Read;

#[derive(Debug, Clone)]
#[cfg_attr(feature = "python", pyo3::pyclass)]
pub struct Http {
    pub authority: String,
    pub rest_address: String,
    pub rest_address_versionless: String,
    pub headers: ::http::header::HeaderMap,
    pub request_client: Arc<
        hyper_util::client::legacy::Client<
            crate::HttpsConnector,
            http_body_util::Full<bytes::Bytes>,
        >,
    >,
}

impl Http {
    /// Makes an HTTP/1.1 request using Hyper to endpoints that return deserializable data.
    pub async fn request<R: serde::de::DeserializeOwned, T: serde::Serialize + ?Sized, U>(
        &self,
        method: Method,
        uri: U,
        data: Option<&T>,
    ) -> LavalinkResult<R>
    where
        Uri: TryFrom<U>,
        <Uri as TryFrom<U>>::Error: Into<::http::Error>,
    {
        let mut request_builder = Request::builder().method(method).uri(uri);

        {
            *request_builder.headers_mut().unwrap() = self.headers.clone()
        }

        request_builder = request_builder.header(::http::header::HOST, &self.authority);

        let request = if let Some(data) = data {
            request_builder =
                request_builder.header(::http::header::CONTENT_TYPE, "application/json");
            request_builder.body(http_body_util::Full::from(serde_json::to_vec(data)?))?
        } else {
            request_builder.body(http_body_util::Full::default())?
        };

        let response = self.request_client.request(request).await?;

        let raw_body = response.collect().await?.aggregate();
        let body = serde_json::from_reader(raw_body.reader())?;

        Ok(body)
    }

    /// Makes an HTTP/1.1 request using Hyper to endpoints that return text data.
    pub async fn raw_request<T: serde::Serialize + ?Sized, U>(
        &self,
        method: Method,
        uri: U,
        data: Option<&T>,
    ) -> LavalinkResult<String>
    where
        Uri: TryFrom<U>,
        <Uri as TryFrom<U>>::Error: Into<::http::Error>,
    {
        let mut request_builder = Request::builder().method(method).uri(uri);

        {
            *request_builder.headers_mut().unwrap() = self.headers.clone()
        }

        request_builder = request_builder.header(::http::header::HOST, &self.authority);

        let request = if let Some(data) = data {
            request_builder =
                request_builder.header(::http::header::CONTENT_TYPE, "application/json");
            request_builder.body(http_body_util::Full::from(serde_json::to_vec(data)?))?
        } else {
            request_builder.body(http_body_util::Full::default())?
        };

        let response = self.request_client.request(request).await?;

        let mut body = "".to_string();
        let raw_body = response.collect().await?.aggregate();
        raw_body.reader().read_to_string(&mut body)?;

        Ok(body)
    }

    /// Convert a path and query to a uri that points to the lavalink server.
    pub fn path_to_uri(&self, path: &str, with_version: bool) -> Result<Uri, InvalidUri> {
        if with_version {
            format!("{}{}", self.rest_address, path).try_into()
        } else {
            format!("{}{}", self.rest_address_versionless, path).try_into()
        }
    }

    /// Destroys the player for this guild in this session.
    pub async fn delete_player(
        &self,
        guild_id: impl Into<GuildId>,
        session_id: &str,
    ) -> LavalinkResult<()> {
        let guild_id = guild_id.into();

        self.raw_request(
            Method::DELETE,
            self.path_to_uri(
                &format!("/sessions/{}/players/{}", session_id, guild_id.0),
                true,
            )?,
            None::<&()>,
        )
        .await?;

        Ok(())
    }

    /// Updates or creates the player for this guild.
    pub async fn update_player(
        &self,
        guild_id: impl Into<GuildId>,
        session_id: &str,
        data: &http::UpdatePlayer,
        no_replace: bool,
    ) -> LavalinkResult<player::Player> {
        let guild_id = guild_id.into();

        let uri = self.path_to_uri(
            &format!(
                "/sessions/{}/players/{}?noReplace={}",
                session_id, guild_id.0, no_replace
            ),
            true,
        )?;

        let response = self
            .request::<crate::error::RequestResult<_>, _, _>(Method::PATCH, uri, Some(data))
            .await?
            .to_result()?;

        Ok(response)
    }

    /// Updates the session with the resuming state and timeout.
    pub async fn set_resuming_state(
        &self,
        session_id: &str,
        resuming_state: &http::ResumingState,
    ) -> LavalinkResult<http::ResumingState> {
        let response = self
            .request::<crate::error::RequestResult<_>, _, _>(
                Method::PATCH,
                self.path_to_uri(&format!("/sessions/{}", session_id), true)?,
                Some(resuming_state),
            )
            .await?
            .to_result()?;

        Ok(response)
    }

    /// Resolves audio tracks for use with the `update_player` endpoint.
    ///
    /// # Parameters
    ///
    /// - `identifier`: A track identifier.
    ///  - Can be a url: "https://youtu.be/watch?v=DrM2lo6B04I"
    ///  - A unique identifier: "DrM2lo6B04I"
    ///  - A search: "ytsearch:Ne Obliviscaris - Forget Not"
    pub async fn load_tracks(&self, identifier: &str) -> LavalinkResult<track::Track> {
        let uri = self.path_to_uri(
            &format!("/loadtracks?identifier={}", urlencoding::encode(identifier)),
            true,
        )?;

        let response = self
            .request::<crate::error::RequestResult<track::Track>, _, _>(
                Method::GET,
                uri,
                None::<&()>,
            )
            .await?
            .to_result()?;

        match response.data {
            Some(track::TrackLoadData::Error(why)) => Err(why.into()),
            _ => Ok(response),
        }
    }

    /// Request Lavalink server version.
    pub async fn version(&self) -> LavalinkResult<String> {
        let response = self
            .raw_request(
                Method::GET,
                self.path_to_uri("/version", false)?,
                None::<&()>,
            )
            .await?;

        Ok(response)
    }

    /// Request Lavalink statistics.
    ///
    /// NOTE: The frame stats will never be returned.
    pub async fn stats(&self) -> LavalinkResult<events::Stats> {
        let response = self
            .request::<crate::error::RequestResult<_>, _, _>(
                Method::GET,
                self.path_to_uri("/stats", true)?,
                None::<&()>,
            )
            .await?
            .to_result()?;

        Ok(response)
    }

    /// Request Lavalink server information.
    pub async fn info(&self) -> LavalinkResult<http::Info> {
        let response = self
            .request::<crate::error::RequestResult<_>, _, _>(
                Method::GET,
                self.path_to_uri("/info", true)?,
                None::<&()>,
            )
            .await?
            .to_result()?;

        Ok(response)
    }

    /// Decode a single track into its info.
    ///
    /// # Parameters
    ///
    /// - `track`: base64 encoded track data.
    pub async fn decode_track(&self, track: &str) -> LavalinkResult<track::TrackData> {
        let uri = self.path_to_uri(
            &format!("/decodetrack?encodedTrack={}", urlencoding::encode(track)),
            true,
        )?;

        let response = self
            .request::<crate::error::RequestResult<_>, _, _>(Method::GET, uri, None::<&()>)
            .await?
            .to_result()?;

        Ok(response)
    }

    /// Decode multiple tracks into their info.
    ///
    /// # Parameters
    ///
    /// - `tracks`: base64 encoded tracks.
    pub async fn decode_tracks(&self, tracks: &[String]) -> LavalinkResult<Vec<track::TrackData>> {
        let response = self
            .request::<crate::error::RequestResult<_>, _, _>(
                Method::POST,
                self.path_to_uri("/decodetracks", true)?,
                Some(tracks),
            )
            .await?
            .to_result()?;

        Ok(response)
    }

    /// Returns the player for this guild in this session.
    pub async fn get_player(
        &self,
        guild_id: impl Into<GuildId>,
        session_id: &str,
    ) -> LavalinkResult<player::Player> {
        let guild_id = guild_id.into();

        let response = self
            .request::<crate::error::RequestResult<_>, _, _>(
                Method::GET,
                self.path_to_uri(
                    &format!("/sessions/{}/players/{}", session_id, guild_id.0),
                    true,
                )?,
                None::<&()>,
            )
            .await?
            .to_result()?;

        Ok(response)
    }

    /// Returns a list of players in this specific session.
    pub async fn get_players(&self, session_id: &str) -> LavalinkResult<Vec<player::Player>> {
        let response = self
            .request::<crate::error::RequestResult<_>, _, _>(
                Method::GET,
                self.path_to_uri(&format!("/sessions/{}/players", session_id), true)?,
                None::<&()>,
            )
            .await?
            .to_result()?;

        Ok(response)
    }
}