Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 09f1db452b |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,2 @@
|
||||
/target
|
||||
target
|
||||
.env
|
||||
1741
discord_client/Cargo.lock
generated
Normal file
1741
discord_client/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
discord_client/Cargo.toml
Normal file
12
discord_client/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "discord_client"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
reqwest = { version = "0.12.22", features = ["blocking", "json"] }
|
||||
serde = "1.0.219"
|
||||
serde_json = "1.0.140"
|
||||
|
||||
[dev-dependencies]
|
||||
mockito = "1.7.0"
|
||||
59
discord_client/src/lib.rs
Normal file
59
discord_client/src/lib.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
pub struct DiscordNotifier{
|
||||
webhook_url: String,
|
||||
username: String,
|
||||
avatar_url: String,
|
||||
}
|
||||
|
||||
|
||||
impl DiscordNotifier {
|
||||
pub fn new(webhook_url: String, username: String, avatar_url: String) -> DiscordNotifier {
|
||||
DiscordNotifier {
|
||||
webhook_url,
|
||||
username,
|
||||
avatar_url,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_notification(&self, message: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let payload = serde_json::json!({
|
||||
"content": message,
|
||||
"username": self.username,
|
||||
"avatar_url": self.avatar_url
|
||||
});
|
||||
|
||||
client.post(&self.webhook_url)
|
||||
.json(&payload)
|
||||
.send()
|
||||
.map(|_| ())
|
||||
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_send_notification() {
|
||||
use serde_json::json;
|
||||
|
||||
const USERNAME : &str = "TEST_USER";
|
||||
const AVATAR_URL : &str = "https://example.com/avatar.png";
|
||||
const MESSAGE : &str = "Test message";
|
||||
|
||||
let mut server = mockito::Server::new();
|
||||
|
||||
let mock = server.mock("POST", "/")
|
||||
.with_status(204)
|
||||
.match_header("content-type", "application/json")
|
||||
.match_body(mockito::Matcher::Json(json!({
|
||||
"content": MESSAGE,
|
||||
"username": USERNAME,
|
||||
"avatar_url": AVATAR_URL
|
||||
})))
|
||||
.create();
|
||||
|
||||
let webhook_url = server.url();
|
||||
|
||||
let notifier = DiscordNotifier::new(webhook_url.clone(), USERNAME.to_string(), AVATAR_URL.to_string());
|
||||
let result = notifier.send_notification(MESSAGE);
|
||||
assert!(result.is_ok(), "{}", format!("Failed to send notification: {:?}", result.err()));
|
||||
mock.assert();
|
||||
}
|
||||
1
src/app.rs
Normal file
1
src/app.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod node_notifier;
|
||||
2
src/app/node_notifier.rs
Normal file
2
src/app/node_notifier.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod ports;
|
||||
pub mod services;
|
||||
0
src/app/node_notifier/ports.rs
Normal file
0
src/app/node_notifier/ports.rs
Normal file
1
src/app/node_notifier/services.rs
Normal file
1
src/app/node_notifier/services.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod service;
|
||||
24
src/app/node_notifier/services/service.rs
Normal file
24
src/app/node_notifier/services/service.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
pub struct Service {}
|
||||
|
||||
impl Service {
|
||||
pub fn new() -> Self {
|
||||
Service {}
|
||||
}
|
||||
|
||||
pub fn run(&self) -> Result<(), String> {
|
||||
println!("Service is running...");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_service_run() {
|
||||
let service = Service::new();
|
||||
assert!(service.run().is_ok(), "Service should run without errors");
|
||||
}
|
||||
}
|
||||
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod app;
|
||||
0
src/outside.rs
Normal file
0
src/outside.rs
Normal file
0
src/outside/driven.rs
Normal file
0
src/outside/driven.rs
Normal file
0
src/outside/driven/for_collecting_data.rs
Normal file
0
src/outside/driven/for_collecting_data.rs
Normal file
0
src/outside/driven/for_notifying.rs
Normal file
0
src/outside/driven/for_notifying.rs
Normal file
0
src/outside/driven/for_notifying/discord_client.rs
Normal file
0
src/outside/driven/for_notifying/discord_client.rs
Normal file
Reference in New Issue
Block a user