DEVELOPMENT ENVIRONMENT

~liljamo/canwa

ref: 45439cc45439d0050fb8dd130f48993bba82574f canwa/src/service/gotify.rs -rw-r--r-- 1.2 KiB
45439cc4Jonni Liljamo feat: initial version 27 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * Copyright (C) 2025 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under GPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */

use async_trait::async_trait;
use serde::{Deserialize, Serialize};

use crate::config::ServiceConfig;

use super::Service;

#[derive(Clone, Deserialize, Serialize)]
pub struct GotifyConfig {
    pub instance: String,
    pub token: String,
}

#[typetag::serde(name = "gotify")]
impl ServiceConfig for GotifyConfig {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

pub struct GotifyService {
    client: reqwest::Client,
    config: GotifyConfig,
}

impl GotifyService {
    pub fn new(client: reqwest::Client, config: GotifyConfig) -> Self {
        Self { client, config }
    }
}

#[async_trait]
impl Service for GotifyService {
    async fn send(&self, title: &str, message: &str) -> Result<(), Box<dyn std::error::Error>> {
        let _ = self
            .client
            .post(format!("{}/message", self.config.instance))
            .query(&[("token", &self.config.token)])
            .form(&[("title", title), ("message", message)])
            .send()
            .await?;
        Ok(())
    }
}