apifrom.plugins.baseο
Base plugin module for APIFromAnything.
This module provides the base classes and interfaces for creating plugins for the APIFromAnything library. The plugin system is designed to be robust, dynamic, and feature-rich, allowing for extensive customization and extension of the core functionality.
Overviewο
Classes
PluginPluginEventPluginHookPluginPriorityPluginState
Classesο
- class apifrom.plugins.base.API(debug=False, title='APIFromAnything API', description='API created with APIFromAnything', version='1.0.0', docs_url='/docs', openapi_config=None, swagger_ui_config=None, enable_docs=True)[source]ο
- Parameters:
debug (bool)
title (str)
description (str)
version (str)
docs_url (str)
openapi_config (apifrom.docs.openapi.OpenAPIConfig | None)
swagger_ui_config (apifrom.docs.swagger_ui.SwaggerUIConfig | None)
enable_docs (bool)
Main application container for APIFromAnything.
This class serves as the central registry for all API endpoints, middleware, and configuration. It provides methods for registering endpoints, middleware, and starting the server.
Initialize a new API instance.
- param debug:
Whether to run in debug mode.
- param title:
The title of the API.
- param description:
The description of the API.
- param version:
The version of the API.
- param docs_url:
The URL for the API documentation.
- param openapi_config:
Configuration for OpenAPI documentation.
- param swagger_ui_config:
Configuration for Swagger UI.
- param enable_docs:
Whether to enable API documentation.
- 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.base.__str__()ο
Get a string representation of the plugin.
- returns:
A string representation of the plugin
- apifrom.plugins.base.activate()ο
Activate the plugin.
This method is called when the plugin is activated.
- apifrom.plugins.base.deactivate()ο
Deactivate the plugin.
This method is called when the plugin is deactivated.
- apifrom.plugins.base.get_config()ο
Get the configuration for this plugin.
- returns:
The plugin configuration
- apifrom.plugins.base.get_metadata()ο
- :abstractmethod:
Get the metadata for this plugin.
- returns:
The plugin metadata
- apifrom.plugins.base.initialize(api)ο
Initialize the plugin.
This method is called when the plugin is registered with the API.
- param api:
The API instance
- apifrom.plugins.base.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.base.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.base.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.base.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.base.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.base.shutdown()ο
Shutdown the plugin.
This method is called when the API is shutting down.
- apifrom.plugins.base.unregister_hook(hook, callback)ο
Unregister a callback from a hook.
- param hook:
The hook to unregister from
- param callback:
The callback function
Configuration for a plugin.
This class stores configuration options for a plugin, which can be set by the user and accessed by the plugin.
Initialize the plugin configuration.
- param defaults:
Default values for configuration options
- param schema:
JSON Schema for validating configuration options
- apifrom.plugins.base.get(key, default=None)ο
Get a configuration value.
- param key:
The configuration key
- param default:
The default value to return if the key is not found
- returns:
The configuration value
- apifrom.plugins.base.set(key, value)ο
Set a configuration value.
- param key:
The configuration key
- param value:
The configuration value
- apifrom.plugins.base.update(values)ο
Update multiple configuration values.
- param values:
A dictionary of configuration values to update
- apifrom.plugins.base.validate()ο
Validate the configuration against the schema.
- returns:
True if the configuration is valid, False otherwise
- PluginEvent:bases: enum.Enum
Events that can be emitted by the plugin system.
This enum defines the events that can be emitted by the plugin system, which plugins can listen for and respond to.
- PluginHook(name, description = ''):bases: Generic[T]
A hook that plugins can register callbacks for.
This class provides a way for plugins to register callbacks for specific hooks in the API lifecycle, allowing them to extend or modify the behavior of the API at various points.
Initialize the plugin hook.
- param name:
The name of the hook
- param description:
A description of the hook
- apifrom.plugins.base.__call__(*args, **kwargs)ο
- :async:
Call all registered callbacks with the given arguments.
- param *args:
Positional arguments to pass to the callbacks
- param **kwargs:
Keyword arguments to pass to the callbacks
- returns:
A list of the results from all callbacks
- apifrom.plugins.base.register(callback, priority=PluginPriority.NORMAL.value)ο
Register a callback for this hook.
- param callback:
The callback function
- param priority:
The priority of the callback (higher priority callbacks are executed first)
- apifrom.plugins.base.unregister(callback)ο
Unregister a callback from this hook.
- param callback:
The callback function to unregister
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.base.__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.base.__iter__()ο
Iterate over all registered plugins.
- returns:
An iterator over all registered plugins
- apifrom.plugins.base.__len__()ο
Get the number of registered plugins.
- returns:
The number of registered plugins
- apifrom.plugins.base.emit_event(event, **kwargs)ο
Emit an event to all registered listeners.
- param event:
The event to emit
- param **kwargs:
Additional event data
- apifrom.plugins.base.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.base.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.base.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.base.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.base.initialize(api)ο
Initialize the plugin manager with an API instance.
- param api:
The API instance
- apifrom.plugins.base.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.base.on_shutdown()ο
Call the on_shutdown method of all active plugins.
- apifrom.plugins.base.on_startup()ο
Call the on_startup method of all active plugins.
- apifrom.plugins.base.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.base.pre_request(request)ο
- :async:
Process a request through all active plugins.
- param request:
The request object
- returns:
The processed request object
- apifrom.plugins.base.register(plugin)ο
Register a plugin with the manager (alias for register_plugin).
- param plugin:
The plugin to register
- apifrom.plugins.base.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.base.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.base.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.base.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.base.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.base.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
- class apifrom.plugins.base.PluginMetadata(name, version, description='', author='', website='', license='', dependencies=None, tags=None)[source]ο
Metadata for a plugin.
This class stores metadata about a plugin, such as its name, version, description, and dependencies.
Initialize the plugin metadata.
- param name:
The name of the plugin
- param version:
The version of the plugin
- param description:
A description of the plugin
- param author:
The author of the plugin
- param website:
The website of the plugin
- param license:
The license of the plugin
- param dependencies:
A list of plugin names that this plugin depends on
- param tags:
A list of tags for the plugin
- PluginPriority:bases: enum.Enum
Priority levels for plugins.
This enum defines the priority levels for plugins, which determine the order in which plugins are executed. Higher priority plugins are executed first.
- PluginState:bases: enum.Enum
States for plugins.
This enum defines the possible states for plugins, which are used to track the lifecycle of a plugin.
- class apifrom.plugins.base.Request(request=None, path_params=None, method=None, path=None, query_params=None, headers=None, body=None, client_ip=None)[source]ο
Request class for APIFromAnything.
This class wraps a Starlette request and provides methods for accessing request data in a convenient way.
- apifrom.plugins.base._requestο
The underlying Starlette request.
- apifrom.plugins.base.path_paramsο
Path parameters extracted from the URL.
- apifrom.plugins.base.query_paramsο
Query parameters extracted from the URL.
- apifrom.plugins.base.headersο
HTTP headers.
- apifrom.plugins.base.methodο
HTTP method.
- apifrom.plugins.base.pathο
Request path.
- apifrom.plugins.base._bodyο
Cached request body.
Initialize a new Request instance.
- param request:
The underlying Starlette request.
- param path_params:
Path parameters extracted from the URL.
- param method:
The HTTP method.
- param path:
The request path.
- param query_params:
Query parameters.
- param headers:
HTTP headers.
- param body:
Request body.
- param client_ip:
Client IP address.
- class apifrom.plugins.base.Response(content=None, status_code=200, headers=None, content_type='application/json')[source]ο
Response class for APIFromAnything.
This class represents an HTTP response and provides methods for setting response data, status code, and headers.
- apifrom.plugins.base.contentο
The response content.
- apifrom.plugins.base.status_codeο
The HTTP status code.
- apifrom.plugins.base.headersο
HTTP headers.
- apifrom.plugins.base.content_typeο
The content type of the response.
Initialize a new Response instance.
- param content:
The response content.
- param status_code:
The HTTP status code.
- param headers:
HTTP headers.
- param content_type:
The content type of the response.