Runtime

Request Dispatch Components

Abstract Classes

class ask_sdk_runtime.dispatch.AbstractRequestDispatcher

Bases: object

Dispatcher which handles dispatching input request to the corresponding handler.

User needs to implement the dispatch method, to handle the processing of the incoming request in the handler input. A response may be expected out of the dispatch method.

dispatch(handler_input)

Dispatches an incoming request to the appropriate request handler and returns the output.

Parameters:handler_input (Input) – generic input to the dispatcher
Returns:generic output returned by handler in the dispatcher
Return type:Union[None, Output]
class ask_sdk_runtime.dispatch_components.request_components.AbstractRequestHandler

Bases: object

Request Handlers are responsible for processing dispatch inputs and generating output.

Custom request handlers needs to implement can_handle and handle methods. can_handle returns True if the handler can handle the current input. handle processes the input and may return a output.

can_handle(handler_input)

Returns true if Request Handler can handle the dispatch input.

Parameters:handler_input (Input) – Generic input passed to the dispatcher.
Returns:Boolean value that tells the dispatcher if the current input can be handled by this handler.
Return type:bool
handle(handler_input)

Handles the dispatch input and provides an output for dispatcher to return.

Parameters:handler_input (Input) – Generic input passed to the dispatcher.
Returns:Generic Output for the dispatcher to return or None
Return type:Union[Output, None]
class ask_sdk_runtime.dispatch_components.request_components.AbstractRequestInterceptor

Bases: object

Interceptor that runs before the handler is called.

The process method has to be implemented, to run custom logic on the input, before it is handled by the Handler.

process(handler_input)

Process the input before the Handler is run.

Parameters:handler_input (Input) – Generic input passed to the dispatcher.
Return type:None
class ask_sdk_runtime.dispatch_components.request_components.AbstractResponseInterceptor

Bases: object

Interceptor that runs after the handler is called.

The process method has to be implemented, to run custom logic on the input and the dispatch output generated after the handler is executed on the input.

process(handler_input, response)

Process the input and the output after the Handler is run.

Parameters:
  • handler_input (Input) – Generic input passed to the dispatcher.
  • response (Union[None, Output]) – Execution result of the Handler on dispatch input.
Return type:

None

class ask_sdk_runtime.dispatch_components.request_components.AbstractRequestHandlerChain

Bases: object

Abstract class containing Request Handler and corresponding Interceptors.

request_handler()
Returns:Registered Request Handler instance.
Return type:object
request_interceptors()
Returns:List of registered Request Interceptors.
Return type:list( ask_sdk_runtime.dispatch_components.request_components.AbstractRequestInterceptor)
response_interceptors()
Returns:List of registered Response Interceptors.
Return type:list( ask_sdk_runtime.dispatch_components.request_components.AbstractResponseInterceptor)
class ask_sdk_runtime.dispatch_components.request_components.AbstractRequestMapper

Bases: object

Class for request routing to the appropriate handler chain.

User needs to implement get_request_handler_chain method, to provide a routing mechanism of the input to the appropriate request handler chain containing the handler and the interceptors.

get_request_handler_chain(handler_input)

Get the handler chain that can process the handler input.

Parameters:handler_input (Input) – Generic input passed to the dispatcher.
Returns:Handler Chain that can handle the request under dispatch input.
Return type:AbstractRequestHandlerChain
class ask_sdk_runtime.dispatch_components.request_components.AbstractHandlerAdapter

Bases: object

Abstracts handling of a request for specific handler types.

supports(handler)

Returns true if adapter supports the handler.

This method checks if the adapter supports the handler execution. This is usually checked by the type of the handler.

Parameters:handler (object) – Request Handler instance.
Returns:Boolean denoting whether the adapter supports the handler.
Return type:bool
execute(handler_input, handler)

Executes the handler with the provided dispatch input.

Parameters:
  • handler_input (Input) – Generic input passed to the dispatcher.
  • handler (object) – Request Handler instance.
Returns:

Result executed by passing handler_input to handler.

Return type:

Union[None, Output]

class ask_sdk_runtime.dispatch_components.exception_components.AbstractExceptionHandler

Bases: object

Handles exception types and optionally produce an output.

The abstract class is similar to Request Handler, with methods can_handle and handle. The can_handle method checks if the handler can support the input and the exception. The handle method processes the input and exception, to optionally produce an output.

can_handle(handler_input, exception)

Checks if the handler can support the exception raised during dispatch.

Parameters:
  • handler_input (Input) – Generic input passed to the dispatcher.
  • exception (Exception) – Exception raised during dispatch.
Returns:

Boolean whether handler can handle exception or not.

Return type:

bool

handle(handler_input, exception)

Process the dispatch input and exception.

Parameters:
  • handler_input (Input) – Generic input passed to the dispatcher.
  • exception (Exception) – Exception raised during dispatch.
