xref: /xiu/library/streamhub/src/notify/mod.rs (revision 8e71d710)
1 use reqwest::Client;
2 pub struct Notifier {
3     request_client: Client,
4     on_publish_url: Option<String>,
5     on_unpublish_url: Option<String>,
6     on_play_url: Option<String>,
7     on_stop_url: Option<String>,
8 }
9 
10 impl Notifier {
new( on_publish_url: Option<String>, on_unpublish_url: Option<String>, on_play_url: Option<String>, on_stop_url: Option<String>, ) -> Self11     pub fn new(
12         on_publish_url: Option<String>,
13         on_unpublish_url: Option<String>,
14         on_play_url: Option<String>,
15         on_stop_url: Option<String>,
16     ) -> Self {
17         Self {
18             request_client: reqwest::Client::new(),
19             on_publish_url,
20             on_unpublish_url,
21             on_play_url,
22             on_stop_url,
23         }
24     }
on_publish_notify(&self, body: String)25     pub async fn on_publish_notify(&self, body: String) {
26         if let Some(on_publish_url) = &self.on_publish_url {
27             match self
28                 .request_client
29                 .post(on_publish_url)
30                 .body(body)
31                 .send()
32                 .await
33             {
34                 Err(err) => {
35                     log::error!("on_publish error: {}", err);
36                 }
37                 Ok(response) => {
38                     log::info!("on_publish success: {:?}", response);
39                 }
40             }
41         }
42     }
43 
on_unpublish_notify(&self, body: String)44     pub async fn on_unpublish_notify(&self, body: String) {
45         if let Some(on_unpublish_url) = &self.on_unpublish_url {
46             match self
47                 .request_client
48                 .post(on_unpublish_url)
49                 .body(body)
50                 .send()
51                 .await
52             {
53                 Err(err) => {
54                     log::error!("on_unpublish error: {}", err);
55                 }
56                 Ok(response) => {
57                     log::info!("on_unpublish success: {:?}", response);
58                 }
59             }
60         }
61     }
62 
on_play_notify(&self, body: String)63     pub async fn on_play_notify(&self, body: String) {
64         if let Some(on_play_url) = &self.on_play_url {
65             match self
66                 .request_client
67                 .post(on_play_url)
68                 .body(body)
69                 .send()
70                 .await
71             {
72                 Err(err) => {
73                     log::error!("on_play error: {}", err);
74                 }
75                 Ok(response) => {
76                     log::info!("on_play success: {:?}", response);
77                 }
78             }
79         }
80     }
81 
on_stop_notify(&self, body: String)82     pub async fn on_stop_notify(&self, body: String) {
83         if let Some(on_stop_url) = &self.on_stop_url {
84             match self
85                 .request_client
86                 .post(on_stop_url)
87                 .body(body)
88                 .send()
89                 .await
90             {
91                 Err(err) => {
92                     log::error!("on_stop error: {}", err);
93                 }
94                 Ok(response) => {
95                     log::info!("on_stop success: {:?}", response);
96                 }
97             }
98         }
99     }
100 }
101