Skip to content

Commit

Permalink
feat: basic websocket client (plus sidetracked into router setup, was…
Browse files Browse the repository at this point in the history
…ted hours and then found an easier solution lmao)
  • Loading branch information
112batuhan committed Dec 21, 2024
1 parent e67852e commit f22e4a6
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 109 deletions.
130 changes: 110 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ tower-http = { version = "0.6.1", features = [
] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
webhook = { version = "2.1.2", features = ["models"] }

[patch.crates-io]
serde = { git = "https://github.com/frederik-uni/serde" }
Expand Down
32 changes: 32 additions & 0 deletions src/discord_webhook.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use http::StatusCode;
use webhook::models::Message;

use crate::error::AppError;

pub struct WebhookClient {
client: reqwest::Client,
url: String,
}

impl WebhookClient {
pub fn new(url: &str) -> WebhookClient {
WebhookClient {
client: reqwest::Client::new(),
url: url.to_owned(),
}
}

/// Basically a simple recreation of webhook-rs client send implementation with reqwest
pub async fn send(&self, message: &Message) -> Result<(), AppError> {
let res = self.client.post(&self.url).json(message).send().await?;
if res.status() == StatusCode::NO_CONTENT {
Ok(())
} else {
let err_msg = match res.text().await {
Ok(msg) => msg,
Err(err) => format!("Webhook reqwest client error: {}", err),
};
Err(AppError::Webhook(err_msg))
}
}
}
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ pub enum AppError {

#[error("Parse int: {0}")]
ParseInt(#[from] ParseIntError),

#[error("Discord webhook error:{0}")]
Webhook(String),
}

#[derive(Serialize)]
Expand All @@ -97,6 +100,7 @@ impl IntoResponse for AppError {
| AppError::ActivityStreamClosed
| AppError::SurrealDbSerialization(_)
| AppError::StdIO(_)
| AppError::Webhook(_)
| AppError::ActivityPreferencesQuery
| AppError::SephomoreError(_) => StatusCode::INTERNAL_SERVER_ERROR,
AppError::MissingTokenCookie
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use osu_api::request::Requester;
pub mod custom_cache;
pub mod daily_update;
pub mod database;
pub mod discord_webhook;
pub mod documentation;
pub mod error;
pub mod handlers;
Expand Down
Loading

0 comments on commit f22e4a6

Please sign in to comment.