-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The router is unable to add Starlette's HTTPEndpoint #317
Comments
That's not really how FastAPI's meant to look like.
Therefore, what you could do is: # main.py
from fastapi import FastAPI
from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse
app = FastAPI()
@app.route("/")
class HealthHandler(HTTPEndpoint):
async def get(self, request):
return JSONResponse({'status': 'OK'}) uvicorn main:app
# INFO: Started server process [19630]
# INFO: Waiting for application startup.
# INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
# INFO: ('127.0.0.1', 50608) - "GET / HTTP/1.1" 200 ==> {"status":"OK"} |
Follow up question if you don't mind, what if the Endpoint class is defined in a different module, how can I add it without using the decorator syntax? |
One solution could be this: #health_handler.py
from main import app
from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse
@app.route("/")
class HealthHandler(HTTPEndpoint):
async def get(self, request):
return JSONResponse({'status': 'OK'}) # main.py
from fastapi import FastAPI
app = FastAPI()
import health_handler |
@stefanondisponibile @didip
|
@HarrySky follow up question, if I do Is there a |
Yep. @stefanondisponibile is correct. And then @HarrySky's insight is correct too 😄 Thanks for the help @stefanondisponibile and @HarrySky ! |
@didip The Swagger autogeneration depends on OpenAPI. OpenAPI depends on being able to know the parameters of your request. And that currently depends on using the normal decorator style, not the |
Gotcha, thank you for all the explanations, guys! |
Thanks for reporting back and closing the issue. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Describe the bug
I cannot find a way to add Starlette's HTTPEndpoint onto app.router.
To Reproduce
Steps to reproduce the behavior:
Create app:
app = fastapi.FastAPI()
Create an endpoint:
app.router
.Expected behavior
Able to add HTTPEndpoint class.
Environment:
OS: macOS
FastAPI Version:
fastapi==0.29.1
Python version: 3.7.3
The text was updated successfully, but these errors were encountered: