Skip to content

Instantly share code, notes, and snippets.

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()),
},
)
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 = {
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
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:
async def request_validation_exception_handler(request: Request, exc: RequestValidationError):
return PlainTextResponse(str(exc), status_code=400)
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
}
)
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"
@Montgoner
Montgoner / mutual_info.py
Last active February 1, 2017 23:31 — forked from GaelVaroquaux/mutual_info.py
Estimating entropy and mutual information with scikit-learn
'''
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
@Montgoner
Montgoner / recover_ML_structure.ipynb
Last active October 26, 2016 16:29
Preliminary data exploration of results of experiments with model averaging algorithms where we generate domains (with fixed "external parents") within the class of structures over which we perform model averaging
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.