Plugins

Plugins provide the functional aspects of the plugin server. These classes provide the code to handle routers. These routes must return immediatelt, however, async tasks can be started to handle background operations and the plugin can be queried later for results. The plugin should return JSON web responses, serialized JSON data, dicts, lists, or strings. These types, or their attributes, must be serializable to JSON.

Plugins should be based on plugincore.BasePlugin. This base class handles initialization and basic task handling so the plugin doesn’t have to manage that aspect of its operation.

The BasePlugin class should be subclassed for plugins as it sets up global variables and provides utility methods.

BasePlugin

BasePlugin has a few required and optional kwargs:

kwarg

Required

Usage

config

This is the global configuration object

route_path

*

Optionally set the route path

auth_type

*

Set the plugin auth type, values are global or plugin

(*) denotes parameters passed via the config.plugin_parms.<plugin module> seetting in the configuration.

Request Handling with BasePlugin

When a request for a route is made, the BasePlugin handle_request method is called, it is given thw request variables are passed to handle_request in the **args parameter. The BasePlugin method _check_auth is called, and, if auth is enabled, checks the apikey argument against the configured apikey. Once these checks are made, the subclass’s request_handler method is called. This method is also called with the same aguments as BasePlugin.handle_reqest. This method should return a str, list or dict containing the results of this request. It is vital that this data is JSON serializable.

Once the request is completed it is handed back to the server’s top level route handler which checks to see if CORS is enabled, and, if so checks the ACL and updates the response headers to send the proper CORS headers.

Arguments to rquest_handler

The request_handler method takes a single **args which is a dict-like object. This object contains key,value pairs for each of the GET and/or POST values submitted as part of the request, if any. Additionally, the request headers are in args[‘request_headers’]. This gives a lot of flexibility to passing data to the plugin.

Example Plugin

This is a ‘bare-bones’ plugin that will run as example.py, and it’s route will be /example.

from plugincore.baseplugin import BasePlugin

class Example(BasePlugin):

    def request_hanlder(**args):
        return {'example': 'Hi from Example.py'}

As you can see, this is very simple. If the plugin is configured for authentication the base class takes care of that and the aiohttp app class takes care of the transport. There’s no need to worry about handling the transport between client and server.