This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def request_validation_exception_handler(request: Request, exc: RequestValidationError) -> JSONResponse: | |
return JSONResponse( | |
status_code=HTTP_422_UNPROCESSABLE_ENTITY, | |
content={ | |
"error_name": exc.__class__.__name__, | |
"message": jsonable_encoder(exc.errors()), | |
}, | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LATEST_API_VERSION = APIVersion(major_version=1, minor_version=1) | |
# Create app object and add routes | |
app = FastAPI(title="My Item App") | |
app.include_router(unversioned_router) | |
app.include_router(router_v1_0) | |
app.include_router(router_v1_1) | |
exception_handlers = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def version_app( | |
app: FastAPI, | |
default_api_version: APIVersion, | |
exception_handlers: Optional[Dict[Union[int, Type[Exception]], Callable]], | |
**kwargs | |
): | |
app = VersionedFastAPI( | |
app, | |
version=default_api_version.to_str(), # Version that appears at the top of the API docs | |
default_version=default_api_version.to_tuple(), # Version at which unversioned endpoints start to be available |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class APIVersion: | |
def __init__(self, major_version, minor_version): | |
self._major_version = major_version | |
self._minor_version = minor_version | |
def to_tuple(self) -> Tuple[int, int]: | |
return self._major_version, self._minor_version | |
def to_str(self) -> str: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def request_validation_exception_handler(request: Request, exc: RequestValidationError): | |
return PlainTextResponse(str(exc), status_code=400) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app = FastAPI( | |
title="My Item App”, | |
exception_handlers={ | |
500: internal_error_exception_handler, # Uncontrolled internal server errors (e.g. raised by FastAPI's middlewares) | |
RequestValidationError: request_validation_exception_handler, # Custom data validation error | |
} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI | |
from fastapi_versioning import VersionedFastAPI, version | |
app = FastAPI(title="My App") | |
@app.get("/greet") | |
@version(1, 0) | |
def greet_with_hello(): | |
return "Hello" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Non-parametric computation of entropy and mutual-information | |
Adapted by G Varoquaux for code created by R Brette, itself | |
from several papers (see in the code). | |
These computations rely on nearest-neighbor statistics | |
''' | |
import numpy as np |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.