Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Correct code examples #317

Merged
merged 2 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aws_lambda_powertools/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Logger(logging.Logger): # lgtm [py/missing-call-to-init]
LOG_LEVEL: str
logging level (e.g. INFO, DEBUG)
POWERTOOLS_LOGGER_SAMPLE_RATE: float
samping rate ranging from 0 to 1, 1 being 100% sampling
sampling rate ranging from 0 to 1, 1 being 100% sampling

Parameters
----------
Expand Down
2 changes: 0 additions & 2 deletions aws_lambda_powertools/middleware_factory/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
class MiddlewareInvalidArgumentError(Exception):
"""When middleware receives non keyword=arguments"""

pass
1 change: 1 addition & 0 deletions aws_lambda_powertools/utilities/batch/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def batch_processor(
Examples
--------
**Processes Lambda's event with PartialSQSProcessor**

>>> from aws_lambda_powertools.utilities.batch import batch_processor, PartialSQSProcessor
>>>
>>> def record_handler(record):
Expand Down
4 changes: 3 additions & 1 deletion aws_lambda_powertools/utilities/batch/sqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class PartialSQSProcessor(BasePartialProcessor):
>>> # have been deleted from the queue after context's exit.
>>>
>>> return result

"""

def __init__(self, config: Optional[Config] = None, suppress_exception: bool = False):
Expand Down Expand Up @@ -163,10 +164,11 @@ def sqs_batch_processor(
Examples
--------
**Processes Lambda's event with PartialSQSProcessor**

>>> from aws_lambda_powertools.utilities.batch import sqs_batch_processor
>>>
>>> def record_handler(record):
>>> return record["body"]
>>> return record["body"]
>>>
>>> @sqs_batch_processor(record_handler=record_handler)
>>> def handler(event, context):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def idempotent(
Examples
--------
**Processes Lambda's event in an idempotent manner**

>>> from aws_lambda_powertools.utilities.idempotency import (
>>> idempotent, DynamoDBPersistenceLayer, IdempotencyConfig
>>> )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ def __init__(
Examples
--------
**Create a DynamoDB persistence layer with custom settings**
>>> from aws_lambda_powertools.utilities.idempotency import idempotent, DynamoDBPersistenceLayer

>>> from aws_lambda_powertools.utilities.idempotency import (
>>> idempotent, DynamoDBPersistenceLayer
>>> )
>>>
>>> persistence_store = DynamoDBPersistenceLayer(table_name="idempotency_store")
>>>
>>> @idempotent(persistence_store=persistence_store, event_key="body")
>>> @idempotent(persistence_store=persistence_store)
>>> def handler(event, context):
>>> return {"StatusCode": 200}
"""
Expand Down
26 changes: 16 additions & 10 deletions docs/utilities/data_classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ For example, if your Lambda function is being triggered by an API Gateway proxy
def lambda_handler(event, context):
event: APIGatewayProxyEvent = APIGatewayProxyEvent(event)

if 'helloworld' in event.path && event.http_method == 'GET':
if 'helloworld' in event.path and event.http_method == 'GET':
do_something_with(event.body, user)
```

Expand Down Expand Up @@ -80,7 +80,7 @@ Typically used for API Gateway REST API or HTTP API using v1 proxy event.
request_context = event.request_context
identity = request_context.identity

if 'helloworld' in event.path && event.http_method == 'GET':
if 'helloworld' in event.path and event.http_method == 'GET':
user = identity.user
do_something_with(event.body, user)
```
Expand All @@ -97,7 +97,7 @@ Typically used for API Gateway REST API or HTTP API using v1 proxy event.
request_context = event.request_context
query_string_parameters = event.query_string_parameters

if 'helloworld' in event.raw_path && request_context.http.method == 'POST':
if 'helloworld' in event.raw_path and request_context.http.method == 'POST':
do_something_with(event.body, query_string_parameters)
```

Expand All @@ -110,11 +110,12 @@ decompress and parse json data from the event.

```python
from aws_lambda_powertools.utilities.data_classes import CloudWatchLogsEvent
from aws_lambda_powertools.utilities.data_classes.cloud_watch_logs_event import CloudWatchLogsDecodedData

def lambda_handler(event, context):
event: CloudWatchLogsEvent = CloudWatchLogsEvent(event)

decompressed_log = event.parse_logs_data
decompressed_log: CloudWatchLogsDecodedData = event.parse_logs_data
log_events = decompressed_log.log_events
for event in log_events:
do_something_with(event.timestamp, event.message)
Expand Down Expand Up @@ -146,7 +147,7 @@ Verify Auth Challenge | `data_classes.cognito_user_pool_event.VerifyAuthChalleng
def lambda_handler(event, context):
event: PostConfirmationTriggerEvent = PostConfirmationTriggerEvent(event)

user_attributes = user_attributes = event.request.user_attributes
user_attributes = event.request.user_attributes
do_something_with(user_attributes)
```

Expand All @@ -159,7 +160,10 @@ attributes values (`AttributeValue`), as well as enums for stream view type (`St
=== "lambda_app.py"

```python
from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import DynamoDBStreamEvent, DynamoDBRecordEventName
from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import (
DynamoDBStreamEvent,
DynamoDBRecordEventName
)

def lambda_handler(event, context):
event: DynamoDBStreamEvent = DynamoDBStreamEvent(event)
Expand Down Expand Up @@ -194,14 +198,16 @@ or plain text, depending on the original payload.
```python
from aws_lambda_powertools.utilities.data_classes import KinesisStreamEvent


def lambda_handler(event, context):
event: KinesisStreamEvent = KinesisStreamEvent(event)

# if data was delivered as json
data = event.data_as_text()
kinesis_record = next(event.records).kinesis

# if data was delivered as text
data = event.data_as_json()
data = kinesis_record.data_as_text()

# if data was delivered as json
data = kinesis_record.data_as_json()

do_something_with(data)
```
Expand Down
2 changes: 1 addition & 1 deletion docs/utilities/idempotency.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ You can quickly start by initializing the `DynamoDBPersistenceLayer` class and u
)
...
return {
"payment_id": payment.id
"payment_id": payment.id,
"message": "success",
"statusCode": 200,
}
Expand Down