canarieapi.utility_rest

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.

Module Contents

Classes

AnyIntConverter

Matches one of the items provided.

Functions

request_wants_json(→ bool)

Check if the request type is of type JSON considering both the Accept header and f query parameter.

set_html_as_default_response(→ None)

Set the default response Media-Type, with fallback to JSON.

get_config(→ JSON)

Return the config for the particular service/platform associated with the given route name.

validate_route(→ None)

Check if the route name is a value amongst known services/platforms in the configuration.

get_api_title(→ str)

Get the API title to be shown in rendered html page.

get_canarie_api_response(...)

Provide a valid response for the CANARIE API request based on the service route.

make_error_response(→ Tuple[Union[flask.Response, ...)

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

get_db(→ sqlite3.Connection)

Get a connection to an existing database.

init_db(→ None)

Initialize a database from a schema.

Attributes

APIType

_JSON

JSON

canarieapi.utility_rest.APIType[source]
canarieapi.utility_rest._JSON: typing_extensions.TypeAlias = 'JSON'[source]
canarieapi.utility_rest.JSON[source]
canarieapi.utility_rest.request_wants_json() bool[source]

Check if the request type is of type JSON considering both the Accept header and f query parameter.

The default Media-Type */* will be interpreted as JSON. Omitting a preferred type entirely will also default to JSON.

canarieapi.utility_rest.set_html_as_default_response() None[source]

Set the default response Media-Type, with fallback to JSON.

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. This is useful for automatically rendering HTML by web browsers that do not provide explicitly the desired mimetype.

canarieapi.utility_rest.get_config(route_name: str, api_type: APIType) JSON[source]

Return the config for the particular service/platform associated with the given route name.

Parameters
  • route_name – Route name of the service/platform coming from the URL e.g. : [‘pavics’, ‘node’, ‘bias’, etc.]

  • api_type – Api type of the route which must be one of platform or service

Raises

Exception if the route is unknown

canarieapi.utility_rest.validate_route(route_name: str, api_type: APIType) None[source]

Check if the route name is a value amongst known services/platforms in the configuration.

Parameters
  • route_name – Route name of the service/platform coming from the URL e.g. : [‘pavics’, ‘node’, ‘bias’, etc.]

  • api_type – Api type of the route which must be one of platform or service

Raises

Exception if the route is unknown

canarieapi.utility_rest.get_api_title(route_name: str, api_type: APIType) str[source]

Get the API title to be shown in rendered html page.

Parameters
  • route_name – Route name of the service/platform coming from the URL e.g. : [‘pavics’, ‘node’, ‘bias’, etc.]

  • api_type – Api type of the route which must be one of platform or service

Returns

An API title

canarieapi.utility_rest.get_canarie_api_response(route_name: str, api_type: APIType, api_request: str) flask.typing.ResponseReturnValue[source]

Provide a valid response for the CANARIE API request based on the service route.

Parameters
  • route_name – Route name of the service/platform coming from the URL e.g. : [‘pavics’, ‘node’, ‘bias’, etc.]

  • api_type – Api type of the route which must be one of platform or service

  • api_request – The request specified in the URL

Returns

A valid HTML response

canarieapi.utility_rest.make_error_response(http_status: Optional[int] = None, http_status_response: Optional[str] = None) Tuple[Union[flask.Response, str], int][source]

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

Parameters
  • http_status – HTTP status

  • http_status_response – Standard message associated with a status code. Obtained via http.client.responses if not provided.

canarieapi.utility_rest.get_db() sqlite3.Connection[source]

Get a connection to an existing database.

If the database does not exist, create a connection to local sqlite3 file. If the local sqlite3 file doesn’t exist, initialize it using a schema.

Stores the established connection in the application’s global context to reuse it whenever required.

canarieapi.utility_rest.init_db(database: sqlite3.Connection) None[source]

Initialize a database from a schema.

class canarieapi.utility_rest.AnyIntConverter(mapping: werkzeug.routing.Map, *items: Union[int, str])[source]

Bases: werkzeug.routing.BaseConverter

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.