Skip to content

Callback hook

callback runs for every versioned router, the latest_prefix alias included, right before RouterVersioner includes it in the app, handy for logging every mount point or wiring metrics:

def on_version_created(router: APIRouter, version, prefix: str) -> None:
    print(f"Registered version {version} at {prefix}")

RouterVersioner(
    app=app,
    routers=router,
    version_format=VersionFormat.SEMVER,
    callback=on_version_created,
).versionize()

If versionize() raises, don't catch the exception and retry. Errors found while the versions are built, such as a callback that throws or two versions that resolve to the same prefix, are raised before anything is added to the app, but the routers are then included one after another, and this package makes no attempt to undo a partial inclusion. versionize() normally runs at startup, so the app fails to start; fix the underlying issue and start it again.