Routes¶
Example¶
Adding routes follows the standard transmute pattern, with a decorator converting a function to an aiohttp route:
from sanic_transmute import describe, add_route
from sanic import Sanic
app = Sanic()
# define a GET endpoint, taking a query parameter integers left and right,
# which must be integers.
@describe(paths="/{name}")
async def multiply(request, name: str, left: int, right: int) -> int:
return left + right
# append to your route later
add_route(app, multiply)
the sanic request argument is supported: it will be passed into any function that has ‘request’ in it’s function signature.
see transmute-core:function for more information on customizing transmute routes.
API Documentation¶
-
sanic_transmute.
describe
(**kwargs)[source]¶ describe is a decorator to customize the rest API that transmute generates, such as choosing certain arguments to be query parameters or body parameters, or a different method.
Parameters: - paths (list(str)) – the path(s) for the handler to represent (using swagger’s syntax for a path)
- methods (list(str)) – the methods this function should respond to. if non is set, transmute defaults to a GET.
- query_parameters (list(str)) – the names of arguments that should be query parameters. By default, all arguments are query_or path parameters for a GET request.
- body_parameters (List[str] or str) –
the names of arguments that should be body parameters. By default, all arguments are either body or path parameters for a non-GET request.
in the case of a single string, the whole body is validated against a single object.
- header_parameters (list(str)) – the arguments that should be passed into the header.
- path_parameters (list(str)) – the arguments that are specified by the path. By default, arguments that are found in the path are used first before the query_parameters and body_parameters.
- parameter_descriptions (list(str)) – descriptions for each parameter, keyed by attribute name. this will appear in the swagger documentation.