use crate::error::CoreError;
use crate::retry::RetryPolicy;
use crate::webhook_service::BoxedWebhookSender;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceInfo {
pub name: String,
pub version: String,
pub description: String,
pub webhook_url_format: String,
pub supports_embeds: bool,
pub max_content_length: usize,
pub features: Vec<String>,
pub rate_limit_retry_after: Option<Duration>,
pub documentation_url: Option<String>,
pub icon_url: Option<String>,
pub platforms: Vec<Platform>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Platform {
Discord,
Slack,
Teams,
Mattermost,
Generic,
Custom(String),
}
#[derive(Debug, Clone)]
pub struct ServiceConfig {
pub url: String,
pub timeout: Duration,
pub retry_policy: RetryPolicy,
pub custom_headers: FxHashMap<String, String>,
pub additional_config: FxHashMap<String, serde_json::Value>,
}
impl ServiceConfig {
#[must_use]
pub fn new(url: impl Into<String>) -> Self {
Self {
url: url.into(),
timeout: Duration::from_secs(30),
retry_policy: RetryPolicy::default(),
custom_headers: FxHashMap::default(),
additional_config: FxHashMap::default(),
}
}
#[must_use]
pub const fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
#[must_use]
pub const fn with_retry_policy(mut self, policy: RetryPolicy) -> Self {
self.retry_policy = policy;
self
}
#[must_use]
pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.custom_headers.insert(key.into(), value.into());
self
}
#[must_use]
pub fn with_config(
mut self,
key: impl Into<String>,
value: impl Serialize,
) -> Result<Self, CoreError> {
self.additional_config
.insert(key.into(), serde_json::to_value(value)?);
Ok(self)
}
}
pub trait ServiceBuilder: Send + Sync {
fn build(
&self,
config: ServiceConfig,
) -> Result<BoxedWebhookSender, Box<dyn std::error::Error + Send + Sync>>;
fn info(&self) -> Arc<ServiceInfo>;
#[must_use]
fn validate_url(&self, _url: &str) -> bool {
true
}
}