diff options
Diffstat (limited to 'src/mattermost.rs')
-rw-r--r-- | src/mattermost.rs | 10 |
1 files changed, 8 insertions, 2 deletions
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<String>, 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<T:Handler>(&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::Value, _> = 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}"), } } |