Exemplo n.º 1
0
    def test_singleton_serializer_instances(self):
        test_serializer_1 = Serializer.get_instance()
        test_serializer_2 = Serializer.get_instance()

        self.assertIsInstance(test_serializer_1, Serializer)
        self.assertIsInstance(test_serializer_2, Serializer)
        self.assertIs(
            test_serializer_1, test_serializer_2,
            "Serializer get_instance() did not return the same "
            "singleton instance.")
Exemplo n.º 2
0
 def test_singleton_serializer(self):
     with self.assertRaises(TypeError) as exc:
         _test_serializer = Serializer()
     self.assertIn(
         "Singletons must be accessed through get_instance()",
         str(exc.exception),
         "Serializer Singleton class didn't throw exception for "
         "Constructor instantiation.")
Exemplo n.º 3
0
def get_deserialized_request(skill_request_payload):
    # type:(str) -> Request
    """Deserialize the incoming request payload into
    :py:class:`ask_sdk_model.dynamic_endpoints.Request` class.

    :param skill_request_payload: Incoming skill request payload.
    :type skill_request_payload: str
    :return: Serialized skill request.
    :rtype: :py:class:`ask_sdk_model.dynamic_endpoints.Request
    """
    try:
        default_serializer = Serializer.get_instance()  # type: ignore
        return default_serializer.deserialize(payload=skill_request_payload,
                                              obj_type=Request)
    except Exception as ex:
        logger.error("Failed to deserialize request : {}".format(str(ex)))
        raise LocalDebugSdkException(
            "Failed to deserialize skill_request : {}".format(str(ex)))
Exemplo n.º 4
0
def get_skill_response(local_debug_request, skill_invoker_config):
    # type: (Request, SkillInvokerConfiguration) -> str
    """Invokes skill code with skill request payload.

    :param local_debug_request: Skill request payload.
    :type local_debug_request: :py:class:`ask_sdk_model.dynamic_endpoints.Request`
    :param skill_invoker_config: Skill Invoker instance to invoke skill code
    :type skill_invoker_config: :py:class:`ask_sdk_local_debug.skill_invoker_config.SkillInvokerConfiguration`
    :return: Response payload.
    :rtype: str
    """
    try:
        request_envelope = json.loads(local_debug_request.request_payload)
        skill_builder_func = getattr(skill_invoker_config.skill_invoker,
                                     skill_invoker_config.skill_handler)
        default_serializer = Serializer.get_instance()  # type: ignore
        response_payload = None  # type: BaseResponse

        try:
            skill_response = skill_builder_func(request_envelope, None)
        except Exception as ex:
            logger.error("Failed to retrieve skill response : {}".format(
                str(ex)))
            response_payload = get_local_debug_failure_response(
                local_debug_request=local_debug_request, exception=ex)

        if response_payload is None:
            skill_response = json.dumps(skill_response)
            response_payload = get_local_debug_success_response(
                local_debug_request=local_debug_request,
                skill_success_response=skill_response)
        serialized_response = default_serializer.serialize(response_payload)

        return json.dumps(serialized_response)
    except Exception as ex:
        logger.error("Error in get_skill_response : {}".format(str(ex)))
        raise LocalDebugSdkException("Error in get_skill_response : {}".format(
            str(ex)))
Exemplo n.º 5
0
 def setUp(self):
     self.test_local_debug_request = Request(
         request_id=self.TEST_REQUEST_ID,
         version=self.TEST_VERSION,
         request_payload=self.TEST_REQUEST_PAYLOAD)
     self.default_serializer = Serializer.get_instance()