summary refs log tree commit diff
path: root/src/mattermost.rs
diff options
context:
space:
mode:
authorPaweł Dybiec <pawel@dybiec.info>2022-12-25 21:35:25 +0000
committerPaweł Dybiec <pawel@dybiec.info>2022-12-25 21:35:25 +0000
commit7957ff3d8fb2c1ce9091e89cd3de6ba2f559e431 (patch)
tree27bb4b1c987090ef6bdbc64226ef962bdcd0e64f /src/mattermost.rs
parentParse json response from server (diff)
Support for custom message handlers
Diffstat (limited to 'src/mattermost.rs')
-rw-r--r--src/mattermost.rs10
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}"),
                         }
                     }