Returns:

Optional output object to serve as dispatch return.

Return type:

Union[None, Output]

class ask_sdk_runtime.dispatch_components.exception_components.AbstractExceptionMapper

Bases: object

Mapper to register custom Exception Handler instances.

The exception mapper is used by ask_sdk_runtime.dispatch.GenericRequestDispatcher dispatch method, to handle exceptions. The mapper can contain one or more exception handlers. Handlers are accessed through the mapper to attempt to find a handler that is compatible with the current exception.

get_handler(handler_input, exception)

Returns a suitable exception handler to dispatch the specified exception, if one exists.

Parameters:
Returns:

Exception Handler that can handle the input or None.

Return type:

Union[None, AbstractExceptionHandler]

Implementations

class ask_sdk_runtime.dispatch.GenericRequestDispatcher(options)

Bases: ask_sdk_runtime.dispatch.AbstractRequestDispatcher

Generic implementation of AbstractRequestDispatcher.

The runtime configuration contains the components required for the dispatcher, which is passed during initialization.

When the dispatch method is invoked, using a list of ask_sdk_runtime.dispatch_components.request_components.RequestMapper , the Dispatcher finds a handler for the request and delegates the invocation to the supported ask_sdk_runtime.dispatch_components.request_components.HandlerAdapter . If the handler raises any exception, it is delegated to ask_sdk_runtime.dispatch_components.exception_components.ExceptionMapper to handle or raise it to the upper stack.

dispatch(handler_input)

Dispatches an incoming request to the appropriate request handler and returns the output.

Before running the request on the appropriate request handler, dispatcher runs any predefined global request interceptors. On successful response returned from request handler, dispatcher runs predefined global response interceptors, before returning the response.

Parameters:handler_input (Input) – generic input to the dispatcher
Returns:generic output handled by the handler, optionally containing a response
Return type:Union[None, Output]
Raises:ask_sdk_runtime.exceptions.DispatchException
class ask_sdk_runtime.dispatch_components.request_components.GenericRequestHandlerChain(request_handler, request_interceptors=None, response_interceptors=None)

Bases: ask_sdk_runtime.dispatch_components.request_components.AbstractRequestHandlerChain

Generic implementation of AbstractRequestHandlerChain.

Generic Request Handler Chain accepts request handler of any type.

Parameters:
request_handler
Returns:Registered Request Handler instance.
Return type:object
request_interceptors
Returns:List of registered Request Interceptors.
Return type:list( ask_sdk_runtime.dispatch_components.request_components.AbstractRequestInterceptor)
response_interceptors
Returns:List of registered Response Interceptors.
Return type:list( ask_sdk_runtime.dispatch_components.request_components.AbstractResponseInterceptor)
add_request_interceptor(interceptor)

Add interceptor to Request Interceptors list.

Parameters:interceptor (ask_sdk_runtime.dispatch_components.request_components.AbstractRequestInterceptor) – Request Interceptor instance.
add_response_interceptor(interceptor)

Add interceptor to Response Interceptors list.

Parameters:interceptor (ask_sdk_runtime.dispatch_components.request_components.AbstractResponseInterceptor) – Response Interceptor instance.
class ask_sdk_runtime.dispatch_components.request_components.GenericRequestMapper(request_handler_chains)

Bases: ask_sdk_runtime.dispatch_components.request_components.AbstractRequestMapper

Implementation of AbstractRequestMapper that registers RequestHandlerChain.

The class accepts request handler chains of type GenericRequestHandlerChain only. The get_request_handler_chain method returns the GenericRequestHandlerChain instance that can handle the request in the handler input.

Parameters:request_handler_chains (list(GenericRequestHandlerChain)) – List of GenericRequestHandlerChain instances.
request_handler_chains
Returns:List of GenericRequestHandlerChain instances.
Return type:list(GenericRequestHandlerChain)
add_request_handler_chain(request_handler_chain)

Checks the type before adding it to the request_handler_chains instance variable.

Parameters:request_handler_chain (RequestHandlerChain) – Request Handler Chain instance.
Raises:ask_sdk_runtime.exceptions.DispatchException if a null input is provided or if the input is of invalid type
get_request_handler_chain(handler_input)

Get the request handler chain that can handle the dispatch input.

Parameters:handler_input (Input) – Generic input passed to the dispatcher.
Returns:Handler Chain that can handle the input.
Return type:Union[None, GenericRequestHandlerChain]
class ask_sdk_runtime.dispatch_components.request_components.GenericHandlerAdapter

Bases: ask_sdk_runtime.dispatch_components.request_components.AbstractHandlerAdapter

GenericHandler Adapter for handlers of type ask_sdk_runtime.dispatch_components.request_components.AbstractRequestHandler.

supports(handler)

