Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 666782d

Browse files
Merge pull request #4 from madeiramadeirabr/hotfix_sqs_examples
tratativas e correcoes de sqs
2 parents 410b997 + 65529dc commit 666782d

File tree

12 files changed

+135
-16
lines changed

12 files changed

+135
-16
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"sample": "json"
2+
"chavenfe": "32210206107255000134550010001712551245826554",
3+
"ocor": "MOTIVO DO CANCELAMENTO",
4+
"origem": "SAC/EAGLE",
5+
"pedido": "Z1223321"
36
}

examples/lambda_api/scripts/localstack/sqs/send-message.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ MESSAGE=$2
5656
if [ -z "$MESSAGE" ]
5757
then
5858
MESSAGE=$(cat ${current_file_path}sample.json)
59+
else
60+
if test -f $2 ; then
61+
MESSAGE=$(cat $2)
62+
fi
5963
fi
6064

6165
# cat ${current_file_path}sample.json
62-
# echo $MESSAGE
66+
#echo $MESSAGE
6367
echo "aws --endpoint-url=http://$HOST:4566 sqs send-message --queue-url $QUEUE --message-body '$MESSAGE'"
64-
aws --endpoint-url=http://$HOST:4566 sqs send-message --queue-url $QUEUE --message-body "'$MESSAGE'"
68+
aws --endpoint-url=http://$HOST:4566 sqs send-message --queue-url $QUEUE --message-body "$MESSAGE"

