streamhub/notify/
http.rs

1use crate::notify::Notifier;
2use reqwest::Client;
3use async_trait::async_trait;
4use crate::define::{StreamHubEventMessage};
5
6macro_rules! serialize_event {
7    ($message:expr) => {{
8        let event_serialize_str = match serde_json::to_string(&$message) {
9            Ok(data) => {
10                log::info!("event data: {}", data);
11                data
12            }
13            Err(_) => String::from("empty body"),
14        };
15        event_serialize_str
16    }};
17}
18
19
20pub struct HttpNotifier {
21    request_client: Client,
22    on_publish_url: Option<String>,
23    on_unpublish_url: Option<String>,
24    on_play_url: Option<String>,
25    on_stop_url: Option<String>,
26}
27
28impl HttpNotifier {
29    pub fn new(
30        on_publish_url: Option<String>,
31        on_unpublish_url: Option<String>,
32        on_play_url: Option<String>,
33        on_stop_url: Option<String>,
34    ) -> Self {
35        Self {
36            request_client: reqwest::Client::new(),
37            on_publish_url,
38            on_unpublish_url,
39            on_play_url,
40            on_stop_url,
41        }
42    }
43}
44
45#[async_trait]
46impl Notifier for HttpNotifier {
47    async fn on_publish_notify(&self, event: &StreamHubEventMessage) {
48        if let Some(on_publish_url) = &self.on_publish_url {
49            match self
50                .request_client
51                .post(on_publish_url)
52                .body(serialize_event!(event))
53                .send()
54                .await
55            {
56                Err(err) => {
57                    log::error!("on_publish error: {}", err);
58                }
59                Ok(response) => {
60                    log::info!("on_publish success: {:?}", response);
61                }
62            }
63        }
64    }
65
66    async fn on_unpublish_notify(&self, event: &StreamHubEventMessage) {
67        if let Some(on_unpublish_url) = &self.on_unpublish_url {
68            match self
69                .request_client
70                .post(on_unpublish_url)
71                .body(serialize_event!(event))
72                .send()
73                .await
74            {
75                Err(err) => {
76                    log::error!("on_unpublish error: {}", err);
77                }
78                Ok(response) => {
79                    log::info!("on_unpublish success: {:?}", response);
80                }
81            }
82        }
83    }
84
85    async fn on_play_notify(&self, event: &StreamHubEventMessage) {
86        if let Some(on_play_url) = &self.on_play_url {
87            match self
88                .request_client
89                .post(on_play_url)
90                .body(serialize_event!(event))
91                .send()
92                .await
93            {
94                Err(err) => {
95                    log::error!("on_play error: {}", err);
96                }
97                Ok(response) => {
98                    log::info!("on_play success: {:?}", response);
99                }
100            }
101        }
102    }
103
104    async fn on_stop_notify(&self, event: &StreamHubEventMessage) {
105        if let Some(on_stop_url) = &self.on_stop_url {
106            match self
107                .request_client
108                .post(on_stop_url)
109                .body(serialize_event!(event))
110                .send()
111                .await
112            {
113                Err(err) => {
114                    log::error!("on_stop error: {}", err);
115                }
116                Ok(response) => {
117                    log::info!("on_stop success: {:?}", response);
118                }
119            }
120        }
121    }
122}