Returns true if handler is ask_sdk_runtime.dispatch_components.request_components.AbstractRequestHandler instance.

Parameters:handler (ask_sdk_runtime.dispatch_components.request_components.AbstractRequestHandler) – Request Handler instance
Returns:Boolean denoting whether the adapter supports the handler.
Return type:bool
execute(handler_input, handler)

Executes the handler with the provided handler input.

Parameters:
  • handler_input (Input) – Generic input passed to the dispatcher.
  • handler (object) – Request Handler instance.
Returns:

Result executed by passing handler_input to handler.

Return type:

Union[None, Output]

class ask_sdk_runtime.dispatch_components.exception_components.GenericExceptionMapper(exception_handlers)

Bases: ask_sdk_runtime.dispatch_components.exception_components.AbstractExceptionMapper

Generic Implementation of exception mapper, to register AbstractExceptionHandler instances.

The class accepts exception handlers of type AbstractExceptionHandler only. The get_handler method returns the AbstractExceptionHandler instance that can handle the dispatch input and the exception raised from the dispatch method.

Parameters:exception_handlers (list( ask_sdk_runtime.dispatch_components.exception_components.AbstractExceptionHandler)) – List of ask_sdk_runtime.dispatch_components.exception_components.AbstractExceptionHandler instances.
exception_handlers
Returns:List of ask_sdk_runtime.dispatch_components.exception_components.AbstractExceptionHandler instances.
Return type:list( ask_sdk_runtime.dispatch_components.exception_components.AbstractExceptionHandler)
add_exception_handler(exception_handler)

Checks the type before adding it to the exception_handlers instance variable.

Parameters:exception_handler (ask_sdk_runtime.dispatch_components.exception_components.AbstractExceptionHandler) – Exception Handler instance.
Raises:ask_sdk_runtime.exceptions.DispatchException if a null input is provided or if the input is of invalid type
get_handler(handler_input, exception)

Get the exception handler that can handle the input and exception.

Parameters:
Returns:

Exception Handler that can handle the input or None.

Return type:

Union[None, ask_sdk_runtime.dispatch_components.exception_components.AbstractExceptionHandler]

Skill Components

class ask_sdk_runtime.skill.RuntimeConfiguration(request_mappers, handler_adapters, request_interceptors=None, response_interceptors=None, exception_mapper=None, loaders=None, renderer=None)

Bases: object

Configuration Object that represents standard components needed to build the dispatcher in the AbstractSkill.

Parameters:
class ask_sdk_runtime.skill.RuntimeConfigurationBuilder

Bases: object

Builder class for creating a runtime configuration object, from base dispatch components.

add_request_handler(request_handler)

Register input to the request handlers list.

Parameters:request_handler (AbstractRequestHandler) – Request Handler instance to be registered.
Returns:None
add_request_handlers(request_handlers)

Register input to the request handlers list.

Parameters:request_handlers (list(AbstractRequestHandler)) – List of Request Handler instances to be registered.
Returns:None
add_exception_handler(exception_handler)

Register input to the exception handlers list.

Parameters:exception_handler (AbstractExceptionHandler) – Exception Handler instance to be registered.
Returns:None
add_global_request_interceptor(request_interceptor)

Register input to the global request interceptors list.

Parameters:request_interceptor (AbstractRequestInterceptor) – Request Interceptor instance to be registered.
Returns:None
add_global_response_interceptor(response_interceptor)

Register input to the global response interceptors list.

Parameters:response_interceptor (AbstractResponseInterceptor) – Response Interceptor instance to be registered.
Returns:None
add_loader(loader)

Register input to the loaders list.

Parameters:loader (ask_sdk_runtime.view_resolvers.AbstractTemplateLoader) – Loader to load the template
add_loaders(loaders)

Register input to the loaders list.

Parameters:loaders (ask_sdk_runtime.view_resolvers.AbstractTemplateLoader) – List of loaders
add_renderer(renderer)

Register input to the renderer.

Parameters:renderer (ask_sdk_runtime.view_resolvers.AbstractTemplateRenderer) – Renderer to render the template
get_runtime_configuration()

Build the runtime configuration object from the registered components.

Returns:Runtime Configuration Object
Return type:RuntimeConfiguration
class ask_sdk_runtime.skill.AbstractSkill

Bases: object

Abstract class that acts as entry level container for skill invocation.

Domain SDKs should implement the supports and invoke methods.

supports(event, context)

Check if the skill supports the corresponding input.

Parameters:
  • event (SkillInput) – input instance containing request information.
  • context (Any) – Context passed during invocation
Returns:

boolean if this type of request can be handled by this skill.

Return type:

bool

invoke(event, context)

Invokes the dispatcher, to handle the skill input and return a skill output.

Parameters:
  • event (SkillInput) – input instance containing request information.
  • context (Any) – Context passed during invocation
