use crate::client::HttpClient;
use crate::error::CoreError;
use crate::message::GenericMessage;
use crate::retry::RetryPolicy;
use crate::sender::WebhookSender;
use crate::service::ServiceInfo;
use async_trait::async_trait;
use std::sync::Arc;
pub type BoxedWebhookSender =
Box<dyn WebhookSender<Message = GenericMessage, Error = CoreError> + Send + Sync>;
#[derive(Clone)]
pub struct WebhookService {
sender: Arc<BoxedWebhookSender>,
name: String,
client: HttpClient,
url: String,
default_username: Option<String>,
default_avatar_url: Option<String>,
service_info: Arc<ServiceInfo>,
retry_policy: RetryPolicy,
}
impl WebhookService {
#[must_use]
pub fn new(
name: String,
url: String,
client: HttpClient,
service_info: Arc<ServiceInfo>,
sender: BoxedWebhookSender,
) -> Self {
Self {
name,
client,
url,
default_username: None,
default_avatar_url: None,
service_info,
sender: Arc::new(sender),
retry_policy: RetryPolicy::default(),
}
}
#[must_use]
pub fn with_sender(sender: BoxedWebhookSender) -> Self {
Self {
sender: Arc::new(sender),
name: String::new(),
client: HttpClient::new(),
url: String::new(),
default_username: None,
default_avatar_url: None,
service_info: Arc::new(ServiceInfo {
name: "unknown".to_string(),
version: "0.1.0".to_string(),
description: "Unknown service".to_string(),
webhook_url_format: String::new(),
supports_embeds: false,
max_content_length: 0,
features: vec![],
rate_limit_retry_after: None,
documentation_url: None,
icon_url: None,
platforms: vec![],
}),
retry_policy: RetryPolicy::default(),
}
}
#[must_use]
pub fn with_default_username(mut self, username: impl Into<String>) -> Self {
self.default_username = Some(username.into());
self
}
#[must_use]
pub fn with_default_avatar(mut self, avatar_url: impl Into<String>) -> Self {
self.default_avatar_url = Some(avatar_url.into());
self
}
#[must_use]
pub const fn with_retry_policy(mut self, policy: RetryPolicy) -> Self {
self.retry_policy = policy;
self
}
#[must_use]
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.name = name.into();
self
}
#[must_use]
pub fn with_service_info(mut self, info: Arc<ServiceInfo>) -> Self {
self.service_info = info;
self
}
pub async fn send_generic(&self, message: &GenericMessage) -> Result<(), CoreError> {
let mut enriched_message = message.clone();
if enriched_message.username.is_none() {
enriched_message.username = self.default_username.clone();
}
if enriched_message.avatar_url.is_none() {
enriched_message.avatar_url = self.default_avatar_url.clone();
}
self.sender.send(&enriched_message).await
}
pub async fn send_generic_with_retry(&self, message: &GenericMessage) -> Result<(), CoreError> {
let max_attempts = self.retry_policy.max_attempts.max(1);
let mut last_error = None;
for attempt in 0..max_attempts {
match self.send_generic(message).await {
Ok(result) => return Ok(result),
Err(err) => {
let is_last = attempt + 1 >= max_attempts;
if !is_last {
let delay = self.retry_policy.calculate_delay(attempt);
tokio::time::sleep(delay).await;
}
last_error = Some(err);
}
}
}
Err(last_error
.unwrap_or_else(|| CoreError::RetryExhausted("All retry attempts failed".to_string())))
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub const fn info(&self) -> &Arc<ServiceInfo> {
&self.service_info
}
#[must_use]
pub const fn client(&self) -> &HttpClient {
&self.client
}
#[must_use]
pub fn url(&self) -> &str {
&self.url
}
}
#[async_trait]
impl WebhookSender for WebhookService {
type Message = GenericMessage;
type Error = CoreError;
async fn send(&self, message: &Self::Message) -> Result<(), Self::Error> {
self.send_generic(message).await
}
}