apifrom.plugins =============== Plugin system for APIFromAnything. This module provides a plugin system for extending the functionality of the APIFromAnything library. .. py:currentmodule:: apifrom.plugins Overview -------- **Classes** * :py:class:`LoggingPlugin` * :py:class:`Plugin` * :py:class:`PluginManager` Classes ------- .. py:class:: 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 .. :: _name .. :: exclude_methods .. :: exclude_paths .. :: level .. :: log_headers .. :: log_request_body .. :: log_response_body .. :: logger Get the logger for this plugin. :returns: The plugin logger .. method:: initialize(api) Initialize the plugin. :param api: The API instance .. method:: on_error(error, request) Log an error. :param error: The error that occurred :param request: The request object :returns: None .. method:: on_shutdown() Log server shutdown. .. method:: on_startup() Log server startup. .. method:: post_response(response, request) Log the response. :param response: The response object :param request: The request object :returns: The response object .. method:: pre_request(request) Log the request. :param request: The request object :returns: The request object .. py:class:: 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. .. :: _api .. :: _config .. :: _logger .. :: _metadata .. :: _state .. method:: __str__() Get a string representation of the plugin. :returns: A string representation of the plugin .. method:: activate() Activate the plugin. This method is called when the plugin is activated. .. method:: deactivate() Deactivate the plugin. This method is called when the plugin is deactivated. .. method:: get_config() Get the configuration for this plugin. :returns: The plugin configuration .. method:: get_metadata() :abstractmethod: Get the metadata for this plugin. :returns: The plugin metadata .. method:: initialize(api) Initialize the plugin. This method is called when the plugin is registered with the API. :param api: The API instance .. method:: 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 .. method:: on_event(event, **kwargs) :async: Handle an event emitted by the plugin system. :param event: The event that occurred :param \*\*kwargs: Additional event data .. method:: 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 .. method:: pre_request(request) :async: Process a request before it is handled by the API. :param request: The request object :returns: The processed request object .. method:: 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 .. method:: shutdown() Shutdown the plugin. This method is called when the API is shutting down. .. method:: unregister_hook(hook, callback) Unregister a callback from a hook. :param hook: The hook to unregister from :param callback: The callback function .. py:class:: PluginManager 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. .. :: api :annotation: Optional[apifrom.core.app.API] .. :: event_listeners :annotation: Dict[PluginEvent, List[Tuple[Plugin, int]]] .. :: hooks :annotation: Dict[str, PluginHook] .. :: logger .. :: plugins :annotation: Dict[str, Plugin] .. method:: __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 .. method:: __iter__() Iterate over all registered plugins. :returns: An iterator over all registered plugins .. method:: __len__() Get the number of registered plugins. :returns: The number of registered plugins .. method:: emit_event(event, **kwargs) Emit an event to all registered listeners. :param event: The event to emit :param \*\*kwargs: Additional event data .. method:: get_hook(name) Get a hook by name. :param name: The name of the hook :returns: The hook instance, or None if not found .. method:: 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 .. method:: 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 .. method:: 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 .. method:: initialize(api) Initialize the plugin manager with an API instance. :param api: The API instance .. method:: 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 .. method:: on_shutdown() Call the on_shutdown method of all active plugins. .. method:: on_startup() Call the on_startup method of all active plugins. .. method:: 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 .. method:: pre_request(request) :async: Process a request through all active plugins. :param request: The request object :returns: The processed request object .. method:: register(plugin) Register a plugin with the manager (alias for register_plugin). :param plugin: The plugin to register .. method:: 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 .. method:: 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 .. method:: 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 .. method:: unregister(plugin_name) Unregister a plugin from the manager (alias for unregister_plugin). :param plugin_name: The name of the plugin to unregister .. method:: 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 .. method:: 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