Utils reference

Utils reference

Note

This section of the documentation is a reference of the cloudkitty.api.v2.utils module. It is generated from the docstrings of the functions. Please report any documentation bug you encounter on this page

class cloudkitty.api.v2.utils.SingleQueryParam(param_type)[source]

Voluptuous validator allowing to validate unique query parameters.

This validator checks that a URL query parameter is provided only once, verifies its type and returns it directly, instead of returning a list containing a single element.

Note that this validator uses voluptuous.Coerce internally and thus should not be used together with api_utils.get_string_type in python2.

Parameters:param_type – Type of the query parameter
cloudkitty.api.v2.utils.add_input_schema(location, schema)[source]

Add a voluptuous schema validation on a method’s input

Takes a dict which can be converted to a volptuous schema as parameter, and validates the parameters with this schema. The “location” parameter is used to specify the parameters’ location. Note that for query parameters, a MultiDict is returned by Flask. Thus, each dict key will contain a list. In order to ease interaction with unique query parameters, the SingleQueryParam voluptuous validator can be used:

from cloudkitty.api.v2 import utils as api_utils
@api_utils.add_input_schema('query', {
    voluptuous.Required('fruit'): api_utils.SingleQueryParam(str),
})
def put(self, fruit=None):
    return fruit

To accept a list of query parameters, the following syntax can be used:

from cloudkitty.api.v2 import utils as api_utils
@api_utils.add_input_schema('query', {
    voluptuous.Required('fruit'): [str],
})
def put(self, fruit=[]):
    for f in fruit:
        # Do something with the fruit
Parameters:
  • location (str) – Location of the args. Must be one of [‘body’, ‘query’]
  • schema (dict) – Schema to apply to the method’s kwargs
cloudkitty.api.v2.utils.paginated(func)[source]

Helper function for pagination.

Adds two parameters to the decorated function: * offset: int >=0. Defaults to 0. * limit: int >=1. Defaults to 100.

Example usage:

class Example(flask_restful.Resource):

    @api_utils.paginated
    @api_utils.add_output_schema({
        voluptuous.Required(
            'message',
            default='This is an example endpoint',
        ): api_utils.get_string_type(),
    })
    def get(self, offset=0, limit=100):
        # [...]
cloudkitty.api.v2.utils.add_output_schema(schema)[source]

Add a voluptuous schema validation on a method’s output

Example usage:

class Example(flask_restful.Resource):

    @api_utils.add_output_schema({
        voluptuous.Required(
            'message',
            default='This is an example endpoint',
        ): api_utils.get_string_type(),
    })
    def get(self):
        return {}
Parameters:schema (dict) – Schema to apply to the method’s output
cloudkitty.api.v2.utils.do_init(app, blueprint_name, resources)[source]

Registers a new Blueprint containing one or several resources to app.

Parameters:
  • app (flask.Flask) – Flask app in which the Blueprint should be registered
  • blueprint_name (str) – Name of the blueprint to create
  • resources (list of dicts matching cloudkitty.api.v2.RESOURCE_SCHEMA) – Resources to add to the Blueprint’s Api
cloudkitty.api.v2.utils.get_string_type()[source]

Returns basestring in python2 and str in python3.

Creative Commons Attribution 3.0 License

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.