srs-client 0.3.0

Provides bindings for the main functionalities of the SRS
Documentation
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! [HTTP API] definitions of [SRS].
//!
//! [SRS]: https://ossrs.io/
//! [1]: https://ossrs.io/lts/en-us/docs/v5/doc/http-api
#![allow(unused_imports)]

mod client;
mod common;
mod error;
mod feature;
mod meminfos;
mod response;
mod rusages;
mod self_proc_stats;
mod stream;
mod summary;
mod system_proc_stats;
mod vhost;

pub use client::Client;
pub use common::{Hls, Kbps, Publish};
pub use error::SrsClientError;
pub use response::{SrsClientResp, SrsClientRespData};
pub use stream::{Audio, Stream, Video};
pub use summary::{Summary, Tests, Urls};
pub use vhost::Vhost;

use reqwest::{Client as ReqwestClient, Response as ReqwestResponse};
use url::Url;

/// Client for performing requests to [HTTP API][1] of spawned [SRS].
///
/// [SRS]: https://ossrs.io/
/// [1]: https://ossrs.io/lts/en-us/docs/v5/doc/http-api
#[derive(Clone, Debug)]
pub struct SrsClient {
    http_client: ReqwestClient,
    base_url: Url,
}

impl SrsClient {
    /// Build [`SrsClient`] for future call to [HTTP API][1] API of spawned [SRS]. .
    ///
    /// # Errors
    ///
    /// If incorrect `base_url` passed
    ///
    /// [SRS]: https://ossrs.io/
    /// [1]: https://ossrs.io/lts/en-us/docs/v5/doc/http-api
    pub fn build<S: Into<String>>(base_url: S) -> Result<Self, SrsClientError> {
        let base_url = Url::parse(&base_url.into())
            .and_then(|url| url.join("/api/v1/"))
            .map_err(SrsClientError::IncorrectBaseUrl)?;
        tracing::debug!("base_url: {base_url}");
        Ok(Self {
            http_client: ReqwestClient::new(),
            base_url,
        })
    }

    async fn get(&self, url: &str) -> Result<ReqwestResponse, SrsClientError> {
        self.http_client
            .get(
                self.base_url
                    .join(url)
                    .map_err(SrsClientError::IncorrectApiUrl)?,
            )
            .send()
            .await
            .map_err(SrsClientError::RequestFailed)
    }

    async fn delete(&self, url: &str) -> Result<ReqwestResponse, SrsClientError> {
        self.http_client
            .delete(
                self.base_url
                    .join(url)
                    .map_err(SrsClientError::IncorrectApiUrl)?,
            )
            .send()
            .await
            .map_err(SrsClientError::RequestFailed)
    }

    async fn process_resp(&self, resp: ReqwestResponse) -> Result<SrsClientResp, SrsClientError> {
        if !resp.status().is_success() {
            return Err(SrsClientError::BadStatus(resp.status()));
        }
        // tracing::debug!(url = resp.url().to_string(), "processing request");
        tracing::debug!("processing request to: {}", resp.url());
        let resp = resp
            .json::<SrsClientResp>()
            .await
            .map_err(SrsClientError::DeserializeError)?;
        Ok(resp)
    }

