ds_common_aws_rs_lib/sqs/mod.rs
1//! AWS SQS module.
2//!
3//! This module contains functions for interacting with AWS SQS.
4
5pub mod error;
6
7use aws_sdk_sqs::Client;
8
9use crate::error::{Error, Result};
10use crate::sqs::error::{map_sqs_err, SqsError};
11
12/// SQS service for interacting with AWS SQS
13///
14/// This service provides a high-level interface for SQS operations
15/// with dependency injection for better performance and testability.
16pub struct SqsService {
17 client: Client,
18}
19
20impl SqsService {
21 /// Create a new SQS service with a custom client
22 ///
23 /// # Arguments
24 ///
25 /// * `client` - The SQS client to use
26 ///
27 /// # Returns
28 ///
29 /// Returns a new SqsService instance with the provided client.
30 pub fn with_client(client: Client) -> Self {
31 Self { client }
32 }
33
34 /// Create a new SQS service with a shared configuration
35 ///
36 /// # Arguments
37 ///
38 /// * `config` - The AWS SDK configuration to use
39 ///
40 /// # Returns
41 ///
42 /// Returns a new SqsService instance with the provided configuration.
43 pub fn with_config(config: &aws_config::SdkConfig) -> Self {
44 Self {
45 client: Client::new(config),
46 }
47 }
48
49 /// Get an SQS queue URL
50 ///
51 /// # Arguments
52 ///
53 /// * `name` - The name of the queue to get
54 ///
55 /// # Returns
56 ///
57 /// Returns the queue URL as a String
58 ///
59 /// # Errors
60 ///
61 /// * [`SqsError::Service`] - If the SQS client fails to create.
62 /// * [`SqsError::Timeout`] - If the request times out.
63 /// * [`SqsError::Transport`] - If the request fails to dispatch.
64 /// * [`SqsError::Build`] - If the request fails to build.
65 /// * [`SqsError::InvalidResponse`] - If the queue URL is not found.
66 pub async fn get_queue(&self, name: &str) -> Result<String> {
67 let response = self
68 .client
69 .get_queue_url()
70 .queue_name(name)
71 .send()
72 .await
73 .map_err(map_sqs_err)?;
74
75 match response.queue_url() {
76 Some(url) => Ok(url.to_string()),
77 None => Err(Error::from(SqsError::InvalidResponse(name.to_string()))),
78 }
79 }
80
81 /// Send an SQS message
82 ///
83 /// # Arguments
84 ///
85 /// * `queue_url` - The URL of the queue to send
86 /// * `body` - The message to send
87 /// * `delay_seconds` - The delay in seconds before the message is sent
88 ///
89 /// # Returns
90 ///
91 /// Returns the message ID as a String
92 ///
93 /// # Errors
94 ///
95 /// * [`SqsError::Service`] - If the SQS client fails to create.
96 /// * [`SqsError::Timeout`] - If the request times out.
97 /// * [`SqsError::Transport`] - If the request fails to dispatch.
98 /// * [`SqsError::Build`] - If the request fails to build.
99 /// * [`SqsError::InvalidResponse`] - If the message ID is not found.
100 pub async fn send_message(&self, queue_url: &str, body: &str, delay_seconds: Option<i32>) -> Result<String> {
101 let response = self
102 .client
103 .send_message()
104 .queue_url(queue_url)
105 .message_body(body)
106 .delay_seconds(delay_seconds.unwrap_or(0))
107 .send()
108 .await
109 .map_err(map_sqs_err)?;
110
111 match response.message_id() {
112 Some(id) => Ok(id.to_string()),
113 None => Err(Error::from(SqsError::InvalidResponse(queue_url.to_string()))),
114 }
115 }
116}