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

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:

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.

apifrom.plugins.base.router

The router instance for managing routes.

Type:

Router

apifrom.plugins.base.middleware

List of middleware instances.

Type:

list

apifrom.plugins.base.debug

Whether to run in debug mode.

Type:

bool

apifrom.plugins.base.title

The title of the API.

Type:

str

apifrom.plugins.base.description

The description of the API.

Type:

str

apifrom.plugins.base.version

The version of the API.

Type:

str

apifrom.plugins.base.docs_url

The URL for the API documentation.

Type:

str

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

class apifrom.plugins.base.PluginConfig(defaults=None, schema=None)[source]
Parameters:
  • defaults (Dict[str, Any])

  • schema (Dict[str, Any])

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

class apifrom.plugins.base.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.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]
Parameters:
  • name (str)

  • version (str)

  • description (str)

  • author (str)

  • website (str)

  • license (str)

  • dependencies (List[str])

  • tags (List[str])

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]
Parameters:
  • request (Optional[starlette.requests.Request])

  • path_params (Optional[dict[Any, Any]])

  • method (Optional[str])

  • path (Optional[str])

  • query_params (Optional[dict[Any, Any]])

  • headers (Optional[dict[Any, Any]])

  • body (Optional[Union[str, bytes]])

  • client_ip (Optional[str])

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]
Parameters:
  • content (Any)

  • status_code (int)

  • headers (Optional[Dict[str, str]])

  • content_type (str)

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.