Skip to content

api.py

File containing the routes of the API.

Exception handling

Bases: HTTPException

Exception raised by the API.

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/api.py
14
15
16
17
18
19
20
21
22
class APIException(HTTPException):
    """Exception raised by the API.

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

    pass

Define the exception handler for APIException.

Source code in oblique/api.py
25
26
27
async def api_exception_handler(request: Request, exc: HTTPException):
    """Define the exception handler for APIException."""
    return await http_exception_handler(request, exc)

Data models

Bases: BaseModel

Parameters to pass to identify a package.

  • pkg_name (str): Name of the package to get.
  • force_refresh (bool, optional): If set to True, the local cache is ignored and the PyPi API is called. Note that it might be slower. Defaults to False.
Source code in oblique/api.py
33
34
35
36
37
38
39
40
41
42
43
class PackageParameters(BaseModel):
    """Parameters to pass to identify a package.

    * pkg_name (str): Name of the package to get.
    * force_refresh (bool, optional): If set to `True`, the local cache is
        ignored and the PyPi API is called. Note that it might be slower.
        Defaults to `False`.
    """

    pkg_name: str
    force_refresh: bool = False

Routes

Route to get the informations of the package requested.

These informations are
  • Last release date
  • Number of versions released
  • Number of versions yanked
Source code in oblique/api.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@router.post("/pkg_infos")
def get_pkg_infos(parameters: PackageParameters, db: Session = Depends(get_db)):
    """Route to get the informations of the package requested.

    These informations are :
     * Last release date
     * Number of versions released
     * Number of versions yanked
    """
    try:
        last_release, n_versions, n_versions_yanked = get_package_info(
            db, parameters.pkg_name, human_readable=False, force_refresh=parameters.force_refresh
        )
        return {
            "last_release": last_release,
            "n_versions": n_versions,
            "n_versions_yanked": n_versions_yanked,
        }
    except UnknownPackageException:
        raise APIException(status_code=404, detail="This package was not published to PyPi index.")