API Reference

Api

Custom flask_smorest.Api class.

class flask_smore_better.api.Api(app=None, *, spec_kwargs=None, config_prefix='')

Bases: Api

Custom flask_smorest.Api class.

This class is used to initialize the API and register the blueprints. It also initializes the blueprint plugins. This class is used instead of the default flask_smorest.Api class to allow for the use of the Blueprint class with plugins.

Note

Blueprint plugins must be registered before the API is initialized. See Blueprint for more information.

init_app(app, *, spec_kwargs=None)

Initialize Api with application

Parameters:

spec_kwargs – kwargs to pass to internal APISpec instance. Updates spec_kwargs passed in Api init.

Blueprint

Blueprint utility for Flask.

class flask_smore_better.blueprint.blueprint.Blueprint(name: str, import_name: str, **kwargs)

Subclass of flask_smorest.Blueprint that allows for the specification of plugins to add extra decorators to the blueprint. Use the plugins class attribute to specify the plugins to use.

Parameters:
  • name (str) – The name of the blueprint (used in api generation in angular)

  • import_name (str) – The name of the package or module to scan for routes

  • **kwargs – Extra keyword arguments to pass to the parent class (like description)

Example

from flask_smore_better.blueprint import Blueprint
from my_plugin import MyPlugin

Blueprint.plugins = [MyPlugin()]
# Now the blueprints may be created as normal
__getattr__(name: str) Callable

Return the decorator for a plugin if it exists.

plugins: list[PluginABC] = []

The list of plugins to use with the blueprint.

class flask_smore_better.blueprint.blueprint.BlueprintLoader(register_blueprint_method: Callable, blueprint_collector: BlueprintCollectorABC)

Bases: object

This class will load all blueprints in a given module and register them with the app.

Parameters:
  • register_blueprint_method (Callable) – The method to use to register the blueprint

  • blueprint_collector (BlueprintCollectorABC) – The blueprint collector to use

__call__(import_name: str) None

Register all blueprints in a package.

Parameters:

import_name (str) – The name of the module/package containing the blueprints

class flask_smore_better.blueprint.blueprint.DevBlueprintCollector

Bases: BlueprintCollectorABC

Blueprint collector that searches for blueprints in a given package/module.

__call__(import_name: str) list[Blueprint]

Search for blueprints in a given module/package and return a list of them.

Parameters:

import_name (str) – The name of the file to search. This should be the name of the module containing the blueprints. It will be used to determine the name of the cache file.

Returns:

The blueprints found in the module

Return type:

list[flask.Blueprint]

class flask_smore_better.blueprint.blueprint.PyCachedBlueprintCollector(cache_folder: str)

Bases: BlueprintCollectorABC

Blueprint collector that searches for a cache file that loads all blueprints in it.

Parameters:

cache_folder (str) – The folder where the cache file is located

__call__(import_name: str) list[Blueprint]

Return all blueprints in the BLUEPRINTS tuple in the cache file.

Parameters:

import_name (str) – The name of the module/package to search. This should be the name of the module or package containing the blueprints. It will be used to determine the name of the cache file.

Returns:

The blueprints found in the cache file

Return type:

list[flask.Blueprint]

classmethod generate_cache_module_name(import_name: str) str

Transform import name to module name of the cache file, without an extension.

Parameters:

import_name (str) – The name of the package to search

Returns:

The transformed module name of the cache file

Return type:

str

Plugin

Plugin ABCs for Flask Utility.

class flask_smore_better.blueprint.plugin.PluginABC

Bases: ABC

Abstract base class for blueprint plugins.

__decorator_name__: ClassVar[str]

The name of the decorator method to use the plugin on a route.

abstract __on_request__(*args, **kwargs) dict[str, Any]

Takes an action upon the request being received. This is called with positional and keyword arguments passed to the __decorator_func__ method.

Returns:

The data to pass to the route function as keyword arguments

Return type:

dict[str, Any]

abstract __process_wrapper_args__(*args, **kwargs) dict

When the decorator is called, this method will be called to determine what to merge into the _apidoc attribute of the decorated function. The overridden method can accept any arguments, but must return a dict.

Parameters:
  • *args – Extra positional arguments

  • **kwargs – Extra keyword arguments

Returns:

The dict to merge into the _apidoc attribute of the decorated function

Return type:

dict

api_init(api: Api) None

Initialize the plugin for the API. Typically, this will be used to add additional items to the API documentation that are necessary for the plugin.

By default, this will do nothing

Parameters:

api (Api) – The API object

Returns:

None

final decorator_func(*args, **kwargs) Callable

The decorator function to add to the blueprint. This method should not be overridden.

Parameters:
  • *args – Extra positional arguments

  • **kwargs – Extra keyword arguments

abstract prepare_apidocs(current_doc: dict, route_doc: dict, *, api: Api, spec: APISpec, **kwargs) dict

Prepare the API documentation for routes decorated with the plugin’s decorator. The returned dict will be used to deep update the API documentation for the route.

Parameters:
  • current_doc (dict) – The current documentation for the route

  • route_doc (dict) – The documentation for the route (IE: func._apidoc)

  • api (Api) – The API object

  • spec (APISpec) – The API spec; can be used to determine which OpenAPI version is being used

  • **kwargs – Extra keyword arguments, like method

classmethod update_apidoc(api: dict, callable_: Callable) None

Update the API documentation for a route callable.

Parameters:
  • api (dict) – The API documentation to update

  • callable (Callable) – The callable to update the API documentation for

Returns:

None

classmethod wraps_apidoc(wrapped: Callable, api: dict)

Update the API documentation for a route callable using a decorator. This decorator should be used in place of functools.wraps.

Parameters:
  • wrapped (Callable) – The callable to update the API documentation for

  • api (dict) – The API documentation to update

Returns:

The decorated callable

Return type:

Callable