diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 13 | ||||
-rw-r--r-- | src/mattermost.rs | 10 |
2 files changed, 19 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index d4f30c4..e2a9028 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,17 @@ mod mattermost; use tokio::{self}; use tracing::{debug, info}; -#[tokio::main(worker_threads = 2)] +struct Vav{ +} +#[async_trait::async_trait] +impl mattermost::Handler for Vav{ + async fn handle(&self,message:serde_json::Value,client: & mattermost::Client) { + debug!("{message:?}"); + } +} + +#[tokio::main(worker_threads = 2)] async fn main() -> Result<(), Box<dyn std::error::Error>> { tracing_subscriber::fmt::init(); let login = std::env::var("USER_MAIL")?; @@ -12,6 +21,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { let auth = mattermost::AuthData { login, password }; let mut client = mattermost::Client::new(auth, "https://mattermost.continuum.ii.uni.wroc.pl"); client.update_bearer_token().await?; - client.handle_websocket_stream().await?; + client.handle_websocket_stream(Vav{}).await?; Ok(()) } 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}"), } } |