Returns:

output generated by handling the request.

Return type:

SkillOutput

class ask_sdk_runtime.skill_builder.AbstractSkillBuilder

Bases: object

Abstract Skill Builder with helper functions for building ask_sdk_runtime.skill.AbstractSkill object.

Domain SDKs has to implement the create method that returns an instance of the skill implementation for the domain type.

add_request_handler(request_handler)

Register input to the request handlers list.

Parameters:request_handler (ask_sdk_runtime.dispatch_components.request_components.AbstractRequestHandler) – Request Handler instance to be registered.
Returns:None
add_exception_handler(exception_handler)

Register input to the exception handlers list.

Parameters:exception_handler (ask_sdk_runtime.dispatch_components.request_components.AbstractExceptionHandler) – Exception Handler instance to be registered.
Returns:None
add_global_request_interceptor(request_interceptor)

Register input to the global request interceptors list.

Parameters:request_interceptor (ask_sdk_runtime.dispatch_components.request_components.AbstractRequestInterceptor) – Request Interceptor instance to be registered.
Returns:None
add_global_response_interceptor(response_interceptor)

Register input to the global response interceptors list.

Parameters:response_interceptor (ask_sdk_runtime.dispatch_components.request_components.AbstractResponseInterceptor) – Response Interceptor instance to be registered.
Returns:None
add_loaders(loaders)

Register input to the loaders list.

Parameters:loaders (ask_sdk_runtime.view_resolvers.AbstractTemplateLoader) – List of loaders
add_loader(loader)

Register input to loaders list.

Parameters:loader (ask_sdk_runtime.view_resolvers.AbstractTemplateLoader) – Loader to load template from a specific data source
add_renderer(renderer)

Register renderer to generate template responses.

Parameters:renderer (ask_sdk_runtime.view_resolvers.AbstractTemplateRenderer) – Renderer to render the template
request_handler(can_handle_func)

Decorator that can be used to add request handlers easily to the builder.

The can_handle_func has to be a Callable instance, which takes a single parameter and no varargs or kwargs. This is because of the RequestHandler class signature restrictions. The returned wrapper function can be applied as a decorator on any function that returns a response object by the skill. The function should follow the signature of the handle function in ask_sdk_runtime.dispatch_components.request_components.AbstractRequestHandler class.

Parameters:can_handle_func (Callable[[Input], bool]) – The function that validates if the request can be handled.
Returns:Wrapper function that can be decorated on a handle function.
exception_handler(can_handle_func)

Decorator that can be used to add exception handlers easily to the builder.

The can_handle_func has to be a Callable instance, which takes two parameters and no varargs or kwargs. This is because of the ExceptionHandler class signature restrictions. The returned wrapper function can be applied as a decorator on any function that processes the exception raised during dispatcher and returns a response object by the skill. The function should follow the signature of the handle function in ask_sdk_runtime.dispatch_components.exception_components.AbstractExceptionHandler class.

Parameters:can_handle_func (Callable[[Input, Exception], bool]) – The function that validates if the exception can be handled.
Returns:Wrapper function that can be decorated on a handle function.
global_request_interceptor()

Decorator that can be used to add global request interceptors easily to the builder.

The returned wrapper function can be applied as a decorator on any function that processes the input. The function should follow the signature of the process function in ask_sdk_runtime.dispatch_components.request_components.AbstractRequestInterceptor class.

Returns:Wrapper function that can be decorated on a interceptor process function.
global_response_interceptor()

Decorator that can be used to add global response interceptors easily to the builder.

The returned wrapper function can be applied as a decorator on any function that processes the input and the response generated by the request handler. The function should follow the signature of the process function in ask_sdk_runtime.dispatch_components.request_components.AbstractResponseInterceptor class.

Returns:Wrapper function that can be decorated on a interceptor process function.
create()

Create a skill object using the registered components.

Returns:a skill object that can be used for invocation.
Return type:AbstractSkill

SDK Exceptions

exception ask_sdk_runtime.exceptions.AskSdkException

Bases: Exception

Base class for exceptions raised by the SDK.

exception ask_sdk_runtime.exceptions.DispatchException

Bases: ask_sdk_runtime.exceptions.AskSdkException

Class for exceptions raised during dispatch logic.

exception ask_sdk_runtime.exceptions.SerializationException

Bases: ask_sdk_runtime.exceptions.AskSdkException

Class for exceptions raised during serialization/deserialization.

exception ask_sdk_runtime.exceptions.SkillBuilderException

Bases: ask_sdk_runtime.exceptions.AskSdkException

Base exception class for Skill Builder exceptions.

exception ask_sdk_runtime.exceptions.RuntimeConfigException

Bases: ask_sdk_runtime.exceptions.AskSdkException

Base exception class for Runtime Configuration Builder exceptions.