From 5d25969bb68ce53d7f4d7dd2b5b6523e2539947e Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Sun, 13 Jan 2019 00:11:29 +0000 Subject: [PATCH] Provide Lambda context in event object --- chalice/app.py | 7 ++++--- docs/source/api.rst | 29 +++++++++++++++++++++++++++++ tests/unit/test_app.py | 12 ++++++++---- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/chalice/app.py b/chalice/app.py index 6014f0e69..abb6ecf30 100644 --- a/chalice/app.py +++ b/chalice/app.py @@ -992,7 +992,7 @@ def __init__(self, func, event_class): self.event_class = event_class def __call__(self, event, context): - event_obj = self.event_class(event) + event_obj = self.event_class(event, context) return self.func(event_obj) @@ -1001,8 +1001,9 @@ def __call__(self, event, context): # part of Chalice's public API and must be backwards compatible. class BaseLambdaEvent(object): - def __init__(self, event_dict): + def __init__(self, event_dict, context): self._event_dict = event_dict + self.context = context self._extract_attributes(event_dict) def _extract_attributes(self, event_dict): @@ -1047,7 +1048,7 @@ def _extract_attributes(self, event_dict): def __iter__(self): for record in self._event_dict['Records']: - yield SQSRecord(record) + yield SQSRecord(record, self.context) class SQSRecord(BaseLambdaEvent): diff --git a/docs/source/api.rst b/docs/source/api.rst index 110180463..60274b706 100644 --- a/docs/source/api.rst +++ b/docs/source/api.rst @@ -812,6 +812,12 @@ Scheduled Events For scheduled events, this will include the ARN of the CloudWatch rule that triggered this event. + .. attribute:: context + + A `Lambda context object `_ + that is passed to the handler by AWS Lambda. This is useful if you need + the AWS request ID for tracing, or any other data in the context object. + .. method:: to_dict() Return the original event dictionary provided @@ -860,6 +866,12 @@ Scheduled Events access to the original URL encoded key name, you can access it through the ``to_dict()`` method. + .. attribute:: context + + A `Lambda context object `_ + that is passed to the handler by AWS Lambda. This is useful if you need + the AWS request ID for tracing, or any other data in the context object. + .. method:: to_dict() Return the original event dictionary provided @@ -896,6 +908,12 @@ Scheduled Events The string value of the SNS message that was published. + .. attribute:: context + + A `Lambda context object `_ + that is passed to the handler by AWS Lambda. This is useful if you need + the AWS request ID for tracing, or any other data in the context object. + .. method:: to_dict() Return the original event dictionary provided @@ -927,6 +945,12 @@ Scheduled Events the event. Each element in the iterable is of type :class:`SQSRecord`. + .. attribute:: context + + A `Lambda context object `_ + that is passed to the handler by AWS Lambda. This is useful if you need + the AWS request ID for tracing, or any other data in the context object. + .. method:: to_dict() Return the original event dictionary provided @@ -950,6 +974,11 @@ Scheduled Events if you need to manually delete an SQS message to account for partial failures. + .. attribute:: context + + A `Lambda context object `_ + that is passed to the handler by AWS Lambda. + .. method:: to_dict() Return the original dictionary associated with the given diff --git a/tests/unit/test_app.py b/tests/unit/test_app.py index 707f51a1c..1044009b3 100644 --- a/tests/unit/test_app.py +++ b/tests/unit/test_app.py @@ -1463,7 +1463,7 @@ def test_s3_event_urldecodes_keys(): }}, ] } - event = app.S3Event(s3_event) + event = app.S3Event(s3_event, FakeLambdaContext()) # We should urldecode the key name. assert event.key == 'file with spaces' # But the key should remain unchanged in to_dict(). @@ -1487,7 +1487,7 @@ def test_s3_event_urldecodes_unicode_keys(): }}, ] } - event = app.S3Event(s3_event) + event = app.S3Event(s3_event, FakeLambdaContext()) # We should urldecode the key name. assert event.key == u'\u2713' assert event.bucket == u'mybucket' @@ -1533,10 +1533,12 @@ def handler(event): 'TopicArn': 'arn:aws:sns:us-west-2:12345:ConsoleTestTopic', 'Type': 'Notification', 'UnsubscribeUrl': 'https://unsubscribe-url/'}}]} - actual_event = handler(sns_event, context=None) + lambda_context = FakeLambdaContext() + actual_event = handler(sns_event, context=lambda_context) assert actual_event.message == 'This is a raw message' assert actual_event.subject == 'ThisIsTheSubject' assert actual_event.to_dict() == sns_event + assert actual_event.context == lambda_context def test_can_create_sqs_handler(sample_app): @@ -1582,7 +1584,8 @@ def handler(event): 'messageId': 'message-id', 'receiptHandle': 'receipt-handle' }]} - actual_event = handler(sqs_event, context=None) + lambda_context = FakeLambdaContext() + actual_event = handler(sqs_event, context=lambda_context) records = list(actual_event) assert len(records) == 1 first_record = records[0] @@ -1590,6 +1593,7 @@ def handler(event): assert first_record.receipt_handle == 'receipt-handle' assert first_record.to_dict() == sqs_event['Records'][0] assert actual_event.to_dict() == sqs_event + assert actual_event.context == lambda_context def test_bytes_when_binary_type_is_application_json():