fastapi-factory-utilities 0.5.0__py3-none-any.whl → 0.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of fastapi-factory-utilities might be problematic. Click here for more details.
- fastapi_factory_utilities/core/api/v1/sys/health.py +1 -1
- fastapi_factory_utilities/core/app/application.py +22 -26
- fastapi_factory_utilities/core/app/builder.py +8 -32
- fastapi_factory_utilities/core/app/fastapi_builder.py +3 -3
- fastapi_factory_utilities/core/plugins/__init__.py +2 -31
- fastapi_factory_utilities/core/plugins/abstracts.py +40 -0
- fastapi_factory_utilities/core/plugins/aiopika/__init__.py +23 -0
- fastapi_factory_utilities/core/plugins/aiopika/abstract.py +48 -0
- fastapi_factory_utilities/core/plugins/aiopika/configs.py +85 -0
- fastapi_factory_utilities/core/plugins/aiopika/exceptions.py +29 -0
- fastapi_factory_utilities/core/plugins/aiopika/exchange.py +70 -0
- fastapi_factory_utilities/core/plugins/aiopika/listener/__init__.py +7 -0
- fastapi_factory_utilities/core/plugins/aiopika/listener/abstract.py +72 -0
- fastapi_factory_utilities/core/plugins/aiopika/message.py +86 -0
- fastapi_factory_utilities/core/plugins/aiopika/plugins.py +63 -0
- fastapi_factory_utilities/core/plugins/aiopika/publisher/__init__.py +7 -0
- fastapi_factory_utilities/core/plugins/aiopika/publisher/abstract.py +66 -0
- fastapi_factory_utilities/core/plugins/aiopika/queue.py +86 -0
- fastapi_factory_utilities/core/plugins/odm_plugin/__init__.py +11 -156
- fastapi_factory_utilities/core/plugins/odm_plugin/builder.py +1 -1
- fastapi_factory_utilities/core/plugins/odm_plugin/configs.py +1 -1
- fastapi_factory_utilities/core/plugins/odm_plugin/documents.py +1 -1
- fastapi_factory_utilities/core/plugins/odm_plugin/plugins.py +155 -0
- fastapi_factory_utilities/core/plugins/opentelemetry_plugin/__init__.py +8 -121
- fastapi_factory_utilities/core/plugins/opentelemetry_plugin/instruments/__init__.py +85 -0
- fastapi_factory_utilities/core/plugins/opentelemetry_plugin/plugins.py +137 -0
- fastapi_factory_utilities/core/protocols.py +1 -54
- fastapi_factory_utilities/core/security/jwt.py +3 -3
- fastapi_factory_utilities/example/app.py +13 -4
- {fastapi_factory_utilities-0.5.0.dist-info → fastapi_factory_utilities-0.6.0.dist-info}/METADATA +4 -2
- {fastapi_factory_utilities-0.5.0.dist-info → fastapi_factory_utilities-0.6.0.dist-info}/RECORD +34 -23
- fastapi_factory_utilities/core/app/plugin_manager/__init__.py +0 -15
- fastapi_factory_utilities/core/app/plugin_manager/exceptions.py +0 -33
- fastapi_factory_utilities/core/app/plugin_manager/plugin_manager.py +0 -189
- fastapi_factory_utilities/core/plugins/example/__init__.py +0 -31
- fastapi_factory_utilities/core/plugins/httpx_plugin/__init__.py +0 -31
- {fastapi_factory_utilities-0.5.0.dist-info → fastapi_factory_utilities-0.6.0.dist-info}/WHEEL +0 -0
- {fastapi_factory_utilities-0.5.0.dist-info → fastapi_factory_utilities-0.6.0.dist-info}/entry_points.txt +0 -0
- {fastapi_factory_utilities-0.5.0.dist-info → fastapi_factory_utilities-0.6.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
"""Plugin manager.
|
|
2
|
-
|
|
3
|
-
This module provides the plugin manager for the application.
|
|
4
|
-
|
|
5
|
-
Objective:
|
|
6
|
-
- Provide a way to manage plugins for the application.
|
|
7
|
-
- The plugins are activated based on the configuration.
|
|
8
|
-
- The plugins are checked for pre-conditions.
|
|
9
|
-
- The plugins are loaded on application load.
|
|
10
|
-
- The plugins are started on application startup.
|
|
11
|
-
- The plugins are stopped on application shutdown.
|
|
12
|
-
"""
|
|
13
|
-
|
|
14
|
-
from importlib import import_module
|
|
15
|
-
from types import ModuleType
|
|
16
|
-
from typing import Self
|
|
17
|
-
|
|
18
|
-
from fastapi_factory_utilities.core.plugins import PluginsEnum, PluginState
|
|
19
|
-
from fastapi_factory_utilities.core.protocols import (
|
|
20
|
-
ApplicationAbstractProtocol,
|
|
21
|
-
PluginProtocol,
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
from .exceptions import InvalidPluginError, PluginPreConditionNotMetError
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
class PluginManager:
|
|
28
|
-
"""Plugin manager for the application."""
|
|
29
|
-
|
|
30
|
-
DEFAULT_PLUGIN_PACKAGE: str = "fastapi_factory_utilities.core.plugins"
|
|
31
|
-
|
|
32
|
-
@classmethod
|
|
33
|
-
def _import_module(cls, name: str, package: str) -> ModuleType:
|
|
34
|
-
"""Import the module. This is a wrapper around the import_module function to be able to mock it in the tests.
|
|
35
|
-
|
|
36
|
-
Args:
|
|
37
|
-
name (str): The name of the module.
|
|
38
|
-
package (str): The package of the module.
|
|
39
|
-
|
|
40
|
-
Returns:
|
|
41
|
-
ModuleType: The module.
|
|
42
|
-
|
|
43
|
-
Raises:
|
|
44
|
-
ImportError: If the module cannot be imported.
|
|
45
|
-
ModuleNotFoundError: If the module is not found.
|
|
46
|
-
"""
|
|
47
|
-
return import_module(name=f"{package}.{name}")
|
|
48
|
-
|
|
49
|
-
@classmethod
|
|
50
|
-
def _check_pre_conditions(
|
|
51
|
-
cls, plugin_package: str, want_to_activate_plugins: list[PluginsEnum], application: ApplicationAbstractProtocol
|
|
52
|
-
) -> list[PluginProtocol]:
|
|
53
|
-
"""Check the pre-conditions for the plugins.
|
|
54
|
-
|
|
55
|
-
Args:
|
|
56
|
-
plugin_package (str): The package for the plugins.
|
|
57
|
-
want_to_activate_plugins (list[PluginsEnum]): The plugins to activate.
|
|
58
|
-
application (BaseApplicationProtocol): The application.
|
|
59
|
-
|
|
60
|
-
Returns:
|
|
61
|
-
list[PluginProtocol]: The activated plugins.
|
|
62
|
-
|
|
63
|
-
Raises:
|
|
64
|
-
InvalidPluginError: If the plugin is invalid.
|
|
65
|
-
PluginPreConditionNotMetError: If the pre-conditions are not met.
|
|
66
|
-
"""
|
|
67
|
-
plugins: list[PluginProtocol] = []
|
|
68
|
-
|
|
69
|
-
for plugin_enum in want_to_activate_plugins:
|
|
70
|
-
try:
|
|
71
|
-
# Using a custom import function to be able to mock it in the tests.
|
|
72
|
-
plugin_module: ModuleType = cls._import_module(name=plugin_enum.value, package=plugin_package)
|
|
73
|
-
except (ImportError, ModuleNotFoundError) as import_error:
|
|
74
|
-
# TODO: Later, if we introduce extra mecanism to manage dependencies,
|
|
75
|
-
# we must handle a part of the error here. To be able to provide a better error message.
|
|
76
|
-
# For now, we just raise the error.
|
|
77
|
-
raise InvalidPluginError(
|
|
78
|
-
plugin_name=plugin_enum.value, message="Error importing the plugin "
|
|
79
|
-
) from import_error
|
|
80
|
-
|
|
81
|
-
if not isinstance(plugin_module, PluginProtocol):
|
|
82
|
-
raise InvalidPluginError(
|
|
83
|
-
plugin_name=plugin_enum.value, message="The plugin does not implement the PluginProtocol"
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
if not plugin_module.pre_conditions_check(application=application):
|
|
87
|
-
raise PluginPreConditionNotMetError(
|
|
88
|
-
plugin_name=plugin_enum.value, message="The pre-conditions are not met"
|
|
89
|
-
)
|
|
90
|
-
|
|
91
|
-
plugins.append(plugin_module)
|
|
92
|
-
|
|
93
|
-
return plugins
|
|
94
|
-
|
|
95
|
-
def __init__(self, activated: list[PluginsEnum] | None = None, plugin_package: str | None = None) -> None:
|
|
96
|
-
"""Instanciate the plugin manager."""
|
|
97
|
-
self._plugin_package: str = plugin_package or self.DEFAULT_PLUGIN_PACKAGE
|
|
98
|
-
self._plugins_wanted_to_be_activated: list[PluginsEnum] = activated or []
|
|
99
|
-
self._activated_plugins: list[PluginProtocol] = []
|
|
100
|
-
self._states: list[PluginState] = []
|
|
101
|
-
|
|
102
|
-
def activate(self, plugin: PluginsEnum) -> Self:
|
|
103
|
-
"""Activate a plugin.
|
|
104
|
-
|
|
105
|
-
Args:
|
|
106
|
-
plugin (PluginsEnum): The plugin to activate.
|
|
107
|
-
|
|
108
|
-
Returns:
|
|
109
|
-
Self: The plugin manager.
|
|
110
|
-
"""
|
|
111
|
-
self._plugins_wanted_to_be_activated.append(plugin)
|
|
112
|
-
|
|
113
|
-
return self
|
|
114
|
-
|
|
115
|
-
@property
|
|
116
|
-
def states(self) -> list[PluginState]:
|
|
117
|
-
"""Get the states."""
|
|
118
|
-
return self._states
|
|
119
|
-
|
|
120
|
-
def add_application_context(self, application: ApplicationAbstractProtocol) -> Self:
|
|
121
|
-
"""Add the application context to the plugins.
|
|
122
|
-
|
|
123
|
-
Args:
|
|
124
|
-
application (BaseApplicationProtocol): The application.
|
|
125
|
-
|
|
126
|
-
Returns:
|
|
127
|
-
Self: The plugin manager.
|
|
128
|
-
"""
|
|
129
|
-
self._application: ApplicationAbstractProtocol = application
|
|
130
|
-
|
|
131
|
-
return self
|
|
132
|
-
|
|
133
|
-
def add_states(self, states: list[PluginState]) -> Self:
|
|
134
|
-
"""Add the states to the plugin manager.
|
|
135
|
-
|
|
136
|
-
Args:
|
|
137
|
-
states (list[PluginState]): The states.
|
|
138
|
-
|
|
139
|
-
Returns:
|
|
140
|
-
Self: The plugin manager.
|
|
141
|
-
"""
|
|
142
|
-
for state in states:
|
|
143
|
-
self._states.append(state)
|
|
144
|
-
return self
|
|
145
|
-
|
|
146
|
-
def clear_states(self) -> Self:
|
|
147
|
-
"""Clear the states."""
|
|
148
|
-
self._states = []
|
|
149
|
-
return self
|
|
150
|
-
|
|
151
|
-
def load(self) -> Self:
|
|
152
|
-
"""Load the plugins.
|
|
153
|
-
|
|
154
|
-
Returns:
|
|
155
|
-
Self: The plugin manager.
|
|
156
|
-
|
|
157
|
-
Raises:
|
|
158
|
-
InvalidPluginError: If the plugin is invalid.
|
|
159
|
-
PluginPreConditionNotMetError: If the pre-conditions are not met.
|
|
160
|
-
|
|
161
|
-
"""
|
|
162
|
-
# Remove duplicates.
|
|
163
|
-
self._plugins_wanted_to_be_activated = list(set(self._plugins_wanted_to_be_activated))
|
|
164
|
-
# Check the pre-conditions for the plugins.
|
|
165
|
-
self._activated_plugins = self._check_pre_conditions(
|
|
166
|
-
plugin_package=self._plugin_package,
|
|
167
|
-
want_to_activate_plugins=self._plugins_wanted_to_be_activated,
|
|
168
|
-
application=self._application,
|
|
169
|
-
)
|
|
170
|
-
# Load from the previous check.
|
|
171
|
-
for plugin in self._activated_plugins:
|
|
172
|
-
self.add_states(states=plugin.on_load(application=self._application) or [])
|
|
173
|
-
|
|
174
|
-
return self
|
|
175
|
-
|
|
176
|
-
async def trigger_startup(self) -> Self:
|
|
177
|
-
"""Trigger the startup of the plugins."""
|
|
178
|
-
for plugin in self._activated_plugins:
|
|
179
|
-
states: list[PluginState] | None = await plugin.on_startup(application=self._application)
|
|
180
|
-
self.add_states(states=states or [])
|
|
181
|
-
|
|
182
|
-
return self
|
|
183
|
-
|
|
184
|
-
async def trigger_shutdown(self) -> Self:
|
|
185
|
-
"""Trigger the shutdown of the plugins."""
|
|
186
|
-
for plugin in self._activated_plugins:
|
|
187
|
-
await plugin.on_shutdown(application=self._application)
|
|
188
|
-
|
|
189
|
-
return self
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"""Example Plugin Module."""
|
|
2
|
-
|
|
3
|
-
from typing import TYPE_CHECKING
|
|
4
|
-
|
|
5
|
-
if TYPE_CHECKING:
|
|
6
|
-
from fastapi_factory_utilities.core.plugins import PluginState
|
|
7
|
-
from fastapi_factory_utilities.core.protocols import ApplicationAbstractProtocol
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
def pre_conditions_check(application: "ApplicationAbstractProtocol") -> bool: # pylint: disable=unused-argument
|
|
11
|
-
"""Check the pre-conditions for the example plugin."""
|
|
12
|
-
return True
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def on_load(
|
|
16
|
-
application: "ApplicationAbstractProtocol", # pylint: disable=unused-argument
|
|
17
|
-
) -> list["PluginState"] | None:
|
|
18
|
-
"""Actions to perform on load for the example plugin."""
|
|
19
|
-
return
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
async def on_startup(
|
|
23
|
-
application: "ApplicationAbstractProtocol", # pylint: disable=unused-argument
|
|
24
|
-
) -> list["PluginState"] | None:
|
|
25
|
-
"""Actions to perform on startup for the example plugin."""
|
|
26
|
-
return
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
async def on_shutdown(application: "ApplicationAbstractProtocol") -> None: # pylint: disable=unused-argument
|
|
30
|
-
"""Actions to perform on shutdown for the example plugin."""
|
|
31
|
-
return
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"""Httpx Plugin Module."""
|
|
2
|
-
|
|
3
|
-
from typing import TYPE_CHECKING
|
|
4
|
-
|
|
5
|
-
if TYPE_CHECKING:
|
|
6
|
-
from fastapi_factory_utilities.core.plugins import PluginState
|
|
7
|
-
from fastapi_factory_utilities.core.protocols import ApplicationAbstractProtocol
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
def pre_conditions_check(application: "ApplicationAbstractProtocol") -> bool: # pylint: disable=unused-argument
|
|
11
|
-
"""Check the pre-conditions for the example plugin."""
|
|
12
|
-
return True
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def on_load(
|
|
16
|
-
application: "ApplicationAbstractProtocol", # pylint: disable=unused-argument
|
|
17
|
-
) -> list["PluginState"] | None:
|
|
18
|
-
"""Actions to perform on load for the example plugin."""
|
|
19
|
-
return
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
async def on_startup(
|
|
23
|
-
application: "ApplicationAbstractProtocol", # pylint: disable=unused-argument
|
|
24
|
-
) -> list["PluginState"] | None:
|
|
25
|
-
"""Actions to perform on startup for the example plugin."""
|
|
26
|
-
return
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
async def on_shutdown(application: "ApplicationAbstractProtocol") -> None: # pylint: disable=unused-argument
|
|
30
|
-
"""Actions to perform on shutdown for the example plugin."""
|
|
31
|
-
return
|
{fastapi_factory_utilities-0.5.0.dist-info → fastapi_factory_utilities-0.6.0.dist-info}/WHEEL
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|