feat: refactoring complete?

This commit is contained in:
Per Stark
2024-11-21 21:23:49 +01:00
parent cd7901eefe
commit 5f64fbd3fb
27 changed files with 428 additions and 338 deletions
+23 -18
View File
@@ -1,11 +1,9 @@
use lapin::{
options::*, publisher_confirm::Confirmation, BasicProperties,
};
use lapin::{options::*, publisher_confirm::Confirmation, BasicProperties};
use crate::models::ingress_object::IngressObject;
use crate::ingress::types::ingress_object::IngressObject;
use super::{RabbitMQCommon, RabbitMQCommonTrait, RabbitMQConfig, RabbitMQError};
use tracing::{info, error};
use tracing::{error, info};
/// Struct to publish messages to RabbitMQ.
pub struct RabbitMQProducer {
@@ -27,7 +25,7 @@ impl RabbitMQProducer {
let common = RabbitMQCommon::new(config).await?;
common.declare_exchange(config, false).await?;
Ok(Self {
Ok(Self {
common,
exchange_name: config.exchange.clone(),
routing_key: config.routing_key.clone(),
@@ -39,19 +37,23 @@ impl RabbitMQProducer {
/// # Arguments
/// * `self` - Reference to self
/// * `ingress_object` - A initialized IngressObject
///
///
/// # Returns
/// * `Result<Confirmation, RabbitMQError>` - Confirmation of sent message or error
pub async fn publish(&self, ingress_object: &IngressObject) -> Result<Confirmation, RabbitMQError> {
pub async fn publish(
&self,
ingress_object: &IngressObject,
) -> Result<Confirmation, RabbitMQError> {
// Serialize IngressObject to JSON
let payload = serde_json::to_vec(ingress_object)
.map_err(|e| {
error!("Serialization Error: {}", e);
RabbitMQError::PublishError(format!("Serialization Error: {}", e))
})?;
let payload = serde_json::to_vec(ingress_object).map_err(|e| {
error!("Serialization Error: {}", e);
RabbitMQError::PublishError(format!("Serialization Error: {}", e))
})?;
// Publish the serialized payload to RabbitMQ
let confirmation = self.common.channel
let confirmation = self
.common
.channel
.basic_publish(
&self.exchange_name,
&self.routing_key,
@@ -69,9 +71,12 @@ impl RabbitMQProducer {
error!("Publish Confirmation Error: {}", e);
RabbitMQError::PublishError(format!("Publish Confirmation Error: {}", e))
})?;
info!("Published IngressObject to exchange '{}' with routing key '{}'", self.exchange_name, self.routing_key);
info!(
"Published IngressObject to exchange '{}' with routing key '{}'",
self.exchange_name, self.routing_key
);
Ok(confirmation)
}
}