-
Notifications
You must be signed in to change notification settings - Fork 640
/
Copy pathsqs.rs
105 lines (90 loc) · 3.18 KB
/
sqs.rs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use anyhow::Context;
use async_trait::async_trait;
use aws_credential_types::Credentials;
use aws_sdk_sqs::config::{BehaviorVersion, Region};
use aws_sdk_sqs::operation::receive_message::ReceiveMessageOutput;
use mockall::automock;
/// The [SqsQueue] trait defines a basic interface for interacting with an
/// AWS SQS queue.
///
/// A [MockSqsQueue] struct is automatically generated by the [automock]
/// attribute. This struct can be used in unit tests to mock the behavior of
/// the [SqsQueue] trait.
///
/// The [SqsQueueImpl] struct is the actual implementation of the trait.
#[automock]
#[async_trait]
pub trait SqsQueue {
async fn receive_messages(&self, max_messages: i32) -> anyhow::Result<ReceiveMessageOutput>;
async fn delete_message(&self, receipt_handle: &str) -> anyhow::Result<()>;
}
/// The [SqsQueueImpl] struct is the actual implementation of the [SqsQueue]
/// trait, which interacts with the real AWS API servers.
#[derive(Debug, Clone)]
pub struct SqsQueueImpl {
client: aws_sdk_sqs::Client,
queue_url: String,
}
impl SqsQueueImpl {
pub fn new(queue_url: impl Into<String>, region: Region, credentials: Credentials) -> Self {
let config = aws_sdk_sqs::Config::builder()
.credentials_provider(credentials)
.region(region)
.behavior_version(BehaviorVersion::v2025_01_17())
.build();
let client = aws_sdk_sqs::Client::from_conf(config);
let queue_url = queue_url.into();
SqsQueueImpl { client, queue_url }
}
}
#[async_trait]
impl SqsQueue for SqsQueueImpl {
async fn receive_messages(&self, max_messages: i32) -> anyhow::Result<ReceiveMessageOutput> {
let response = self
.client
.receive_message()
.max_number_of_messages(max_messages)
.queue_url(&self.queue_url)
.send()
.await
.context("Failed to receive SQS queue message")?;
Ok(response)
}
async fn delete_message(&self, receipt_handle: &str) -> anyhow::Result<()> {
self.client
.delete_message()
.receipt_handle(receipt_handle)
.queue_url(&self.queue_url)
.send()
.await
.context("Failed to delete SQS queue message")?;
Ok(())
}
}
#[async_trait]
impl<T: SqsQueue + Send + Sync + ?Sized> SqsQueue for Box<T> {
async fn receive_messages(&self, max_messages: i32) -> anyhow::Result<ReceiveMessageOutput> {
(**self).receive_messages(max_messages).await
}
async fn delete_message(&self, receipt_handle: &str) -> anyhow::Result<()> {
(**self).delete_message(receipt_handle).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_constructor() {
let credentials = Credentials::new(
"ANOTREAL",
"notrealrnrELgWzOk3IfjzDKtFBhDby",
None,
None,
"test",
);
let queue_url = "https://sqs.us-west-1.amazonaws.com/359172468976/cdn-log-event-queue";
let region = Region::new("us-west-1");
// Check that `SqsQueueImpl::new()` does not panic.
let _queue = SqsQueueImpl::new(queue_url, region, credentials);
}
}