examples/lambda_sqs/README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,31 @@ Execute the follow command:
6969
./scripts/runenv.sh
7070
```
7171

72-
### Boot the lambda
73-
Execute the follow command:
74-
```
75-
./scripts/boot.sh
76-
```
72+
[//]: # (### Boot the lambda)
73+
74+
[//]: # (Execute the follow command:)
75+
76+
[//]: # (```)
77+
78+
[//]: # (./scripts/boot.sh)
79+
80+
[//]: # (```)
7781

7882
### Running the app
83+
84+
85+
[//]: # (```)
86+
87+
[//]: # (./scripts/localstack/lambda/invoke-sqs-function.sh lambda_sqs)
88+
89+
[//]: # (```)
7990
Execute the follow command:
8091
```
81-
./scripts/localstack/lambda/invoke-sqs-function.sh lambda_sqs
92+
./scripts/localstack/sqs/send-message.sh
93+
```
94+
Execute the follow command to send specific file:
95+
```
96+
./scripts/localstack/sqs/send-message.sh test-queue ./samples/ocorens/cancelamento.json
8297
```
8398

8499
## Samples

examples/lambda_sqs/lambda_app/services/v1/carrier_notifier_service.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import ast
23

34
from lambda_app import APP_NAME, APP_VERSION, APP_ARCH_VERSION, helper
45
from lambda_app.decorators import SQSEvent
@@ -70,16 +71,28 @@ def process(self, sqs_event):
7071
return result
7172

7273
def _read_event(self, record):
74+
self.logger.info('try to reading event form record: {}'.format(record))
75+
self.logger.info('Getting type of data: {}'.format(type(record)))
76+
self.logger.info('dump: {}'.format(json.dumps(record)))
7377
event_body = None
7478
try:
7579
if isinstance(record, dict):
76-
event_body = json.loads(record['body'])
80+
try:
81+
event_body = json.loads(record['body'])
82+
except Exception as err:
83+
self.logger.error(err)
84+
unescaped_str = ast.literal_eval(record['body'])
85+
event_body = json.loads(unescaped_str)
86+
elif isinstance(record, str):
87+
record = json.loads(record)
88+
event_body = json.loads(record.body)
7789
elif isinstance(record.body, str):
7890
event_body = json.loads(record.body)
7991
else:
8092
event_body = record.body
8193
except Exception as err:
8294
self.logger.error(err)
95+
self.logger.info('event_body: {}'.format(event_body))
8396
return event_body
8497

8598
def get_records_from_sqs_event(self, sqs_event):

examples/lambda_sqs/samples/ocorens/cancelamento.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"chavenfe": "32210206107255000134550010001712551245826554",
33
"ocor": "MOTIVO DO CANCELAMENTO",
44
"origem": "SAC/EAGLE",
5-
"pedido": "Z1223321"
5+
"pedido": "Z1223322"
66
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"sample": "json"
2+
"chavenfe": "32210206107255000134550010001712551245826554",
3+
"ocor": "MOTIVO DO CANCELAMENTO",
4+
"origem": "SAC/EAGLE",
5+
"pedido": "Z1223321"
36
}

examples/lambda_sqs/scripts/localstack/sqs/send-message.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ MESSAGE=$2
5656
if [ -z "$MESSAGE" ]
5757
then
5858
MESSAGE=$(cat ${current_file_path}sample.json)
59+
else
60+
if test -f $2 ; then
61+
MESSAGE=$(cat $2)
62+
fi
5963
fi
6064

6165
# cat ${current_file_path}sample.json
62-
# echo $MESSAGE
66+
#echo $MESSAGE
6367
echo "aws --endpoint-url=http://$HOST:4566 sqs send-message --queue-url $QUEUE --message-body '$MESSAGE'"
64-
aws --endpoint-url=http://$HOST:4566 sqs send-message --queue-url $QUEUE --message-body "'$MESSAGE'"
68+
aws --endpoint-url=http://$HOST:4566 sqs send-message --queue-url $QUEUE --message-body "$MESSAGE"

examples/lambda_sqs/tests/component/test_lambda_app/services/v1/test_carrier_notifier_service.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from tests.component.componenttestutils import BaseComponentTestCase
1313
from tests.component.helpers.events.aws.sqs_helper import SQSHelper
1414
from tests.unit.helpers.aws.sqs_helper import create_chalice_sqs_event, get_sqs_event_sample
15-
from tests.unit.helpers.events_helper import get_cancelamento_event
15+
from tests.unit.helpers.events_helper import get_cancelamento_event, get_cancelamento_error_event, \
16+
get_cancelamento_quote_error_event
1617
from tests.unit.testutils import get_function_name
1718

1819

@@ -29,6 +30,12 @@ def get_queue_events_samples():
2930

3031
return (sqs_event,),
3132

33+
def get_queue_events_error_samples():
34+
event = get_cancelamento_error_event()
35+
qevent = get_cancelamento_quote_error_event()
36+
37+
return (create_chalice_sqs_event(event),), (create_chalice_sqs_event(qevent),),
38+
3239

3340
class CarrierNotifierServiceTestCase(BaseComponentTestCase):
3441
EXECUTE_FIXTURE = True
@@ -91,6 +98,14 @@ def test_process_by_events(self, sqs_event):
9198

9299
self.assertTrue(result)
93100

101+
@data_provider(get_queue_events_error_samples)
102+
def test_process_by_error_events(self, sqs_event):
103+
self.logger.info('Running test: %s', get_function_name(__name__))
104+
105+
result = self.service.process(sqs_event=sqs_event)
106+
107+
self.assertTrue(result)
108+
94109

95110
if __name__ == '__main__':
96111
unittest.main()

examples/lambda_sqs/tests/datasources/events/sqs/ocorens/cancelamento.event.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
"awsRegion": "us-east-2"
2121
}
2222
]
23-
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"Records": [
3+
{
4+
"messageId": "11d6ee51-4cc7-4302-9e22-7cd8afdaadf5",
5+
"receiptHandle": "AQEBBX8nesZEXmkhsmZeyIE8iQAMig7qw...",
6+
"body": "\"{\n \"chavenfe\": \"32210206107255000134550010001712551245826554\",\n \"ocor\": \"MOTIVO DO CANCELAMENTO\",\n \"origem\": \"SAC/EAGLE\",\n \"pedido\": \"Z1223321\"\n}\"",
7+
"attributes": {
8+
"ApproximateReceiveCount": "1",
9+
"SentTimestamp": "1573251510774",
10+
"SequenceNumber": "18849496460467696128",
11+
"MessageGroupId": "1",
12+
"SenderId": "AIDAIO23YVJENQZJOL4VO",
13+
"MessageDeduplicationId": "1",
14+
"ApproximateFirstReceiveTimestamp": "1573251510774"
15+
},
16+
"messageAttributes": {},
17+
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
18+
"eventSource": "aws:sqs",
19+
"eventSourceARN": "arn:aws:sqs:us-east-2:683720833731:fretzy-delivery-time-simulator-queue.fifo",
20+
"awsRegion": "us-east-2"
21+
}
22+
]
23+
}

0 commit comments

Comments
 (0)