# Path: rollbar/contrib/starlette/logger.py
# class LoggerMiddleware(ASGIReporterMiddleware):
#     def __init__(self, app: ASGIApp) -> None:
#         if sys.version_info < (3, 6):
#             log.error(
#                 'LoggerMiddleware requires Python 3.7+ (or 3.6 with `aiocontextvars` package)'
#             )
#             raise RuntimeError(
#                 'LoggerMiddleware requires Python 3.7+ (or 3.6 with `aiocontextvars` package)'
#             )
# 
#         super().__init__(app)
# 
#     async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
#         store_current_request(scope, receive)
# 
#         await self.app(scope, receive, send)


import rollbar
import uvicorn
from rollbar.contrib.starlette import LoggerMiddleware
from starlette.applications import Starlette
from starlette.responses import JSONResponse

# This example uses Uvicorn package that must be installed. However, it can be
# replaced with any other ASGI-compliant server.
#
# NOTE: Python 3.6 requires aiocontextvars package to be installed.
#
# Run: python app_global_request.py



# Integrate Rollbar with Starlette application
app = Starlette()
app.add_middleware(LoggerMiddleware)  # should be added as the last middleware


async def get_user_agent():
    # Global access to the current request object
    request = rollbar.get_request()

    user_agent = request.headers['User-Agent']
    return user_agent


# $ curl -i http://localhost:8888
@app.route('/')
async def root(request):
    user_agent = await get_user_agent()
    return JSONResponse({'user-agent': user_agent})



# Path: rollbar/contrib/starlette/logger.py
# class LoggerMiddleware(ASGIReporterMiddleware):
#     def __init__(self, app: ASGIApp) -> None:
#         if sys.version_info < (3, 6):
#             log.error(
#                 'LoggerMiddleware requires Python 3.7+ (or 3.6 with `aiocontextvars` package)'
#             )
#             raise RuntimeError(
#                 'LoggerMiddleware requires Python 3.7+ (or 3.6 with `aiocontextvars` package)'
#             )
# 
#         super().__init__(app)
# 
#     async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
#         store_current_request(scope, receive)
# 
#         await self.app(scope, receive, send)


# Path: rollbar/examples/starlette/app_global_request.py
import rollbar
import uvicorn
from rollbar.contrib.starlette import LoggerMiddleware
from starlette.applications import Starlette
from starlette.responses import JSONResponse


# This example uses Uvicorn package that must be installed. However, it can be
# replaced with any other ASGI-compliant server.
#
# NOTE: Python 3.6 requires aiocontextvars package to be installed.
#
# Run: python app_global_request.py



# Integrate Rollbar with Starlette application
app = Starlette()
app.add_middleware(LoggerMiddleware)  # should be added as the last middleware


async def get_user_agent():
    # Global access to the current request object
    request = rollbar.get_request()

    user_agent = request.headers['User-Agent']
    return user_agent


# $ curl -i http://localhost:8888
@app.route('/')
async def root(request):
    user_agent = await get_user_agent()
    return JSONResponse({'user-agent': user_agent})



if __name__ == '__main__':