    /// [Kicks off][1] a client connected to [SRS] server by its `id`.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    ///
    /// [SRS]: https://ossrs.io/
    /// [1]: https://ossrs.io/lts/en-us/docs/v5/doc/http-api#kickoff-client
    pub async fn kickoff_client<T: Into<String>>(
        self,
        id: T,
    ) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.delete(&format!("clients/{}/", id.into())).await?;
        self.process_resp(resp).await
    }

    /// Retrieves the server version.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_version(self) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get("versions").await?;
        self.process_resp(resp).await
    }

    /// Retrieves the server summary.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_summaries(self) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get("summaries").await?;
        self.process_resp(resp).await
    }

    /// Retrieves the server summary as typed data.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_summary(self) -> Result<Option<Summary>, SrsClientError> {
        let response = self.get_summaries().await?;
        match response.data {
            SrsClientRespData::Summary(summary) => Ok(Some(summary)),
            _ => Err(SrsClientError::UnexpectedResponse("summary")),
        }
    }

    /// Retrieves the HTTP request debug API description.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_requests(self) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get("requests").await?;
        self.process_resp(resp).await
    }

    /// Retrieves the HTTP request debug API description as typed summary data.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_requests_summary(self) -> Result<Option<Summary>, SrsClientError> {
        let response = self.get_requests().await?;
        match response.data {
            SrsClientRespData::Summary(summary) => Ok(Some(summary)),
            _ => Err(SrsClientError::UnexpectedResponse("requests summary")),
        }
    }

    /// Retrieves the SRS config API description.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_configs(self) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get("configs").await?;
        self.process_resp(resp).await
    }

    /// Retrieves the SRS config API description as typed summary data.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_configs_summary(self) -> Result<Option<Summary>, SrsClientError> {
        let response = self.get_configs().await?;
        match response.data {
            SrsClientRespData::Summary(summary) => Ok(Some(summary)),
            _ => Err(SrsClientError::UnexpectedResponse("configs summary")),
        }
    }

    /// Manages all vhosts or a specified vhost.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_vhosts(self) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get("vhosts").await?;
        self.process_resp(resp).await
    }

    /// Manages a specified vhost.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_vhost<T: Into<String>>(self, id: T) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get(&format!("vhosts/{}", id.into())).await?;
        self.process_resp(resp).await
    }

    /// Retrieves all vhosts as a typed list.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_vhost_list(self) -> Result<Vec<Vhost>, SrsClientError> {
        let response = self.get_vhosts().await?;
        match response.data {
            SrsClientRespData::Vhosts { vhosts } => Ok(vhosts),
            _ => Err(SrsClientError::UnexpectedResponse("vhosts")),
        }
    }

    /// Retrieves a specified vhost as a typed item.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_vhost_item<T: Into<String>>(
        self,
        id: T,
    ) -> Result<Option<Vhost>, SrsClientError> {
        let response = self.get_vhost(id).await?;
        match response.data {
            SrsClientRespData::Vhost { vhost } => Ok(Some(vhost)),
            _ => Err(SrsClientError::UnexpectedResponse("vhost")),
        }
    }

    /// Manages all streams or a specified stream.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_streams(self) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get("streams").await?;
        self.process_resp(resp).await
    }

    /// Manages all streams using SRS pagination.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_streams_page(
        self,
        start: i64,
        count: i64,
    ) -> Result<SrsClientResp, SrsClientError> {
        let resp = self
            .get(&format!("streams?start={start}&count={count}"))
            .await?;
        self.process_resp(resp).await
    }

    /// Manages a specified stream.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_stream<T: Into<String>>(self, id: T) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get(&format!("streams/{}", id.into())).await?;
        self.process_resp(resp).await
    }

    /// Retrieves all streams as a typed list.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_stream_list(self) -> Result<Vec<Stream>, SrsClientError> {
        let response = self.get_streams().await?;
        match response.data {
            SrsClientRespData::Streams { streams } => Ok(streams),
            _ => Err(SrsClientError::UnexpectedResponse("streams")),
        }
    }

    /// Retrieves a paginated stream response as a typed list.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_stream_page_list(
        self,
        start: i64,
        count: i64,
    ) -> Result<Vec<Stream>, SrsClientError> {
        let response = self.get_streams_page(start, count).await?;
        match response.data {
            SrsClientRespData::Streams { streams } => Ok(streams),
            _ => Err(SrsClientError::UnexpectedResponse("streams page")),
        }
    }

    /// Retrieves a specified stream as a typed item.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_stream_item<T: Into<String>>(
        self,
        id: T,
    ) -> Result<Option<Stream>, SrsClientError> {
        let response = self.get_stream(id).await?;
        match response.data {
            SrsClientRespData::Stream { stream } => Ok(Some(stream)),
            _ => Err(SrsClientError::UnexpectedResponse("stream")),
        }
    }

    /// Manages all clients or a specified client, default query top 10 clients.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_clients(self) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get("clients").await?;
        self.process_resp(resp).await
    }

    /// Manages all clients using SRS pagination.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_clients_page(
        self,
        start: i64,
        count: i64,
    ) -> Result<SrsClientResp, SrsClientError> {
        let resp = self
            .get(&format!("clients?start={start}&count={count}"))
            .await?;
        self.process_resp(resp).await
    }

    /// Manages a specified client.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_client<T: Into<String>>(self, id: T) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get(&format!("clients/{}", id.into())).await?;
        self.process_resp(resp).await
    }

    /// Retrieves all clients as a typed list.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_client_list(self) -> Result<Vec<Client>, SrsClientError> {
        let response = self.get_clients().await?;
        match response.data {
            SrsClientRespData::Clients { clients } => Ok(clients),
            _ => Err(SrsClientError::UnexpectedResponse("clients")),
        }
    }

    /// Retrieves a paginated client response as a typed list.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_client_page_list(
        self,
        start: i64,
        count: i64,
    ) -> Result<Vec<Client>, SrsClientError> {
        let response = self.get_clients_page(start, count).await?;
        match response.data {
            SrsClientRespData::Clients { clients } => Ok(clients),
            _ => Err(SrsClientError::UnexpectedResponse("clients page")),
        }
    }

    /// Retrieves a specified client as a typed item.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_client_item<T: Into<String>>(
        self,
        id: T,
    ) -> Result<Option<Client>, SrsClientError> {
        let response = self.get_client(id).await?;
        match response.data {
            SrsClientRespData::Client { client } => Ok(Some(client)),
            _ => Err(SrsClientError::UnexpectedResponse("client")),
        }
    }

    /// Retrieves the supported features of SRS.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_features(self) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get("features").await?;
        self.process_resp(resp).await
    }

    /// Retrieves the rusage of SRS.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_rusages(self) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get("rusages").await?;
        self.process_resp(resp).await
    }

    /// Retrieves the self process stats.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_self_proc_stats(self) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get("self_proc_stats").await?;
        self.process_resp(resp).await
    }

    /// Retrieves the system process stats.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_system_proc_stats(self) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get("system_proc_stats").await?;
        self.process_resp(resp).await
    }

    /// Retrieves the meminfo of system.
    ///
    /// # Errors
    ///
    /// If API request cannot be performed, or fails. See [`SrsClientError`](enum@SrsClientError)
    /// for details.
    pub async fn get_meminfos(self) -> Result<SrsClientResp, SrsClientError> {
        let resp = self.get("meminfos").await?;
        self.process_resp(resp).await
    }
}