feat: webhook init

This commit is contained in:
2024-05-16 21:55:55 +01:00
parent cf2950bf99
commit a85dbf88b9
9 changed files with 230 additions and 16 deletions

41
src/webhook.rs Normal file
View File

@ -0,0 +1,41 @@
use anyhow::Error;
use serde::{Deserialize, Serialize};
use crate::filesystem::FileSystemResult;
pub(crate) struct Webhook {
url: String,
}
#[derive(Serialize, Deserialize)]
struct WebhookRequest<T> {
pub action: String,
pub subject: Option<String>,
pub payload: T,
}
impl Webhook {
pub fn new(url: String) -> Self {
Self {
url
}
}
/// Ask webhook api if this file can be accepted
pub fn store_file(&self, pubkey: &Vec<u8>, fs: FileSystemResult) -> Result<bool, Error> {
let body: WebhookRequest<FileSystemResult> = WebhookRequest {
action: "store_file".to_string(),
subject: Some(hex::encode(pubkey)),
payload: fs,
};
let req = ureq::post(&self.url)
.set("accept", "application/json")
.send_json(body)?;
if req.status() == 200 {
Ok(true)
} else {
Ok(false)
}
}
}