Models Runtime :ask_sdk_model_runtime package

The Models Runtime classes contains the common models package by the Software Development Kit (SDK) team for Python that provides common implementations like Serializer, ApiClient BaseServiceClient, Authentication Configuration etc. that is used by the SDK models.

Submodules

Note

Canonical imports have been added in the __init__.py of the package. This helps in importing the class directly from the package, than through the module.

For eg: if package a has module b with class C, you can do from a import C instead of from a.b import C.

ask_sdk_model_runtime.api_client module

class ask_sdk_model_runtime.api_client.ApiClient

Bases: object

Represents a basic contract for API request invocation.

invoke(request)

Dispatches a request to an API endpoint described in the request. The ApiClient is expected to resolve in the case an API returns a non-200 HTTP status code. The responsibility of translating a particular response code to an error lies with the caller. :param request: Request to dispatch to the ApiClient :type request: ApiClientRequest :return: Response from the client call :rtype: ApiClientResponse

class ask_sdk_model_runtime.api_client.DefaultApiClient

Bases: ask_sdk_model_runtime.api_client.ApiClient

Default ApiClient implementation of ask_sdk_model_runtime.api_client.ApiClient using the requests library.

invoke(request)

Dispatches a request to an API endpoint described in the request. Resolves the method from input request object, converts the list of header tuples to the required format (dict) for the requests lib call and invokes the method with corresponding parameters on requests library. The response from the call is wrapped under the ApiClientResponse object and the responsibility of translating a response code and response/ error lies with the caller. :param request: Request to dispatch to the ApiClient :type request: ApiClientRequest :return: Response from the client call :rtype: ApiClientResponse :raises: ask_sdk_model_runtime.exceptions.ApiClientException

ask_sdk_model_runtime.api_client_message module

class ask_sdk_model_runtime.api_client_message.ApiClientMessage(headers=None, body=None)

Bases: object

Represents the interface between ask_sdk_model_runtime.api_client.ApiClient implementation and a Service Client.

Parameters:
  • headers (list[tuple[str, str]]) – List of header tuples
  • body (str) – Body of the message

ask_sdk_model_runtime.api_client_request module

class ask_sdk_model_runtime.api_client_request.ApiClientRequest(headers=None, body=None, url=None, method=None)

Bases: ask_sdk_model_runtime.api_client_message.ApiClientMessage

Represents a request sent from Service Clients to an ask_sdk_model_runtime.api_client.ApiClient implementation.

Parameters:
  • headers (list[tuple[str, str]]) – List of header tuples
  • body (str) – Body of the message
  • url (str) – Url of the request
  • method (str) – Method called with the request

ask_sdk_model_runtime.api_client_response module

class ask_sdk_model_runtime.api_client_response.ApiClientResponse(headers=None, body=None, status_code=None)

Bases: ask_sdk_model_runtime.api_client_message.ApiClientMessage

Represents a response returned by ask_sdk_model_runtime.api_client.ApiClient implementation to a Service Client.

Parameters:
  • headers (list[tuple[str, str]]) – List of header tuples
  • body (str) – Body of the message
  • status_code (int) – Status code of the response

ask_sdk_model_runtime.api_configuration module

class ask_sdk_model_runtime.api_configuration.ApiConfiguration(serializer=None, api_client=None, authorization_value=None, api_endpoint=None)

Bases: object

Represents a class that provides API configuration options needed by service clients.

Parameters:

ask_sdk_model_runtime.api_response module

class ask_sdk_model_runtime.api_response.ApiResponse(headers=None, body=None, status_code=None)

Bases: object

Represents a response returned by the Service Client.

Parameters:
  • headers (list[tuple[str, str]]) – List of header tuples
  • body (object) – Body of the response
  • status_code (int) – Status code of the response

ask_sdk_model_runtime.authentication_configuration module

class ask_sdk_model_runtime.authentication_configuration.AuthenticationConfiguration(client_id=None, client_secret=None, refresh_token=None)

Bases: object

Represents a class that provides authentication configuration.

Parameters:
  • client_id (str) – Client ID required for authentication.
  • client_secret (str) – Client Secret required for authentication.
  • refresh_token (str) – Client refresh_token required to get access token for API calls.

ask_sdk_model_runtime.base_service_client module

class ask_sdk_model_runtime.base_service_client.BaseServiceClient(api_configuration)

Bases: object

Class to be used as the base class for the generated service clients.

The class has to be implemented by the service clients and this class instantiation is not supported

Parameters:api_configuration (ask_sdk_model_runtime.api_configuration.ApiConfiguration) – ApiConfiguration implementation
invoke(method, endpoint, path, query_params, header_params, path_params, response_definitions, body, response_type)

Calls the ApiClient based on the ServiceClient specific data provided as well as handles the well-known responses from the Api.

