Routes & Route Sets¶
-
class
routes.
Route
(method=None, view_func=None, rule=None, attribute=None, rel=None, title=None, description=None, schema=None, response_schema=None, format_response=True)¶ Routes are not bound to a specific resource and their schema, generated using
schema_factory()
can vary depending on the resource.If
view_func
has an__annotations__
attribute (a Python 3.x function annotation), the annotations will be used to generate therequest_schema
andresponse_schema
. The return annotation in this case is expected to be aschema.Schema
used for responses, and all other annotations are expected to be of typefields.Raw
and are combined into aschema.Fieldset
.-
relation
¶ A relation for the string, equal to
rel
if one was given.
-
request_schema
¶ request schema (not resource-bound)
-
response_schema
¶ response schema (not resource-bound)
-
@
METHOD
(rule=None, attribute=None, rel=None, title=None, description=None, schema=None, response_schema=None, format_response=True)¶ A decorator for registering the METHOD method handler of a route. Can be used with or without arguments and on both class and route instances. The
rule
andattribute
arguments are only available on the class.When used with an instance will add or replace the view function for the METHOD method of this
Route
with the decorated function; otherwise instantiates a newRoute
with the view function.This decorator is defined for the GET, PUT, POST, PATCH and DELETE methods.
Parameters: - rule (str) – (class-only) route URI relative to the resource, defaults to
/{attribute}
, replacing any'_'
(underscore) inattribute
with'-'
(dash). - attribute (str) – (class-only) attribute on the parent resource, used to identify the route internally; defaults to the attribute name of the decorated view function within the parent resource.
- rel (str) – relation of the method link to the resource
- title (str) – title of link schema
- description (str) – description of link schema
- schema (schema.Schema) – request schema
- response_schema (schema.Schema) – response schema
- format_response (bool) – whether the response should be converted using the response schema
- rule (str) – (class-only) route URI relative to the resource, defaults to
-
schema
¶ Used to get and set the request schema for the most recently decorated request method view function
-
response_schema
Used to get and set the response schema for the most recently decorated request method view function
-
method_links
¶ A dictionary mapping of method names (in upper case) to
routes.Link
objects containing the method view functions.
Parameters: - method (str) – a HTTP request method name (upper case)
- view_func (callable) – view function
- rule – url rule string or callable returning a string
- rel (str) – relation
- title (str) – title of schema
- description (str) – description of schema
- route (routes.Route) – route this link belongs to
- schema (schema.Schema) – request schema
- response_schema (schema.Schema) – response schema
- format_response (bool) – whether the response should be converted using the response schema
-
schema_factory
(resource)¶ Returns a link schema for a specific resource.
-
rule_factory
(resource, relative=False)¶ Returns a URL rule string for this route and resource.
Parameters: - resource (flask_potion.Resource) –
- relative (bool) – whether the rule should be relative to
resource.route_prefix
-
view_factory
(name, resource)¶ Returns a view function for all links within this route and resource.
Parameters: - name – Flask view name
- resource (flask_potion.Resource) –
-
-
class
routes.
ItemRoute
(method=None, view_func=None, rule=None, attribute=None, rel=None, title=None, description=None, schema=None, response_schema=None, format_response=True)¶ This route can be used with
flask_potion.ModelResource
. It is a simple extension overRoute
with the following adjustments:rule_factory()
is changed to prefix<{id_converter}:id>
with any rule.- It changes the implementation of
view_factory()
so that it passes the resolved resource item matching id as the first positional argument to the view function.
-
class
routes.
RouteSet
¶ An abstract class for combining related routes into one, which can also be used as a route factory.
-
class
routes.
Relation
(resource, backref=None, io='rw', attribute=None, **kwargs)¶ Used to define a relation to another
ModelResource
.
-
class
routes.
ItemAttributeRoute
(cls_or_instance, io=None, attribute=None)¶ Parameters: - cls_or_instance (fields.Raw) – a field class or instance
- attribute (str) – defaults to the field’s
attribute
attribute - io (str) –
r
,u
, orru
- defaults to the field’sio
attribute