# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12import base6413import binascii14import logging1516logger = logging.getLogger(__name__)171819def register_dynamodb_paginator_fix(event_emitter):20DynamoDBPaginatorFix(event_emitter).register_events()212223def parse_last_evaluated_key_binary(parsed, **kwargs):24# Because we disable parsing blobs into a binary type and leave them as25# a base64 string if a binary field is present in the continuation token26# as is the case with dynamodb the binary will be double encoded. This27# ensures that the continuation token is properly converted to binary to28# avoid double encoding the contination token.29last_evaluated_key = parsed.get('LastEvaluatedKey', None)30if last_evaluated_key is None:31return32for key, val in last_evaluated_key.items():33if 'B' in val:34val['B'] = base64.b64decode(val['B'])353637class DynamoDBPaginatorFix(object):38def __init__(self, event_emitter):39self._event_emitter = event_emitter4041def register_events(self):42self._event_emitter.register(43'calling-command.dynamodb.*', self._maybe_register_pagination_fix44)4546def _maybe_register_pagination_fix(self, parsed_globals, **kwargs):47if parsed_globals.paginate:48self._event_emitter.register(49'after-call.dynamodb.*', parse_last_evaluated_key_binary50)515253