# Path: rollbar/contrib/starlette/middleware.py
# 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(
#                         'Failed to report asynchronously. Trying to report synchronously.'
#                     )
#                     rollbar.report_exc_info(exc_info, request)
#             raise


import rollbar
import uvicorn
from rollbar.contrib.starlette import ReporterMiddleware as RollbarMiddleware
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
app = Starlette()
app.add_middleware(RollbarMiddleware)  # should be added as the first middleware


# Verify application runs correctly
#
# $ curl http://localhost:8888
@app.route('/')
async def root(request):
    return PlainTextResponse('hello world')


# Cause an uncaught exception to be sent to Rollbar
# GET query params will be sent to Rollbar and available in the UI
#
# $ curl http://localhost:8888/error?param1=hello&param2=world
async def localfunc(arg1, arg2, arg3):
    # Both local variables and function arguments will be sent to Rollbar
    # and available in the UI
    localvar = 'local variable'
    cause_error_with_local_variables


@app.route('/error')
async def error(request):
    await localfunc('func_arg1', 'func_arg2', 1)
    return PlainTextResponse("You shouldn't be seeing this")


if __name__ == '__main__':

# Path: rollbar/contrib/starlette/middleware.py
# 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(
#                         'Failed to report asynchronously. Trying to report synchronously.'
#                     )
#                     rollbar.report_exc_info(exc_info, request)
#             raise


# Path: rollbar/examples/starlette/app.py
import rollbar
import uvicorn
from rollbar.contrib.starlette import ReporterMiddleware as RollbarMiddleware
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse

app = Starlette()
app.add_middleware(RollbarMiddleware)  # should be added as the first middleware


# Verify application runs correctly
#
# $ curl http://localhost:8888
@app.route('/')
async def root(request):
    return PlainTextResponse('hello world')


# Cause an uncaught exception to be sent to Rollbar
# GET query params will be sent to Rollbar and available in the UI
#
# $ curl http://localhost:8888/error?param1=hello&param2=world
async def localfunc(arg1, arg2, arg3):
    # Both local variables and function arguments will be sent to Rollbar
    # and available in the UI
    localvar = 'local variable'
    cause_error_with_local_variables


@app.route('/error')
async def error(request):
    await localfunc('func_arg1', 'func_arg2', 1)
    return PlainTextResponse("You shouldn't be seeing this")


if __name__ == '__main__':

    uvicorn.run(app, host='localhost', port=8888)