-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- fix pluggables logic, pluggables contains now extensions and convert pluggables on the fly - unrestrict pluggables, assigning extensions instances and classes is now possible too. The extend logic is now executed automatically
- Loading branch information
Showing
10 changed files
with
196 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import Optional | ||
|
||
from loguru import logger | ||
|
||
from esmerald import Esmerald, Extension, Gateway, JSONResponse, Request, get | ||
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 like print a log or whatever you need | ||
logger.success("Started the extension manually") | ||
|
||
# Add the extension to the pluggables of Esmerald | ||
# And make it accessible | ||
self.app.add_pluggable("my-extension", self) | ||
|
||
|
||
@get("/home") | ||
async def home(request: Request) -> JSONResponse: | ||
""" | ||
Returns a list of pluggables of the system. | ||
"pluggables": ["my-extension"] | ||
""" | ||
pluggables = list(request.app.pluggables) | ||
|
||
return JSONResponse({"pluggables": pluggables}) | ||
|
||
|
||
app = Esmerald(routes=[Gateway(handler=home)]) | ||
|
||
extension = MyExtension(app=app) | ||
extension.extend() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from .base import Extension, Pluggable | ||
from .base import Extension, ExtensionDict, Pluggable | ||
|
||
__all__ = ["Pluggable", "Extension"] | ||
__all__ = ["Pluggable", "Extension", "ExtensionDict"] |
Oops, something went wrong.