Parameters:
  • method (str) – Http method
  • endpoint – Base endpoint to make the request to
  • path (str) – Specific path to hit. It might contain variables to be interpolated with path_params
  • query_params (list(tuple(str, str))) – Parameter values to be sent as part of query string
  • header_params (list(tuple(str, str))) – Parameter values to be sent as headers
  • path_params (dict(str, str)) – Parameter values to be interpolated in the path
  • response_definitions (list(ask_sdk_model_runtime.service_client_response.ServiceClientResponse)) – Well-known expected responses by the ServiceClient
  • body (object) – Request body
  • response_type (class) – Type of the expected response if applicable
Returns:

ApiResponse object.

Return type:

ask_sdk_model_runtime.api_response.py

Raises:

ask_sdk_model_runtime.exceptions.ServiceException if service fails and ValueError if serializer or API Client is not configured in api_configuration # noqa: E501

ask_sdk_model_runtime.exceptions module

exception ask_sdk_model_runtime.exceptions.ApiClientException

Bases: Exception

Exception class for ApiClient Adapter processing.

exception ask_sdk_model_runtime.exceptions.SerializationException

Bases: Exception

Class for exceptions raised during serialization/deserialization.

exception ask_sdk_model_runtime.exceptions.ServiceException(message, status_code, headers, body)

Bases: Exception

Exception thrown by a Service client when an error response was received or some operation failed.

Parameters:
  • message (str) – Description of the error
  • status_code (int) – Status code of the HTTP Response
  • headers (list(tuple(str, str))) – Headers of the Http response that return the failure
  • body (object) – Body of the HTTP Response

ask_sdk_model_runtime.serializer module

class ask_sdk_model_runtime.serializer.DefaultSerializer

Bases: ask_sdk_model_runtime.serializer.Serializer

NATIVE_TYPES_MAPPING = {'bool': <class 'bool'>, 'date': <class 'datetime.date'>, 'datetime': <class 'datetime.datetime'>, 'float': <class 'float'>, 'int': <class 'int'>, 'long': <class 'int'>, 'object': <class 'object'>, 'str': <class 'str'>}
PRIMITIVE_TYPES = (<class 'float'>, <class 'bool'>, <class 'bytes'>, <class 'str'>, <class 'int'>)
deserialize(payload, obj_type)

Deserializes payload into an instance of provided obj_type.

The obj_type parameter can be a primitive type, a generic model object or a list / dict of model objects.

The list or dict object type has to be provided as a string format. For eg:

  • 'list[a.b.C]' if the payload is a list of instances of class a.b.C.
  • 'dict(str, a.b.C)' if the payload is a dict containing mappings of str : a.b.C class instance types.

The method looks for a deserialized_types dict in the model class, that mentions which payload values has to be deserialized. In case the payload key names are different than the model attribute names, the corresponding mapping can be provided in another special dict attribute_map. The model class should also have the __init__ method with default values for arguments. Check ask_sdk_model.request_envelope.RequestEnvelope source code for an example implementation.

Parameters:
  • payload (str) – data to be deserialized.
  • obj_type (Union[object, str]) – resolved class name for deserialized object
Returns:

deserialized object

Return type:

object

Raises:

ask_sdk_model_runtime.exceptions.SerializationException

serialize(obj)

Builds a serialized object.

  • If obj is None, return None.
  • If obj is str, int, long, float, bool, return directly.
  • If obj is datetime.datetime, datetime.date convert to string in iso8601 format.
  • If obj is list, serialize each element in the list.
  • If obj is dict, return the dict with serialized values.
  • If obj is ask sdk model, return the dict with keys resolved from the union of model’s attribute_map and deserialized_types and values serialized based on deserialized_types.
  • If obj is a generic class instance, return the dict with keys from instance’s deserialized_types and values serialized based on deserialized_types.
Parameters:obj (object) – The data to serialize.
Returns:The serialized form of data.
Return type:Union[Dict[str, Any], List, Tuple, str, int, float, bytes, None]
class ask_sdk_model_runtime.serializer.Serializer

Bases: object

Represents an abstract object used for Serialization tasks.

deserialize(payload, obj_type)

Deserializes the payload to object of provided obj_type.

Parameters:
  • payload (str) – String to deserialize
  • obj_type (object) – Target type of deserialization
Returns:

Deserialized object

Return type:

object

serialize(obj)

Serializes an object into a string.

Parameters:obj – object to serialize
Returns:serialized object in string format
Return type:str

ask_sdk_model_runtime.service_client_response module

class ask_sdk_model_runtime.service_client_response.ServiceClientResponse(response_type, status_code, message)

Bases: object

Represents a well-known response object by Service Client.

Parameters:
  • response_type (Response class) – Well-known representation of the response
  • status_code (int) – Status code to be attached to the response
  • message (str) – Message to be attached to the response