Skip to content

app.py

Main file, containing the FastAPI web-app definition and its routes.

Exception handling

Bases: HTTPException

Exception raised by the web-app.

Parameters:

Name Type Description Default
status_code int

HTTP code to return.

required
detail str

Error description. Defaults to None.

required
Source code in oblique/app.py
18
19
20
21
22
23
24
25
26
class HTMLException(HTTPException):
    """Exception raised by the web-app.

    Args:
        status_code (int): HTTP code to return.
        detail (str, optional): Error description. Defaults to `None`.
    """

    pass

Define the exception handler for HTMLException.

Source code in oblique/app.py
29
30
31
32
33
async def html_exception_handler(request: Request, exc: HTTPException):
    """Define the exception handler for HTMLException."""
    return HTMLResponse(
        status_code=exc.status_code, content=catalog.render("Error", status_code=exc.status_code, error_msg=exc.detail)
    )

Dependencies

Dependency, to make sure the received request is a HTMX request. If it's not a HTMX request, a 404 is returned.

Parameters:

Name Type Description Default
request Request

Request to check.

required
Source code in oblique/app.py
39
40
41
42
43
44
45
46
47
async def htmx(request: Request):
    """Dependency, to make sure the received request is a HTMX request.
    If it's not a HTMX request, a 404 is returned.

    Args:
        request (Request): Request to check.
    """
    if "hx-request" not in request.headers or request.headers["hx-request"] != "true":
        raise HTMLException(status_code=404, detail="Sorry, we couldn't find this page.")

Routes

Main route, sending the home page.

Source code in oblique/app.py
50
51
52
53
@router.get("/", response_class=HTMLResponse)
async def home():
    """Main route, sending the home page."""
    return catalog.render("HomePage")

Favicon.

Source code in oblique/app.py
56
57
58
59
@router.get("/favicon.ico", include_in_schema=False)
async def favicon():
    """Favicon."""
    return FileResponse(ASSETS_DIR / "logo.svg")

Search route, to display the search results.

Source code in oblique/app.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@router.get("/search", response_class=HTMLResponse)
async def search(pkg: str, db: Session = Depends(get_db), h: None = Depends(htmx)):
    """Search route, to display the search results."""
    try:
        last_release, n_versions, n_versions_yanked = get_package_info(db, pkg)
        return catalog.render(
            "SearchResult",
            pkg_name=pkg,
            last_release=last_release,
            n_versions=n_versions,
            n_versions_yanked=n_versions_yanked,
        )
    except UnknownPackageException:
        return catalog.render("UnknownPackage", pkg_name=pkg)

Catch-all route, if the user tries to access an unknown page, we display a 404.

Source code in oblique/app.py
78
79
80
81
82
83
@router.route("/{full_path:path}")
async def unknown_path(request: Request):
    """Catch-all route, if the user tries to access an unknown page, we display
    a 404.
    """
    raise HTMLException(status_code=404, detail="Sorry, we couldn't find this page.")