# 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/contrib/asgi/middleware.py
# class ReporterMiddleware(IntegrationBase):
#     def __init__(self, app: ASGIApp) -> None:
#         super().__init__()
# 
#         self.app = app
# 
#     async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
#         try:
#             await self.app(scope, receive, send)
#         except Exception:
#             if scope['type'] == 'http':
#                 exc_info = sys.exc_info()
# 
#                 try:
#                     await try_report(exc_info)
#                 except RollbarAsyncError:
#                     log.warning(
#                         'Failed to report asynchronously. Trying to report synchronously.'
#                     )
#                     rollbar.report_exc_info(exc_info)
#             raise
# 
# 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/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 starlette import __version__
from starlette.requests import Request
from starlette.types import Receive, Scope, Send
from .requests import store_current_request
from rollbar.contrib.asgi import ReporterMiddleware as ASGIReporterMiddleware
from rollbar.contrib.asgi.integration import integrate
from rollbar.lib._async import RollbarAsyncError, try_report



log = logging.getLogger(__name__)


@integrate(framework_name=f'starlette {__version__}')
class ReporterMiddleware(ASGIReporterMiddleware):
    async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
        try:
            store_current_request(scope, receive)
            await self.app(scope, receive, send)
        except Exception:
            if scope['type'] == 'http':
                request = Request(scope, receive)

                # Consuming the request body in Starlette middleware is problematic.
                # See: https://github.com/encode/starlette/issues/495#issuecomment-494008175
                #
                # Uncomment lines below if you know the risks.
                #
                # Starlette requires the `python-multipart` package to parse the content
                # await request.body()
                # await request.form()

                exc_info = sys.exc_info()

                try:
                    await try_report(exc_info, request)
                except RollbarAsyncError:

# 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/contrib/asgi/middleware.py
# class ReporterMiddleware(IntegrationBase):
#     def __init__(self, app: ASGIApp) -> None:
#         super().__init__()
# 
#         self.app = app
# 
#     async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
#         try:
#             await self.app(scope, receive, send)
#         except Exception:
#             if scope['type'] == 'http':
#                 exc_info = sys.exc_info()
# 
#                 try:
#                     await try_report(exc_info)
#                 except RollbarAsyncError:
#                     log.warning(
#                         'Failed to report asynchronously. Trying to report synchronously.'
#                     )
#                     rollbar.report_exc_info(exc_info)
#             raise
# 
# 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/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/starlette/middleware.py
import logging
import sys
import rollbar
from starlette import __version__
from starlette.requests import Request
from starlette.types import Receive, Scope, Send
from .requests import store_current_request
from rollbar.contrib.asgi import ReporterMiddleware as ASGIReporterMiddleware
from rollbar.contrib.asgi.integration import integrate
from rollbar.lib._async import RollbarAsyncError, try_report




log = logging.getLogger(__name__)


@integrate(framework_name=f'starlette {__version__}')
class ReporterMiddleware(ASGIReporterMiddleware):
    async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
        try:
            store_current_request(scope, receive)
            await self.app(scope, receive, send)
        except Exception:
            if scope['type'] == 'http':
                request = Request(scope, receive)

                # Consuming the request body in Starlette middleware is problematic.
                # See: https://github.com/encode/starlette/issues/495#issuecomment-494008175
                #
                # Uncomment lines below if you know the risks.
                #
                # Starlette requires the `python-multipart` package to parse the content
                # await request.body()
                # await request.form()

                exc_info = sys.exc_info()

                try:
                    await try_report(exc_info, request)
                except RollbarAsyncError:

                    log.warning(