apifrom.plugins

Plugin system for APIFromAnything.

This module provides a plugin system for extending the functionality of the APIFromAnything library.

Overview

Classes

Classes

LoggingPlugin(logger = None, level = logging.INFO, log_request_body = False, log_response_body = False, log_headers = False, exclude_paths = None, exclude_methods = None):bases: apifrom.plugins.base.Plugin

Plugin for logging requests and responses.

Initialize the logging plugin.

param logger:

The logger to use (defaults to a new logger)

param level:

The logging level

param log_request_body:

Whether to log request bodies

param log_response_body:

Whether to log response bodies

param log_headers:

Whether to log headers

param exclude_paths:

Paths to exclude from logging

param exclude_methods:

HTTP methods to exclude from logging

apifrom.plugins.initialize(api)

Initialize the plugin.

param api:

The API instance

apifrom.plugins.on_error(error, request)

Log an error.

param error:

The error that occurred

param request:

The request object

returns:

None

apifrom.plugins.on_shutdown()

Log server shutdown.

apifrom.plugins.on_startup()

Log server startup.

apifrom.plugins.post_response(response, request)

Log the response.

param response:

The response object

param request:

The request object

returns:

The response object

apifrom.plugins.pre_request(request)

Log the request.

param request:

The request object

returns:

The request object

Plugin:bases: abc.ABC

Base class for all plugins.

This class defines the interface that all plugins must implement, providing hooks for various stages of the API lifecycle.

Initialize the plugin.

apifrom.plugins.__str__()

Get a string representation of the plugin.

returns:

A string representation of the plugin

apifrom.plugins.activate()

Activate the plugin.

This method is called when the plugin is activated.

apifrom.plugins.deactivate()

Deactivate the plugin.

This method is called when the plugin is deactivated.

apifrom.plugins.get_config()

Get the configuration for this plugin.

returns:

The plugin configuration

apifrom.plugins.get_metadata()
:abstractmethod:

Get the metadata for this plugin.

returns:

The plugin metadata

apifrom.plugins.initialize(api)

Initialize the plugin.

This method is called when the plugin is registered with the API.

param api:

The API instance

apifrom.plugins.on_error(error, request)
:async:

Handle an error that occurred during request processing.

param error:

The error that occurred

param request:

The request object

returns:

A response object, or None to let the API handle the error

apifrom.plugins.on_event(event, **kwargs)
:async:

Handle an event emitted by the plugin system.

param event:

The event that occurred

param **kwargs:

Additional event data

apifrom.plugins.post_response(response, request)
:async:

Process a response after it is generated by the API.

param response:

The response object

param request:

The request object

returns:

The processed response object

apifrom.plugins.pre_request(request)
:async:

Process a request before it is handled by the API.

param request:

The request object

returns:

The processed request object

apifrom.plugins.register_hook(hook, callback, priority=PluginPriority.NORMAL.value)

Register a callback for a hook.

param hook:

The hook to register for

param callback:

The callback function

param priority:

The priority of the callback

apifrom.plugins.shutdown()

Shutdown the plugin.

This method is called when the API is shutting down.

apifrom.plugins.unregister_hook(hook, callback)

Unregister a callback from a hook.

param hook:

The hook to unregister from

param callback:

The callback function

class apifrom.plugins.PluginManager[source]

Manager for plugins.

This class manages the registration and execution of plugins, providing a robust and dynamic plugin system for extending the functionality of the API.

Initialize the plugin manager.

apifrom.plugins.__contains__(plugin_name)

Check if a plugin is registered.

param plugin_name:

The name of the plugin

returns:

True if the plugin is registered, False otherwise

apifrom.plugins.__iter__()

Iterate over all registered plugins.

returns:

An iterator over all registered plugins

apifrom.plugins.__len__()

Get the number of registered plugins.

returns:

The number of registered plugins

apifrom.plugins.emit_event(event, **kwargs)

Emit an event to all registered listeners.

param event:

The event to emit

param **kwargs:

Additional event data

apifrom.plugins.get_hook(name)

Get a hook by name.

param name:

The name of the hook

returns:

The hook instance, or None if not found

apifrom.plugins.get_plugin(plugin_name)

Get a plugin by name.

param plugin_name:

The name of the plugin

returns:

The plugin instance

raises ValueError:

If the plugin is not registered

apifrom.plugins.get_plugins_by_state(state)

Get plugins by state.

param state:

The state to filter by

returns:

A list of plugins in the specified state

apifrom.plugins.get_plugins_by_tag(tag)

Get plugins by tag.

param tag:

The tag to filter by

returns:

A list of plugins with the specified tag

apifrom.plugins.initialize(api)

Initialize the plugin manager with an API instance.

param api:

The API instance

apifrom.plugins.on_error(error, request)
:async:

Handle an error through all active plugins.

param error:

The error that occurred

param request:

The request object

returns:

A response object, or None if no plugin handled the error

apifrom.plugins.on_shutdown()

Call the on_shutdown method of all active plugins.

apifrom.plugins.on_startup()

Call the on_startup method of all active plugins.

apifrom.plugins.post_response(response, request)
:async:

Process a response through all active plugins.

param response:

The response object

param request:

The request object

returns:

The processed response object

apifrom.plugins.pre_request(request)
:async:

Process a request through all active plugins.

param request:

The request object

returns:

The processed request object

apifrom.plugins.register(plugin)

Register a plugin with the manager (alias for register_plugin).

param plugin:

The plugin to register

apifrom.plugins.register_event_listener(plugin, event, priority=PluginPriority.NORMAL.value)

Register a plugin as a listener for an event.

param plugin:

The plugin to register

param event:

The event to listen for

param priority:

The priority of the listener

apifrom.plugins.register_hook(name, description='')

Register a new hook.

param name:

The name of the hook

param description:

A description of the hook

returns:

The hook instance

apifrom.plugins.register_plugin(plugin)

Register a plugin with the manager.

param plugin:

The plugin to register

raises PluginDependencyError:

If a plugin dependency cannot be satisfied

raises PluginConfigurationError:

If the plugin configuration is invalid

apifrom.plugins.unregister(plugin_name)

Unregister a plugin from the manager (alias for unregister_plugin).

param plugin_name:

The name of the plugin to unregister

apifrom.plugins.unregister_event_listener(plugin, event)

Unregister a plugin as a listener for an event.

param plugin:

The plugin to unregister

param event:

The event to stop listening for

apifrom.plugins.unregister_plugin(plugin_name)

Unregister a plugin from the manager.

param plugin_name:

The name of the plugin to unregister

raises ValueError:

If the plugin is not registered

raises PluginDependencyError:

If other plugins depend on this plugin