feat: swap ureq for reqwest

This commit is contained in:
2024-10-15 11:27:29 +01:00
parent 5453205a58
commit c86631423d
6 changed files with 241 additions and 92 deletions

View File

@ -1,10 +1,12 @@
use anyhow::Error;
use reqwest::{Client, ClientBuilder};
use serde::{Deserialize, Serialize};
use crate::filesystem::FileSystemResult;
pub struct Webhook {
url: String,
client: Client,
}
#[derive(Serialize, Deserialize)]
@ -16,19 +18,26 @@ struct WebhookRequest<T> {
impl Webhook {
pub fn new(url: String) -> Self {
Self { url }
Self {
url,
client: ClientBuilder::new().build().unwrap(),
}
}
/// Ask webhook api if this file can be accepted
pub fn store_file(&self, pubkey: &Vec<u8>, fs: FileSystemResult) -> Result<bool, Error> {
pub async 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)?;
let req = self
.client
.post(&self.url)
.header("accept", "application/json")
.json(&body)
.send()
.await?;
if req.status() == 200 {
Ok(true)