# Path: rollbar/contrib/fastapi/utils.py
# class fastapi_min_version:
#     def __init__(self, min_version):
#         self.min_version = min_version
# 
#     def __call__(self, func):
#         @functools.wraps(func)
#         def wrapper(*args, **kwargs):
#             if fastapi.__version__ < self.min_version:
#                 raise FastAPIVersionError(
#                     self.min_version, reason=f'to use {func.__name__}() function'
#                 )
# 
#             return func(*args, **kwargs)
# 
#         return wrapper
# 
# def get_installed_middlewares(app):
#     candidates = (
#         FastAPIReporterMiddleware,
#         StarletteReporterMiddleware,
#         ASGIReporterMiddleware,
#     )
# 
#     middlewares = []
# 
#     if hasattr(app, 'user_middleware'):  # FastAPI v0.51.0+
#         middlewares = [
#             middleware.cls
#             for middleware in app.user_middleware
#             if middleware.cls in candidates
#         ]
#     elif hasattr(app, 'error_middleware'):
#         middleware = app.error_middleware
# 
#         while hasattr(middleware, 'app'):
#             if isinstance(middleware, candidates):
#                 middlewares.append(middleware)
#             middleware = middleware.app
# 
#         middlewares = [middleware.__class__ for middleware in middlewares]
# 
#     return middlewares
# 
# def has_bare_routing(app_or_router):
#     expected_app_routes = 4
#     expected_router_routes = 0
# 
#     if (
#         isinstance(app_or_router, FastAPI)
#         and expected_app_routes != len(app_or_router.routes)
#     ) or (
#         isinstance(app_or_router, APIRouter)
#         and expected_router_routes != len(app_or_router.routes)
#     ):
#         return False
# 
#     return True
# 
# Path: rollbar/contrib/asgi/integration.py
# class integrate:
#     """
#     Integrates functions and classes (derived from IntegrationBase) in the SDK.
#     """
# 
#     def __init__(self, *, framework_name='unknown'):
#         self._framework_name = framework_name
# 
#     def __call__(self, obj):
#         if inspect.isclass(obj):
#             obj._integrate = self._register_hook
#             return obj
#         else:
#             return self._insert_hook(obj)
# 
#     def _register_hook(self):
#         def _hook(request, data):
#             data['framework'] = self._framework_name
# 
#         rollbar.BASE_DATA_HOOK = _hook
# 
#     def _insert_hook(self, func):
#         @functools.wraps(func)
#         def wrapper(*args, **kwargs):
#             self._register_hook()
#             return func(*args, **kwargs)
# 
#         return wrapper
# 
# Path: rollbar/contrib/starlette/requests.py
# def store_current_request(
#     request_or_scope: Union[Request, Scope], receive: Optional[Receive] = None
# ) -> Optional[Request]:
#     if ContextVar is None:
#         return None
# 
#     if receive is None:
#         request = request_or_scope
#     elif request_or_scope['type'] == 'http':
#         request = Request(request_or_scope, receive)
#     else:
#         request = None
# 
#     _current_request.set(request)
#     return request
# 
# Path: rollbar/lib/_async.py
# class RollbarAsyncError(Exception):
#     ...
# 
# async def try_report(
#     exc_info=None, request=None, extra_data=None, payload_data=None, level=None, **kw
# ):
#     current_handler = rollbar.SETTINGS.get('handler')
#     if not (current_handler in ALLOWED_HANDLERS or current_handler == 'default'):
#         raise RollbarAsyncError('No async handler set.')
# 
#     if httpx is None:
#         raise RollbarAsyncError('HTTPX is required')
# 
#     return await report_exc_info(
#         exc_info, request, extra_data, payload_data, level, **kw
#     )


import logging
import sys
import rollbar
from typing import Callable, Optional, Type, Union
from fastapi import APIRouter, FastAPI, __version__
from fastapi.routing import APIRoute
    from fastapi import Request, Response
    from starlette.requests import Request
    from starlette.responses import Response
from .utils import fastapi_min_version, get_installed_middlewares, has_bare_routing
from rollbar.contrib.asgi.integration import integrate
from rollbar.contrib.starlette.requests import store_current_request
from rollbar.lib._async import RollbarAsyncError, try_report
__all__ = ['add_to']



try:
except ImportError:
    # Added in FastAPI v0.51.0



