diff options
author | Paweł Dybiec <pawel@dybiec.info> | 2022-12-24 00:22:21 +0000 |
---|---|---|
committer | Paweł Dybiec <pawel@dybiec.info> | 2022-12-24 00:22:21 +0000 |
commit | 86f5bb9301a169d7b8d22c3165b05db4727415e5 (patch) | |
tree | e61027adb19e2fc443b434e47fe1f7413cd029c7 /src |
Initial commit
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 17 | ||||
-rw-r--r-- | src/mattermost.rs | 63 |
2 files changed, 80 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d4f30c4 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,17 @@ +mod mattermost; + +use tokio::{self}; +use tracing::{debug, info}; + +#[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")?; + let password = std::env::var("USER_PASSWORD")?; + 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?; + Ok(()) +} diff --git a/src/mattermost.rs b/src/mattermost.rs new file mode 100644 index 0000000..7742e3e --- /dev/null +++ b/src/mattermost.rs @@ -0,0 +1,63 @@ +use anyhow::anyhow; +use async_tungstenite::{tokio::connect_async, tungstenite::Message}; +use futures_util::{SinkExt, StreamExt}; +use serde_json::json; +use tracing::debug; +pub struct AuthData { + pub(crate) login: String, + pub(crate) password: String, +} +pub struct Client { + pub(crate) auth: AuthData, + pub(crate) url: String, + bearer_token: Option<String>, + client: reqwest::Client, +} +impl Client { + pub(crate) fn new(auth: AuthData, url: &str) -> Self { + Self { + auth, + url: url.to_owned(), + bearer_token: None, + client: reqwest::Client::new(), + } + } + pub(crate) async fn update_bearer_token(&mut self) -> Result<(), anyhow::Error> { + debug!("Fetching bearer token from api"); + let response = self + .client + .post(format!("{}/api/v4/users/login", self.url)) + .json(&json!({ + "login_id":self.auth.login, + "password":self.auth.password, + })) + .send() + .await?; + let token = response + .headers() + .get("token") + .ok_or(anyhow!("Haven't received bearer token in headers"))? + .to_str()?; + self.bearer_token = Some(token.to_owned()); + debug!("Successfully updated bearer token"); + Ok(()) + } + pub(crate) async fn handle_websocket_stream(&self) -> 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 + .bearer_token + .clone() + .ok_or(anyhow!("Missing bearer token"))?; + ws_stream + .send(Message::Text( + json!({"seq":1,"action":"authentication_challenge","data":{"token":token}}) + .to_string(), + )) + .await?; + while let Some(message) = ws_stream.next().await { + debug!("{:?}", message); + } + Ok(()) + } +} |