DynamoDb Persistence Adapter

DynamoDb Persistence Adapter

class ask_sdk_dynamodb.adapter.DynamoDbAdapter(table_name, partition_key_name='id', attribute_name='attributes', create_table=False, partition_keygen=<function user_id_partition_keygen>, dynamodb_resource=dynamodb.ServiceResource())

Bases: ask_sdk_core.attributes_manager.AbstractPersistenceAdapter

Persistence Adapter implementation using Amazon DynamoDb.

Amazon DynamoDb based persistence adapter implementation. This internally uses the AWS Python SDK (boto3) to process the dynamodb operations. The adapter tries to create the table if create_table is set, during initialization.

Parameters:
  • table_name (str) – Name of the table to be created or used
  • partition_key_name (str) – Partition key name to be used. Defaulted to ‘id’
  • attribute_name (str) – Attribute name for storing and retrieving attributes from dynamodb. Defaulted to ‘attributes’
  • create_table (bool) – Should the adapter try to create the table if it doesn’t exist. Defaulted to False
  • partition_keygen (Callable[[RequestEnvelope], str]) – Callable function that takes a request envelope and provides a unique partition key value. Defaulted to user id keygen function
  • dynamodb_resource (boto3.resources.base.ServiceResource) – Resource to be used, to perform dynamo operations. Defaulted to resource generated from boto3
delete_attributes(request_envelope)

Deletes attributes from table in Dynamodb resource.

Deletes the attributes from Dynamodb table. Raises PersistenceException if table doesn’t exist or delete_item fails on the table.

Parameters:request_envelope (ask_sdk_model.RequestEnvelope) – Request Envelope passed during skill invocation
Return type:None
Raises:ask_sdk_core.exceptions.PersistenceException
get_attributes(request_envelope)

Get attributes from table in Dynamodb resource.

Retrieves the attributes from Dynamodb table. If the table doesn’t exist, returns an empty dict if the create_table variable is set as True, else it raises PersistenceException. Raises PersistenceException if get_item fails on the table.

Parameters:request_envelope (ask_sdk_model.RequestEnvelope) – Request Envelope passed during skill invocation
Returns:Attributes stored under the partition keygen mapping in the table
Return type:Dict[str, object]
Raises:ask_sdk_core.exceptions.PersistenceException
save_attributes(request_envelope, attributes)

Saves attributes to table in Dynamodb resource.

Saves the attributes into Dynamodb table. Raises PersistenceException if table doesn’t exist or put_item fails on the table.

Parameters:
  • request_envelope (ask_sdk_model.RequestEnvelope) – Request Envelope passed during skill invocation
  • attributes (Dict[str, object]) – Attributes stored under the partition keygen mapping in the table
Return type:

None

Raises:

ask_sdk_core.exceptions.PersistenceException

Partition Key Generator Functions

ask_sdk_dynamodb.partition_keygen.device_id_partition_keygen(request_envelope)

Retrieve device id from request envelope, to use as partition key.

Parameters:request_envelope (ask_sdk_model.RequestEnvelope) – Request Envelope passed during skill invocation
Returns:Device Id retrieved from request envelope
Return type:str
Raises:ask_sdk_core.exceptions.PersistenceException
ask_sdk_dynamodb.partition_keygen.user_id_partition_keygen(request_envelope)

Retrieve user id from request envelope, to use as partition key.

Parameters:request_envelope (ask_sdk_model.RequestEnvelope) – Request Envelope passed during skill invocation
Returns:User Id retrieved from request envelope
Return type:str
Raises:ask_sdk_core.exceptions.PersistenceException