Repository URL to install this package:
|
Version:
0.8.1 ▾
|
try:
from typing import Any, Awaitable, Callable, Dict, List
except ImportError:
pass
from supertenant import consts
from supertenant.supermeter.logger import log_instrumentation_skipped, log_instrumentation_success
try:
import fastapi
from fastapi.responses import JSONResponse
_fastapi_version = getattr(fastapi, "__version__")
from supertenant.supermeter.looseversion import LooseVersion
if hasattr(fastapi, "__version__") and (LooseVersion(fastapi.__version__) >= LooseVersion("0.51.0")):
import wrapt
from starlette.middleware import Middleware
from supertenant.supermeter.managers.asgi.middleware_py3 import SuperASGIMiddleware
class FastAPIMiddleware(SuperASGIMiddleware):
def __init__(self, app):
# type: (Callable[[dict[str, str], Any, Any], Awaitable[Any]]) -> None
super(FastAPIMiddleware, self).__init__(consts.INTEGRATION_MODULE_PYTHON_FASTAPI, app, JSONResponse)
@wrapt.patch_function_wrapper("fastapi.applications", "FastAPI.__init__")
def init_with_supertenant(wrapped, instance, args, kwargs):
# type: (Callable[..., None], Any, List[Any], Dict[str, Any]) -> None
middleware = kwargs.get("middleware")
if middleware is None:
kwargs["middleware"] = [Middleware(FastAPIMiddleware)]
elif isinstance(middleware, list):
middleware.append(Middleware(FastAPIMiddleware))
return wrapped(*args, **kwargs)
log_instrumentation_success(consts.INTEGRATION_MODULE_PYTHON_FASTAPI, _fastapi_version)
else:
log_instrumentation_skipped(consts.INTEGRATION_MODULE_PYTHON_FASTAPI, _fastapi_version)
except ImportError as exc:
log_instrumentation_skipped(consts.INTEGRATION_MODULE_PYTHON_FASTAPI, "", {"exc": exc})