I have created a child class and inherited FastAPI class. I want to define a function lifespan inside it. To implement lifespan I need to create constructor inside myFastAPI class. Below is sample code.
class myFastAPI(FastAPI):
def __init__(self):
self.lifespan = self.lifeSpan
@asynccontextmanager
def lifeSpan(self):
print("Start before Application")
notification = Notification()
yield {'client': notification}
app.state.client.close()
@app.get("/check")
async def index(placeHolder: str) -> str:
message = "API is up and running"
client = request.app.state.client
//some operations
return message
@app.post("/a/b/c")
async def index(request, data) -> str:
client = request.app.state.client
result = somefunc(data, client)
return result
When I try to bring up the API, it is giving below error.
File "/sdfjjgjg/File1.py", line 89, in <module>
@app.get("/check")
File "/asjgjgj/python3.8/site-packages/fastapi/applications.py", line 469, in get
return self.router.get(
AttributeError: 'myFastAPI' object has no attribute 'router'
Why above error is coming and how to fix it. Note: lifespan function I want inside myFastAPI class not outside as it is given in most of the document.
Need to call superclass constructor