Pluggable
class¶
This is the reference for the main object Pluggable
that contains all the parameters,
attributes and functions.
esmerald.Pluggable
¶
Pluggable(cls, **options)
The Pluggable
is used to create an Esmerald
pluggable from an Extension
.
When Esmerald receives pluggables, it hooks them into the system and allows
the access via anywhere in the application.
Read more about the Pluggables and learn how to use them.
Example
from typing import Optional
from loguru import logger
from pydantic import BaseModel
from esmerald import Esmerald, Extension, Pluggable
from esmerald.types import DictAny
class PluggableConfig(BaseModel):
name: str
class MyExtension(Extension):
def __init__(
self, app: Optional["Esmerald"] = None, config: PluggableConfig = None, **kwargs: "DictAny"
):
super().__init__(app, **kwargs)
self.app = app
def extend(self, config: PluggableConfig) -> None:
logger.success(f"Successfully passed a config {config.name}")
my_config = PluggableConfig(name="my extension")
pluggable = Pluggable(MyExtension, config=my_config)
app = Esmerald(routes=[], pluggables={"my-extension": pluggable})
PARAMETER | DESCRIPTION |
---|---|
cls |
TYPE:
|
**options |
TYPE:
|
Source code in esmerald/pluggables/base.py
55 56 57 |
|
esmerald.Extension
¶
Extension(app=None, **kwargs)
Bases: BaseExtension
Extension
object is the one being used to add the logic that will originate
the pluggable
in the application.
The Extension
must implement the extend
function.
Read more about the Extension and learn how to use it.
Example
from typing import Optional
from esmerald import Esmerald, Extension
from esmerald.types import DictAny
class MyExtension(Extension):
def __init__(self, app: Optional["Esmerald"] = None, **kwargs: "DictAny"):
super().__init__(app, **kwargs)
self.app = app
self.kwargs = kwargs
def extend(self, **kwargs: "DictAny") -> None:
'''
Function that should always be implemented when extending
the Extension class or a `NotImplementedError` is raised.
'''
# Do something here
PARAMETER | DESCRIPTION |
---|---|
app |
An
TYPE:
|
**kwargs |
Any additional kwargs needed.
TYPE:
|
Source code in esmerald/pluggables/base.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
extend
abstractmethod
¶
extend(**kwargs)
PARAMETER | DESCRIPTION |
---|---|
**kwargs |
TYPE:
|
Source code in esmerald/pluggables/base.py
93 94 95 |
|