Skip to main content

ossify/ops/bucket/live_channel/
put_live_channel.rs

1//! PutLiveChannel: create a LiveChannel under a bucket.
2//!
3//! Official document: <https://www.alibabacloud.com/help/en/oss/developer-reference/putlivechannel>
4
5use std::future::Future;
6
7use http::Method;
8use serde::{Deserialize, Serialize};
9
10use crate::body::XMLBody;
11use crate::error::Result;
12use crate::ops::common::{LiveChannelConfiguration, LiveChannelUrls};
13use crate::response::BodyResponseProcessor;
14use crate::ser::OnlyKeyField;
15use crate::{Client, Ops, Prepared, Request};
16
17/// PutLiveChannel query parameters: `?live`.
18#[derive(Debug, Clone, Default, Serialize)]
19pub struct PutLiveChannelParams {
20    pub(crate) live: OnlyKeyField,
21}
22
23/// PutLiveChannel response body `<CreateLiveChannelResult>`.
24#[derive(Debug, Clone, Deserialize)]
25#[serde(rename = "CreateLiveChannelResult", rename_all = "PascalCase")]
26pub struct PutLiveChannelResponse {
27    pub publish_urls: LiveChannelUrls,
28    pub play_urls: LiveChannelUrls,
29}
30
31/// PutLiveChannel operation.
32pub struct PutLiveChannel {
33    pub channel_name: String,
34    pub params: PutLiveChannelParams,
35    pub body: LiveChannelConfiguration,
36}
37
38impl Ops for PutLiveChannel {
39    type Response = BodyResponseProcessor<PutLiveChannelResponse>;
40    type Body = XMLBody<LiveChannelConfiguration>;
41    type Query = PutLiveChannelParams;
42
43    fn prepare(self) -> Result<Prepared<PutLiveChannelParams, LiveChannelConfiguration>> {
44        Ok(Prepared {
45            method: Method::PUT,
46            key: Some(self.channel_name),
47            query: Some(self.params),
48            body: Some(self.body),
49            ..Default::default()
50        })
51    }
52}
53
54/// Trait for PutLiveChannel operations.
55pub trait PutLiveChannelOperations {
56    fn put_live_channel(
57        &self,
58        channel_name: impl Into<String>,
59        configuration: LiveChannelConfiguration,
60    ) -> impl Future<Output = Result<PutLiveChannelResponse>>;
61}
62
63impl PutLiveChannelOperations for Client {
64    async fn put_live_channel(
65        &self,
66        channel_name: impl Into<String>,
67        configuration: LiveChannelConfiguration,
68    ) -> Result<PutLiveChannelResponse> {
69        let ops = PutLiveChannel {
70            channel_name: channel_name.into(),
71            params: PutLiveChannelParams::default(),
72            body: configuration,
73        };
74        self.request(ops).await
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81    use crate::ops::common::LiveChannelTarget;
82
83    #[test]
84    fn test_serialize_params() {
85        assert_eq!(crate::ser::to_string(&PutLiveChannelParams::default()).unwrap(), "live");
86    }
87
88    #[test]
89    fn test_deserialize_response() {
90        let xml = r#"<CreateLiveChannelResult>
91  <PublishUrls><Url>rtmp://x</Url></PublishUrls>
92  <PlayUrls><Url>http://x.m3u8</Url></PlayUrls>
93</CreateLiveChannelResult>"#;
94        let resp: PutLiveChannelResponse = quick_xml::de::from_str(xml).unwrap();
95        assert_eq!(resp.publish_urls.urls[0], "rtmp://x");
96        assert_eq!(resp.play_urls.urls[0], "http://x.m3u8");
97    }
98
99    #[test]
100    fn test_configuration_round_trip() {
101        let cfg = LiveChannelConfiguration::new(LiveChannelTarget::hls());
102        let xml = quick_xml::se::to_string(&cfg).unwrap();
103        assert!(xml.contains("<Type>HLS</Type>"));
104    }
105}