Skip to content

docs(dataclasses): new Connect Contact Flow #310

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

Merged
Merged
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
65 changes: 52 additions & 13 deletions docs/utilities/data_classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: Utility

The event source data classes utility provides classes describing the schema of common Lambda events triggers.

**Key Features**
## Key Features

* Type hinting and code completion for common event types
* Helper functions for decoding/deserializing nested fields
Expand All @@ -17,13 +17,30 @@ When authoring Lambda functions, you often need to understand the schema of the
handler. There are several common event types which follow a specific schema, depending on the service triggering the
Lambda function.

## Getting started

## Utilizing the data classes
### Utilizing the data classes

The classes are initialized by passing in the Lambda event object into the constructor of the appropriate data class.

For example, if your Lambda function is being triggered by an API Gateway proxy integration, you can use the
`APIGatewayProxyEvent` class.

=== "app.py"

```python hl_lines="1 4"
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent

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

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

**Autocomplete with self-documented properties and methods**


![Utilities Data Classes](../media/utilities_data_classes.png)


Expand All @@ -49,7 +66,7 @@ Event Source | Data_class
documentation inherently (via autocompletion, types and docstrings).


## API Gateway Proxy
### API Gateway Proxy

Typically used for API Gateway REST API or HTTP API using v1 proxy event.

Expand All @@ -68,7 +85,7 @@ Typically used for API Gateway REST API or HTTP API using v1 proxy event.
do_something_with(event.body, user)
```

## API Gateway Proxy v2
### API Gateway Proxy v2

=== "lambda_app.py"

Expand All @@ -84,7 +101,7 @@ Typically used for API Gateway REST API or HTTP API using v1 proxy event.
do_something_with(event.body, query_string_parameters)
```

## CloudWatch Logs
### CloudWatch Logs

CloudWatch Logs events by default are compressed and base64 encoded. You can use the helper function provided to decode,
decompress and parse json data from the event.
Expand All @@ -103,7 +120,7 @@ decompress and parse json data from the event.
do_something_with(event.timestamp, event.message)
```

## Cognito User Pool
### Cognito User Pool

Cognito User Pools have several [different Lambda trigger sources](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html#cognito-user-identity-pools-working-with-aws-lambda-trigger-sources), all of which map to a different data class, which
can be imported from `aws_lambda_powertools.data_classes.cognito_user_pool_event`:
Expand Down Expand Up @@ -133,7 +150,7 @@ Verify Auth Challenge | `data_classes.cognito_user_pool_event.VerifyAuthChalleng
do_something_with(user_attributes)
```

## DynamoDB Streams
### DynamoDB Streams

The DynamoDB data class utility provides the base class for `DynamoDBStreamEvent`, a typed class for
attributes values (`AttributeValue`), as well as enums for stream view type (`StreamViewType`) and event type
Expand All @@ -154,7 +171,7 @@ attributes values (`AttributeValue`), as well as enums for stream view type (`St
do_something_with(record.dynamodb.old_image)
```

## EventBridge
### EventBridge

=== "lambda_app.py"

Expand All @@ -167,7 +184,7 @@ attributes values (`AttributeValue`), as well as enums for stream view type (`St

```

## Kinesis streams
### Kinesis streams

Kinesis events by default contain base64 encoded data. You can use the helper function to access the data either as json
or plain text, depending on the original payload.
Expand All @@ -189,7 +206,7 @@ or plain text, depending on the original payload.
do_something_with(data)
```

## S3
### S3

=== "lambda_app.py"

Expand All @@ -207,7 +224,7 @@ or plain text, depending on the original payload.
do_something_with(f'{bucket_name}/{object_key}')
```

## SES
### SES

=== "lambda_app.py"

Expand All @@ -225,7 +242,7 @@ or plain text, depending on the original payload.
do_something_with(common_headers.to, common_headers.subject)
```

## SNS
### SNS

=== "lambda_app.py"

Expand All @@ -243,7 +260,7 @@ or plain text, depending on the original payload.
do_something_with(subject, message)
```

## SQS
### SQS

=== "lambda_app.py"

Expand All @@ -257,3 +274,25 @@ or plain text, depending on the original payload.
for record in event.records:
do_something_with(record.body)
```

### Connect

**Connect Contact Flow**

=== "lambda_app.py"

```python
from aws_lambda_powertools.utilities.data_classes.connect_contact_flow_event import (
ConnectContactFlowChannel,
ConnectContactFlowEndpointType,
ConnectContactFlowEvent,
ConnectContactFlowInitiationMethod,
)

def lambda_handler(event, context):
event: ConnectContactFlowEvent = ConnectContactFlowEvent(event)
assert event.contact_data.attributes == {"Language": "en-US"}
assert event.contact_data.channel == ConnectContactFlowChannel.VOICE
assert event.contact_data.customer_endpoint.endpoint_type == ConnectContactFlowEndpointType.TELEPHONE_NUMBER
assert event.contact_data.initiation_method == ConnectContactFlowInitiationMethod.API
```