REST front-end utilities

This module is a collection of utility functions used mainly by the rest_route module and which are placed here to keep the rest_route module as clean as possible.

class VestaRestPackage.utility_rest.AnyIntConverter(mapping, *items)[source]

Matches one of the items provided.

Items must be integer and comma separated with a space to avoid confusion with floating point value in the parser.

For example:

1, 2, 3

And not:

1,2,3

Since it would parse as float 1,2 and 3 .

VestaRestPackage.utility_rest.async_call(fct, *args, **kwargs)[source]

Call AMQP functions with any arg or kwargs in an asynchronous manner.

Parameters:
  • fct – The function to call asynchronously
  • args – Arguments
  • kwargs – Keyword arguments
Returns:

The function output

Raises:

AMQPError if a timeout occurs

VestaRestPackage.utility_rest.get_canarie_api_response(service_route, canarie_api_request)[source]

Provide a valid HTML response for the CANARIE API request based on the service_route.

Parameters:
  • service_route – Route name of the service coming from the URL e.g. : [‘diarisation’, ‘stt’, etc.]
  • canarie_api_request – The request specified in the URL
Returns:

A valid HTML response

VestaRestPackage.utility_rest.get_db(name)[source]

Get a connection to an existing database. If it does not exist, create a connection to local sqlite3 file.

If the local sqlite3 file doesn’t exist, initialize it using a schema.

VestaRestPackage.utility_rest.init_db(database, name)[source]

Initialize a database from a schema

VestaRestPackage.utility_rest.log_request(service_name, url)[source]

Log an invocation into the DB

Parameters:
  • service_name – service to which a request has been made
  • url – URL used to access API
VestaRestPackage.utility_rest.make_error_response(html_status=None, html_status_response=None, vesta_exception=None)[source]

Make an error response based on the request type and given information.

Parameters:
  • html_status – HTML status, if not provided it will be extracted from the vesta_exception (at least one of html_status or vesta_exception must be provided)
  • html_status_response – Standard message associated with a status code. Obtained via httplib.responses if not provided.
  • vesta_exception – Vesta exception instance used to obtain an exception code. Generic one is used if not provided.
VestaRestPackage.utility_rest.request_wants_json()[source]

Check if the request type is of type JSON.

The default mimetype / is interpreted as JSON.

VestaRestPackage.utility_rest.request_wants_xml()[source]

Check if the request type is of type XML.

The default mimetype / is interpreted as JSON.

VestaRestPackage.utility_rest.set_html_as_default_response()[source]

By default if the accepted mimetypes contains /, JSON format will be used.

By calling this function, the / mimetype will be changed explicitly into text/html so that it becomes the mimetype used by default.

VestaRestPackage.utility_rest.store_uuid(uuid, service_name)[source]

Store a UUID so it can be validated later.

Parameters:
  • uuid (Unicode) – UUID of a given request
  • service_name (string) – Name of the service which is requested.
VestaRestPackage.utility_rest.submit_task(storage_doc_id, task_name, service_route='.', **extra_params)[source]

Submit a task to a worker.

Parameters:
  • storage_doc_id – The document ID for which a task should be run.
  • task_name – The task name for logging purposes
  • service_route – service route to obtain the requested service name
  • extra_params – Extra parameters that are passed to send_task_request
Returns:

JSON object with the task UUID or error response.

Raises:

MissingParameterError

VestaRestPackage.utility_rest.uuid_task(task, service_route='.')[source]

Get the status or cancel a task identified by a UUID.

Parameters:
  • task – status or cancel
  • service_route – service route to obtain the requested service name
Returns:

JSON object with latest status or error response.

Raises:

MissingParameterError

VestaRestPackage.utility_rest.validate_service_route(service_route)[source]

Check if service name is a value amongst known services in the configuration.

Parameters:service_route – Route name of the service coming from the URL e.g.: [‘diarisation’, ‘stt’, etc.]
Returns:Service name associated with the route
Raises:UnknownServiceError
VestaRestPackage.utility_rest.validate_state(uuid, service_name, state)[source]

Validate the state of a given task.

  • Check that the activity flag is True
    if the task has leave the PENDING status
  • Check that the worker version in the payload fit the configuration one
Parameters:
  • uuid (Unicode) – UUID of a given request
  • service_name (string) – Name of the service which is requested.
  • state (Dictionary containing task status) – The state of the task
Raises:

VersionMismatchError in case of a version mismatch

VestaRestPackage.utility_rest.validate_url(url)[source]

Check if URL is invalid.

VestaRestPackage.utility_rest.validate_uuid(uuid, service_name)[source]

Validate UUID existence for a given service.

Parameters:
  • uuid (Unicode) – UUID of a given request
  • service_name (string) – Name of the service which is requested.
Raises:

UnknownUUIDError in case that the UUID isn’t owned by the given service.