import pkg_resources

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from starlette.responses import RedirectResponse, JSONResponse
from routers import auth, media, video, photo, user, igtv, clip, album, story, hashtag, direct

app = FastAPI()
app.include_router(auth.router)
app.include_router(media.router)
app.include_router(video.router)
app.include_router(photo.router)
app.include_router(user.router)
app.include_router(igtv.router)
app.include_router(clip.router)
app.include_router(album.router)
app.include_router(story.router)
app.include_router(hashtag.router)
app.include_router(direct.router)

@app.get("/", tags=["system"], summary="Redirect to /docs")
async def root():
    """Redirect to /docs
    """
    return RedirectResponse(url="/docs")


@app.get("/version", tags=["system"], summary="Get dependency versions")
async def version():
    """Get dependency versions
    """
    versions = {}
    for name in ('instagrapi', ):
        item = pkg_resources.require(name)
        if item:
            versions[name] = item[0].version
    return versions


@app.exception_handler(Exception)
async def handle_exception(request, exc: Exception):
    return JSONResponse({
        "detail": str(exc),
        "exc_type": str(type(exc).__name__)
    }, status_code=500)


def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    # for route in app.routes:
    #     body_field = getattr(route, 'body_field', None)
    #     if body_field:
    #         body_field.type_.__name__ = 'name'
    openapi_schema = get_openapi(
        title="instagrapi-rest",
        version="1.0.0",
        description="RESTful API Service for instagrapi",
        routes=app.routes,
    )
    app.openapi_schema = openapi_schema
    return app.openapi_schema

app.openapi = custom_openapi

[((8, 6, 8, 15), 'fastapi.FastAPI', 'FastAPI', ({}, {}), '()', False, 'from fastapi import FastAPI\n'), ((25, 11, 25, 40), 'starlette.responses.RedirectResponse', 'RedirectResponse', (), '', False, 'from starlette.responses import RedirectResponse, JSONResponse\n'), ((55, 21, 60, 5), 'fastapi.openapi.utils.get_openapi', 'get_openapi', (), '', False, 'from fastapi.openapi.utils import get_openapi\n'), ((34, 15, 34, 42), 'pkg_resources.require', 'pkg_resources.require', ({(34, 37, 34, 41): 'name'}, {}), '(name)', False, 'import pkg_resources\n')]