|
1 |
| -use lambda_runtime::{tracing, Error, LambdaEvent}; |
2 | 1 | use aws_lambda_events::event::sqs::SqsEvent;
|
| 2 | +use lambda_runtime::{Error, LambdaEvent}; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | +use serde_json; |
3 | 5 |
|
4 |
| -/// This is the main body for the function. |
5 |
| -/// Write your code inside it. |
6 |
| -/// There are some code example in the following URLs: |
7 |
| -/// - https://github.com/awslabs/aws-lambda-rust-runtime/tree/main/examples |
8 |
| -/// - https://github.com/aws-samples/serverless-rust-demo/ |
9 |
| -pub(crate)async fn function_handler(event: LambdaEvent<SqsEvent>) -> Result<(), Error> { |
| 6 | +#[derive(Deserialize, Serialize, Default)] |
| 7 | +pub struct User { |
| 8 | + id: String, |
| 9 | + session_id: String, |
| 10 | + devide_id: Option<String>, |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Deserialize, Serialize, Default)] |
| 14 | +pub struct Event { |
| 15 | + event_id: String, |
| 16 | + user: User, |
| 17 | + emitter_code: u32, |
| 18 | + action: String, |
| 19 | +} |
| 20 | + |
| 21 | +pub(crate) async fn function_handler(lambda_event: LambdaEvent<SqsEvent>) -> Result<(), Error> { |
10 | 22 | // Extract some useful information from the request
|
11 |
| - let payload = event.payload; |
12 |
| - tracing::info!("Payload: {:?}", payload); |
| 23 | + let payload = lambda_event.payload; |
| 24 | + for message in payload.records { |
| 25 | + let event: Event = serde_json::from_str(&message.body.unwrap()).unwrap(); |
| 26 | + } |
13 | 27 |
|
14 | 28 | Ok(())
|
15 | 29 | }
|
16 | 30 |
|
17 | 31 | #[cfg(test)]
|
18 | 32 | mod tests {
|
19 | 33 | use super::*;
|
| 34 | + use aws_lambda_events::sqs::SqsMessage; |
20 | 35 | use lambda_runtime::{Context, LambdaEvent};
|
| 36 | + use std::collections::HashMap; |
| 37 | + |
| 38 | + fn build_message(body: &str) -> SqsMessage { |
| 39 | + SqsMessage { |
| 40 | + message_id: None, |
| 41 | + receipt_handle: None, |
| 42 | + aws_region: None, |
| 43 | + body: Some(body.to_string()), |
| 44 | + md5_of_body: None, |
| 45 | + md5_of_message_attributes: None, |
| 46 | + event_source: None, |
| 47 | + event_source_arn: None, |
| 48 | + attributes: HashMap::new(), |
| 49 | + message_attributes: HashMap::new(), |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + fn build_event(body: &str) -> SqsEvent { |
| 54 | + SqsEvent { |
| 55 | + records: vec![build_message(body)], |
| 56 | + } |
| 57 | + } |
21 | 58 |
|
22 | 59 | #[tokio::test]
|
23 | 60 | async fn test_event_handler() {
|
24 |
| - let event = LambdaEvent::new(SqsEvent::default(), Context::default()); |
| 61 | + let event = LambdaEvent::new( |
| 62 | + build_event(&serde_json::to_string(&Event::default()).unwrap()), |
| 63 | + Context::default(), |
| 64 | + ); |
25 | 65 | let response = function_handler(event).await.unwrap();
|
26 | 66 | assert_eq!((), response);
|
27 | 67 | }
|
|
0 commit comments