# Path: rollbar/contrib/fastapi/utils.py
# class fastapi_min_version:
#     def __init__(self, min_version):
#         self.min_version = min_version
# 
#     def __call__(self, func):
#         @functools.wraps(func)
#         def wrapper(*args, **kwargs):
#             if fastapi.__version__ < self.min_version:
#                 raise FastAPIVersionError(
#                     self.min_version, reason=f'to use {func.__name__}() function'
#                 )
# 
#             return func(*args, **kwargs)
# 
#         return wrapper
# 
# def get_installed_middlewares(app):
#     candidates = (
#         FastAPIReporterMiddleware,
#         StarletteReporterMiddleware,
#         ASGIReporterMiddleware,
#     )
# 
#     middlewares = []
# 
#     if hasattr(app, 'user_middleware'):  # FastAPI v0.51.0+
#         middlewares = [
#             middleware.cls
#             for middleware in app.user_middleware
#             if middleware.cls in candidates
#         ]
#     elif hasattr(app, 'error_middleware'):
#         middleware = app.error_middleware
# 
#         while hasattr(middleware, 'app'):
#             if isinstance(middleware, candidates):
#                 middlewares.append(middleware)
#             middleware = middleware.app
# 
#         middlewares = [middleware.__class__ for middleware in middlewares]
# 
#     return middlewares
# 
# def has_bare_routing(app_or_router):
#     expected_app_routes = 4
#     expected_router_routes = 0
# 
#     if (
#         isinstance(app_or_router, FastAPI)
#         and expected_app_routes != len(app_or_router.routes)
#     ) or (
#         isinstance(app_or_router, APIRouter)
#         and expected_router_routes != len(app_or_router.routes)
#     ):
#         return False
# 
#     return True
# 
# Path: rollbar/contrib/asgi/integration.py
# class integrate:
#     """
#     Integrates functions and classes (derived from IntegrationBase) in the SDK.
#     """
# 
#     def __init__(self, *, framework_name='unknown'):
#         self._framework_name = framework_name
# 
#     def __call__(self, obj):
#         if inspect.isclass(obj):
#             obj._integrate = self._register_hook
#             return obj
#         else:
#             return self._insert_hook(obj)
# 
#     def _register_hook(self):
#         def _hook(request, data):
#             data['framework'] = self._framework_name
# 
#         rollbar.BASE_DATA_HOOK = _hook
# 
#     def _insert_hook(self, func):
#         @functools.wraps(func)
#         def wrapper(*args, **kwargs):
#             self._register_hook()
#             return func(*args, **kwargs)
# 
#         return wrapper
# 
# Path: rollbar/contrib/starlette/requests.py
# def store_current_request(
#     request_or_scope: Union[Request, Scope], receive: Optional[Receive] = None
# ) -> Optional[Request]:
#     if ContextVar is None:
#         return None
# 
#     if receive is None:
#         request = request_or_scope
#     elif request_or_scope['type'] == 'http':
#         request = Request(request_or_scope, receive)
#     else:
#         request = None
# 
#     _current_request.set(request)
#     return request
# 
# Path: rollbar/lib/_async.py
# class RollbarAsyncError(Exception):
#     ...
# 
# async def try_report(
#     exc_info=None, request=None, extra_data=None, payload_data=None, level=None, **kw
# ):
#     current_handler = rollbar.SETTINGS.get('handler')
#     if not (current_handler in ALLOWED_HANDLERS or current_handler == 'default'):
#         raise RollbarAsyncError('No async handler set.')
# 
#     if httpx is None:
#         raise RollbarAsyncError('HTTPX is required')
# 
#     return await report_exc_info(
#         exc_info, request, extra_data, payload_data, level, **kw
#     )


# Path: rollbar/contrib/fastapi/routing.py
import logging
import sys
import rollbar
from typing import Callable, Optional, Type, Union
from fastapi import APIRouter, FastAPI, __version__
from fastapi.routing import APIRoute
    from fastapi import Request, Response
    from starlette.requests import Request
    from starlette.responses import Response
from .utils import fastapi_min_version, get_installed_middlewares, has_bare_routing
from rollbar.contrib.asgi.integration import integrate
from rollbar.contrib.starlette.requests import store_current_request
from rollbar.lib._async import RollbarAsyncError, try_report

__all__ = ['add_to']



try:
except ImportError:
    # Added in FastAPI v0.51.0



log = logging.getLogger(__name__)