From 7957ff3d8fb2c1ce9091e89cd3de6ba2f559e431 Mon Sep 17 00:00:00 2001 From: Paweł Dybiec Date: Sun, 25 Dec 2022 21:35:25 +0000 Subject: Support for custom message handlers --- src/mattermost.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/mattermost.rs') diff --git a/src/mattermost.rs b/src/mattermost.rs index a062887..9b811d1 100644 --- a/src/mattermost.rs +++ b/src/mattermost.rs @@ -13,6 +13,12 @@ pub struct Client { bearer_token: Option, client: reqwest::Client, } + +#[async_trait::async_trait] +pub trait Handler { + async fn handle(&self, message:serde_json::Value,client: &Client); +} + impl Client { pub(crate) fn new(auth: AuthData, url: &str) -> Self { Self { @@ -42,7 +48,7 @@ impl Client { debug!("Successfully updated bearer token"); Ok(()) } - pub(crate) async fn handle_websocket_stream(&self) -> Result<(), anyhow::Error> { + pub(crate) async fn handle_websocket_stream(&mut self, handler: T) -> Result<(), anyhow::Error> { let url = format!("{}/api/v4/websocket", self.url.replacen("http", "ws", 1)); let (mut ws_stream, _) = connect_async(url).await?; let token = self @@ -61,7 +67,7 @@ impl Client { Message::Text(text) => { let json: Result = serde_json::from_str(&text); match json { - Ok(json) => debug!("Got json {json}"), + Ok(json) => {debug!("Got json {json}");handler.handle(json,&self).await;}, Err(err) => warn!("Error while deserializing {err}"), } } -- cgit 1.4.1