from fastapi import FastAPI
from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse, Response
import uvicorn
class App:
Server: FastAPI = FastAPI()
def __init__(self):
pass
@Server.get("/home", response_class=PlainTextResponse)
async def Home(self):
return "e"
def Run(self, *args, **kwargs):
try:
uvicorn.run(self.Server, **kwargs)
except:
return self
App().Run(host="0.0.0.0", port=42069)
Using this code, when i attempt to run it, and make a request to GET/home/ i get this error client side.
{
"detail": [
{
"loc": [
"query",
"self"
],
"msg": "field required",
"type": "value_error.missing"
}
]
}
and this error server side
INFO: 192.168.1.88:58374 - "GET /home HTTP/1.1" 422 Unprocessable Entity
I know the reason im getting this error is because its recognizing the variable self as a missing query, however im not sure how to fix this without removing it from the class (which i dont want to do) Thanks for any help yall can provide :)).
Hi,
self = None can fix your problem.
i mean:
async def Home(self=None):
return "e"
I didn't understand what you were doing
Hi,
self = Nonecan fix your problem.
i mean:async def Home(self=None): return "e"but I don't understand what you're doing :)
It's just returning the letter e, as a test.
However that likely won't fix the issue, because, it won't be able to call on the rest of the class, as it will be a bull value rather than the class object.
I know,
but in your way, you should initial the self (your Object) or hide it(Null). [i think]
```
Do you need an optional parameter?
Your definition of home is a routing annotation. It's not a member of your App object. So there is no self.
Your definition of home is a routing annotation. It's not a member of your App object. So there is no self.
Is it possible to add the app object on top of it?
This will help you do what you want: https://fastapi-utils.davidmontague.xyz/user-guide/class-based-views/
We don't recommend doing this.
@langengel why is it not recommended?
This will help you do what you want: https://fastapi-utils.davidmontague.xyz/user-guide/class-based-views/
I found multiple solutions, this one is definitely the most helpful. I also found that, if I don't need the class I can make it a static method and stack the decorators.
Most helpful comment
This will help you do what you want: https://fastapi-utils.davidmontague.xyz/user-guide/class-based-views/