Micro.Rest decorators¶
This is decorators for marking methods as REST endpoints.
See docs/decorators.rst for details.
Note
This topic describes convenience decorators defined by Micro.Rest module
The REST decorator¶
- class micro.rest.decorators.Rest(content_type=('application/json', ), swagger_instance=An instance of micro.rest.swag.Swagger, swagger_config={})[source]¶
Callable that provides validation, authentication and support for REST endpoints.
annotation: This decorator marks a request handler as REST API endpoint. - __init__(content_type=('application/json', ), swagger_instance=An instance of micro.rest.swag.Swagger, swagger_config={})[source]¶
Parameters: - content_type – Accepted content-type of REST API method, or iterable of accepted content types
- swagger_instance (micro.rest.swag.Swagger) – Instance of Swagger metadata aggregator
- swagger_config –
A dictionary conforming the following Orderly schema:
object { string operationId `This is an operation ID of the Swagger method`; union { integer;string; } expected_status `An expected status of response. May be specified like 'default', thus the schema given for 'default' keyword will be used by validation`; object {}* responses `The responses definitions of the object`; union { object {}*; null; } definitions `This is arbitrary additional definitions specified by the method. May be null`; string description `This is the description of the method`; union { array [object {}*]; null; } parameters `This is arbitrary parameters of the object. May be null`; union { string; null; } summary `This is a summary of the object. Defaults to description first 120 symbols, when null` };
If swagger_config parameter is not passed, the metadata for endpoint will be parsed from endpoint docstring, using REST endpoint declaration domain language
Returns:
Templated docstring decorator¶
- micro.rest.decorators.templated_docstring(**kwargs)¶
Create API server instance
Parameters: kwargs (dict) – Any dictionary-like object with keys that match the placeholders in the template Returns: Templated dosctring decorator instance, which will process the request handler docstring with its __call__ method Return type: micro.rest.decorators.TemplatedDocstring
Example usage:
from micro.web import Micro from micro.rest import rest, templated_dostring OUTPUT_NO_CONTENT = '204 `No Content`' app = Micro() @rest(content_type="application/octet-stream") @app.route("/") @templated_docstring(output=OUTPUT_NO_CONTENT) def main(): """ I am a simple method which accepts any input and returns 204 No Content @output $output """ return ""