classDiracFastAPI(FastAPI):def__init__(self):@contextlib.asynccontextmanagerasyncdeflifespan(app:DiracFastAPI):asyncwithcontextlib.AsyncExitStack()asstack:awaitasyncio.gather(*(stack.enter_async_context(f())forfinapp.lifetime_functions))yieldself.lifetime_functions=[]super().__init__(swagger_ui_init_oauth={"clientId":"myDIRACClientID","scopes":"property:NormalUser","usePkceWithAuthorizationCodeGrant":True,},generate_unique_id_function=lambdaroute:f"{route.tags[0]}_{route.name}",title="Dirac",lifespan=lifespan,openapi_url="/api/openapi.json",docs_url="/api/docs",swagger_ui_oauth2_redirect_url="/api/docs/oauth2-redirect",)# FIXME: when autorest will support 3.1.0# From 0.99.0, FastAPI is using openapi 3.1.0 by default# This version is not supported by autorest yetself.openapi_version="3.0.2"defopenapi(self,*args,**kwargs):ifnotself.openapi_schema:super().openapi(*args,**kwargs)_downgrade_openapi_schema(self.openapi_schema)# Remove 422 responses as we don't want autorest to use itfor_,method_iteminself.openapi_schema.get("paths").items():for_,paraminmethod_item.items():responses=param.get("responses")if"422"inresponses:delresponses["422"]returnself.openapi_schema
Attributes
lifetime_functions=[]instance-attribute
openapi_version='3.0.2'instance-attribute
Functions
__init__()
Source code in diracx-routers/src/diracx/routers/fastapi_classes.py
def__init__(self):@contextlib.asynccontextmanagerasyncdeflifespan(app:DiracFastAPI):asyncwithcontextlib.AsyncExitStack()asstack:awaitasyncio.gather(*(stack.enter_async_context(f())forfinapp.lifetime_functions))yieldself.lifetime_functions=[]super().__init__(swagger_ui_init_oauth={"clientId":"myDIRACClientID","scopes":"property:NormalUser","usePkceWithAuthorizationCodeGrant":True,},generate_unique_id_function=lambdaroute:f"{route.tags[0]}_{route.name}",title="Dirac",lifespan=lifespan,openapi_url="/api/openapi.json",docs_url="/api/docs",swagger_ui_oauth2_redirect_url="/api/docs/oauth2-redirect",)# FIXME: when autorest will support 3.1.0# From 0.99.0, FastAPI is using openapi 3.1.0 by default# This version is not supported by autorest yetself.openapi_version="3.0.2"
openapi(*args,**kwargs)
Source code in diracx-routers/src/diracx/routers/fastapi_classes.py
defopenapi(self,*args,**kwargs):ifnotself.openapi_schema:super().openapi(*args,**kwargs)_downgrade_openapi_schema(self.openapi_schema)# Remove 422 responses as we don't want autorest to use itfor_,method_iteminself.openapi_schema.get("paths").items():for_,paraminmethod_item.items():responses=param.get("responses")if"422"inresponses:delresponses["422"]returnself.openapi_schema
DiracxRouter
Bases: APIRouter
Source code in diracx-routers/src/diracx/routers/fastapi_classes.py
classDiracxRouter(APIRouter):def__init__(self,*,dependencies=None,require_auth:bool=True,include_in_schema:bool=True,path_root:str="/api",):super().__init__(dependencies=dependencies,include_in_schema=include_in_schema)self.diracx_require_auth=require_authself.diracx_path_root=path_root##### These 2 methods are needed to overwrite routes# https://github.com/tiangolo/fastapi/discussions/8489defadd_api_route(self,path:str,endpoint:Callable[...,Any],**kwargs):route_index=self._get_route_index_by_path_and_methods(path,set(kwargs.get("methods",[])))ifroute_index>=0:self.routes.pop(route_index)returnsuper().add_api_route(path,endpoint,**kwargs)def_get_route_index_by_path_and_methods(self,path:str,methods:set[str])->int:routes=cast(list[Route],self.routes)forindex,routeinenumerate(routes):ifroute.path==pathandmethods==route.methods:returnindexreturn-1
def_downgrade_openapi_schema(data):"""Modify an openapi schema in-place to be compatible with AutoRest."""ifisinstance(data,dict):fork,vinlist(data.items()):ifk=="anyOf":if{"type":"null"}inv:v.pop(v.index({"type":"null"}))data["nullable"]=Trueiflen(v)==1:data|=v[0]elifk=="const":data.pop(k)# https://github.com/fastapi/fastapi/discussions/12984elifk=="propertyNames":data.pop(k)_downgrade_openapi_schema(v)ifisinstance(data,list):forvindata:_downgrade_openapi_schema(v)