def create(self, request):
        print('LOG event.create.request -----')
        print(request)

        try:
            json_object = json.loads(request['body'])

            # Transpose the Endpoint Cloud Event into an Alexa Event Gateway Event

            # Get the common information from the body of the request
            event_type = json_object['event'][
                'type']  # Expect AddOrUpdateReport, ChangeReport, DeleteReport
            endpoint_user_id = json_object['event']['endpoint'][
                'userId']  # Expect a Profile
            endpoint_id = json_object['event']['endpoint'][
                'id']  # Expect a valid AWS IoT Thing Name

            # Get the Access Token
            token = self.get_user_info(endpoint_user_id)

            # Build a default response
            response = AlexaResponse(name='ErrorResponse',
                                     message="No valid event type")

            if event_type == 'AddOrUpdateReport':
                # Get the additional information from the body of the request
                endpoint_friendly_name = json_object['event']['endpoint'][
                    'friendlyName']  # Expect a valid string friendly name
                endpoint_capabilities = json_object['event']['endpoint'][
                    'capabilities']  # Expect a valid AWS IoT Thing Name
                sku = json_object['event']['endpoint'][
                    'sku']  # Expect a meaningful type, ex: SW00

                # From the SKU, get the information for the device and combine it in the payload
                endpoint_sku_details = self.get_sku_details(sku)
                payload = {
                    'endpoints': [{
                        'endpointId':
                        endpoint_id,
                        'friendlyName':
                        endpoint_friendly_name,
                        'description':
                        endpoint_sku_details['description'],
                        'manufacturerName':
                        endpoint_sku_details['manufacturer_name'],
                        'displayCategories':
                        endpoint_sku_details['display_categories'],
                        'capabilities':
                        endpoint_capabilities
                    }],
                    'scope': {
                        'type': 'BearerToken',
                        'token': token
                    }
                }

                # Send an event to Alexa to add/update the endpoint
                response = self.send_event('Alexa.Discovery',
                                           'AddOrUpdateReport', endpoint_id,
                                           token, payload)

            if event_type == 'ChangeReport':
                try:
                    state = json_object['event']['endpoint'][
                        'state']  # Expect a string, ex: powerState
                    state_value = json_object['event']['endpoint'][
                        'value']  # Expect string or JSON

                    # Update the IoT Thing Shadow state
                    msg = {'state': {'desired': {state: state_value}}}
                    mqtt_msg = json.dumps(msg)
                    result = iot_data_aws.update_thing_shadow(
                        thingName=endpoint_id, payload=mqtt_msg.encode())
                    print(
                        'LOG event.create.iot_aws.update_thing_shadow.result -----'
                    )
                    print(result)

                    # Update Alexa with an Event Update
                    if endpoint_user_id == '0':
                        print('LOG Event: Not sent for user_id of 0')
                    else:
                        payload = {
                            'change': {
                                'cause': {
                                    'type': 'PHYSICAL_INTERACTION'
                                },
                                "properties": [
                                    AlexaResponse.create_context_property(
                                        name=state, value=state_value)
                                ]
                            }
                        }
                        print('LOG Event: Sending event')
                        response = self.send_event('Alexa', 'ChangeReport',
                                                   endpoint_id, token, payload)

                except ClientError as e:
                    alexa_response = AlexaResponse(name='ErrorResponse',
                                                   message=e,
                                                   payload={
                                                       'type':
                                                       'INTERNAL_ERROR',
                                                       'message': e
                                                   })
                    return alexa_response.get()

            if event_type == 'DeleteReport':
                # Send an event to Alexa to delete the endpoint
                payload = {
                    'endpoints': [{
                        'endpointId': endpoint_id
                    }],
                    "scope": {
                        "type": "BearerToken",
                        "token": token
                    }
                }
                response = self.send_event('Alexa.Discovery', 'DeleteReport',
                                           endpoint_id, token, payload)

            result = response.read().decode('utf-8')
            print('LOG event.create.result -----')
            print(result)
            return result

        except KeyError as key_error:
            return "KeyError: " + str(key_error)
    def create(self, request):
        print('LOG event.create.request -----')
        print(request)

        try:
            json_object = json.loads(request['body'])

            # Transpose the Endpoint Cloud Event into an Alexa Event Gateway Event

            # Get the common information from the body of the request
            event_type = json_object['event'][
                'type']  # Expect AddOrUpdateReport, ChangeReport, DeleteReport
            endpoint_id = json_object['event']['endpoint'][
                'id']  # Expect a valid AWS IoT Thing Name

            if 'userId' in json_object['event']['endpoint']:
                endpoint_user_id = json_object['event']['endpoint']['userId']
            else:
                endpoint_user_id = os.environ.get('user_id', None)
            # Get the Access Token
            token = self.get_user_info(endpoint_user_id)

            # Build a default response
            response = AlexaResponse(name='ErrorResponse',
                                     message="No valid event type")

            if event_type == 'AddOrUpdateReport':
                # Get the additional information from the body of the request
                endpoint_friendly_name = json_object['event']['endpoint'][
                    'friendlyName']  # Expect a valid string friendly name
                endpoint_capabilities = json_object['event']['endpoint'][
                    'capabilities']  # Expect a valid AWS IoT Thing Name
                endpoint_display_categories = json_object['event']['endpoint'][
                    'displayCategories']
                endpoint_description = json_object['event']['endpoint'][
                    'description']
                endpoint_manufacturer_name = json_object['event']['endpoint'][
                    'manufacturerName']

                payload = {
                    'endpoints': [{
                        'endpointId': endpoint_id,
                        'friendlyName': endpoint_friendly_name,
                        'description': endpoint_description,
                        'manufacturerName': endpoint_manufacturer_name,
                        'displayCategories': endpoint_display_categories,
                        'capabilities': endpoint_capabilities
                    }],
                    'scope': {
                        'type': 'BearerToken',
                        'token': token
                    }
                }

                # Send an event to Alexa to add/update the endpoint
                response = self.send_event('Alexa.Discovery',
                                           'AddOrUpdateReport', endpoint_id,
                                           token, payload)

            if event_type == 'ChangeReport':
                try:
                    state = json_object['event']['endpoint'][
                        'state']  # Expect a string, ex: powerState
                    state_value = json_object['event']['endpoint'][
                        'value']  # Expect string or JSON
                    namespace = json_object['event']['endpoint']['namespace']
                    instance = json_object['event']['endpoint'].get(
                        'instance', None)
                    if instance:
                        state = instance + '.' + state
                        prop = AlexaResponse.create_context_property(
                            instance=instance,
                            namespace=namespace,
                            name=state,
                            value=state_value)
                    else:
                        prop = AlexaResponse.create_context_property(
                            namespace=namespace, name=state, value=state_value)

                    # Update Alexa with an Event Update
                    if endpoint_user_id == '0':
                        print('LOG Event: Not sent for user_id of 0')
                    else:
                        payload = {
                            'change': {
                                'cause': {
                                    'type': 'PHYSICAL_INTERACTION'
                                },
                                "properties": [prop]
                            }
                        }
                        print('LOG Event: Sending event')
                        response = self.send_event('Alexa', 'ChangeReport',
                                                   endpoint_id, token, payload)

                except ClientError as e:
                    alexa_response = AlexaResponse(name='ErrorResponse',
                                                   message=e,
                                                   payload={
                                                       'type':
                                                       'INTERNAL_ERROR',
                                                       'message': e
                                                   })
                    return alexa_response.get()

            if event_type == 'DeleteReport':
                # Send an event to Alexa to delete the endpoint
                payload = {
                    'endpoints': [{
                        'endpointId': endpoint_id
                    }],
                    "scope": {
                        "type": "BearerToken",
                        "token": token
                    }
                }
                response = self.send_event('Alexa.Discovery', 'DeleteReport',
                                           endpoint_id, token, payload)

            result = response.read().decode('utf-8')
            print('LOG event.create.result -----')
            print(result)
            return result

        except KeyError as key_error:
            return "KeyError: " + str(key_error)