Api

The Api class

class flask_restalchemy.Api(blueprint=None, request_decorators=None)[source]

Bases: object

Parameters:
  • blueprint ((Flask|Blueprint)) – Flask application or Blueprint
  • request_decorators (callable) – request decorators for this API object (see Flask-Restful decorators docs for more information)
add_model(model, url=None, serializer_class=None, view_name=None, request_decorators=None, methods=None, query_modifier=None)[source]

Create API endpoints for the given SQLAlchemy declarative class.

Parameters:
  • model (class) – the SQLAlchemy declarative class
  • url (string) – one or more url routes to match for the resource, standard flask routing rules apply. Defaults to model name in lower case.
  • view_name (string) – custom name for the collection endpoint url definition, if not set the model table name will be used
  • serializer_class (Type[ModelSerializer]) – If None, a default serializer will be created.
  • request_decorators (list|dict) – decorators to be applied to HTTP methods. Could be a list of decorators or a dict mapping HTTP method types to a list of decorators (dict keys should be ‘get’, ‘post’ or ‘put’).
  • methods (list) – A list with verbs to be used, if None, default will use all
  • query_modifier (callable) – function that returns a query and expects a model as parameter that should be used to create the query and expects a parent_query to be incremented with the callback query function. The method signature should look like this: query_callback(resource_model, parent_query)
add_property(property_type, model, property_name, url_rule=None, serializer_class=None, request_decorators=[], endpoint_name=None, methods=None, query_modifier=None)[source]
add_relation(relation_property, url_rule=None, serializer_class=None, request_decorators=None, endpoint_name=None, methods=None, query_modifier=None)[source]

Create API endpoints for the given SQLAlchemy relationship.

Parameters:
  • relation_property – model relationship representing the collection to receive the CRUD operations.
  • url_rule (string) – one or more url routes to match for the resource, standard flask routing rules apply. Defaults to model name in lower case.
  • serializer_class (Type[ModelSerializer]) – If None, a default serializer will be created.
  • request_decorators (list|dict) – decorators to be applied to HTTP methods. Could be a list of decorators or a dict mapping HTTP verb types to a list of decorators (dict keys should be ‘get’, ‘post’ or ‘put’). See https://flask-restful.readthedocs.io/en/latest/extending.html#resource-method-decorators for more details.
  • endpoint_name (string) – endpoint name (defaults to {model_collection_name}-{related_collection_name}-relation() Can be used to reference this route in fields.Url fields
  • methods (list) – A list with verbs to be used, if None, default will use all
  • query_modifier (callable) – function that returns a query and expects a model as parameter that should be used to create the query and expects a parent_query to be incremented with the callback query function. The method signature should look like this: query_callback(resource_model, parent_query)
add_resource(resource_class, url_rule, view_name, resource_init_args=(), resource_init_kwargs=None, decorators=None, methods=None)[source]
add_url_rule(rule, endpoint, view_func, methods=None, request_decorators=())[source]

This is almost the same as Flask.add_url_rule method, but with added support for decorators.

Parameters:
  • rule – the URL rule as string
  • endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint
  • view_func – the function to call when serving a request to the provided endpoint
  • methods (list[str]) – verbs to be accepted by view
  • request_decorators (list|dict) – decorators to be applied to HTTP methods. Could be a list of decorators or a dict mapping HTTP verb types to a list of decorators (dict keys should be ‘get’, ‘post’ or ‘put’). See https://flask-restful.readthedocs.io/en/latest/extending.html#resource-method-decorators for more details.
static create_default_serializer(model_class)[source]

Create a default serializer for the given SQLAlchemy declarative class. Recipe based on https://marshmallow-sqlalchemy.readthedocs.io/en/latest/recipes.html#automatically-generating-schemas-for-sqlalchemy-models

Parameters:model_class – the SQLAlchemy mapped class
Return type:class
get_db_session()[source]

ef register_view(self, view_func, url, pk=’id’, pk_type=’int’, methods=None): Returns an SQLAlchemy object session. Used by flask-restful Resources to access the database.

init_app(blueprint)[source]
classmethod register_column_serializer(serializer_class, predicate)[source]

Register a serializer for a given column to be used globally by ModelSerializers

Parameters:
  • serializer_class (Type[ColumnSerializer]) – the Serializer class
  • predicate (callable) – a function that receives a column type and returns True if the given serializer is valid for that column
register_view(view_func, url, pk='id', pk_type='int', methods=None)[source]

Configure URL rule as specified by HTTP verbs passed as parameter.

Parameters:
  • view_func – the function to call when serving a request to the provided endpoint
  • url – one or more url routes to match for the resource, standard flask routing rules apply. Defaults to model name in lower case.
  • pk – primary key
  • pk_type – primary key type
  • methods (list[str]) – verbs to be accepted by view
route(rule, endpoint=None, **kwargs)[source]

A decorator that is used to register a view function for a given URL rule. This does the same thing as add_url_rule() but is intended for decorator usage:

@api.route('/')
def index():
    return 'Hello World'
Parameters:
  • rule – the URL rule as string
  • endpoint – the endpoint for the registered URL rule. Uses the name of the view function by default
  • kwargs – the options to be forwarded to the underlying add_url_rule().