Skip to content

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

Closed
didip opened this issue Jun 15, 2019 · 9 comments
Closed

The router is unable to add Starlette's HTTPEndpoint #317

didip opened this issue Jun 15, 2019 · 9 comments

Comments

@didip
Copy link

didip commented Jun 15, 2019

Describe the bug
I cannot find a way to add Starlette's HTTPEndpoint onto app.router.

To Reproduce
Steps to reproduce the behavior:

  1. Create app: app = fastapi.FastAPI()

  2. Create an endpoint:

class HealthHandler(starlette.endpoints.HTTPEndpoint):
    async def get(self, request):
        return starlette.responses.JSONResponse({'status': 'OK'})
  1. I am stuck now, I cannot see a way to add HTTPEndpoint into app.router.

Expected behavior
Able to add HTTPEndpoint class.

Environment:

  • OS: macOS

  • FastAPI Version: fastapi==0.29.1

  • Python version: 3.7.3

@didip didip added the bug Something isn't working label Jun 15, 2019
@stefanondisponibile
Copy link

That's not really how FastAPI's meant to look like.
However, if you wanted to define an endpoint that way, remember you are in a Starlette environment:

FastAPI is actually a sub-class of Starlette.

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"}

@didip
Copy link
Author

didip commented Jun 18, 2019

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?

@stefanondisponibile
Copy link

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

@HarrySky
Copy link

@stefanondisponibile @didip Starlette (and FastAPI as subclass) has add_route method so there is no need to create cyclical dependencies:

#health_handler.py

from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse

class HealthHandler(HTTPEndpoint):
    async def get(self, request):
        return JSONResponse({'status': 'OK'})
# main.py
from fastapi import FastAPI
from health_handler import HealthHandler

app = FastAPI()
app.add_route(path="/", route=HealthHandler)

@didip
Copy link
Author

didip commented Jun 20, 2019

@HarrySky follow up question, if I do app.add_route, I don't get the auto generated Swagger, yes?

Is there a app.add_api_route that also generate Swagger?

@tiangolo
Copy link
Owner

Yep. @stefanondisponibile is correct. And then @HarrySky's insight is correct too 😄

Thanks for the help @stefanondisponibile and @HarrySky !

@tiangolo
Copy link
Owner

@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 HTTPEndpoint style.

@didip
Copy link
Author

didip commented Jun 20, 2019

Gotcha, thank you for all the explanations, guys!

@didip didip closed this as completed Jun 20, 2019
@tiangolo
Copy link
Owner

Thanks for reporting back and closing the issue.

@tiangolo tiangolo added question Question or problem answered reviewed and removed bug Something isn't working labels Feb 22, 2023
@tiangolo tiangolo changed the title [BUG] The router is unable to add Starlette's HTTPEndpoint The router is unable to add Starlette's HTTPEndpoint Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8142 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

4 participants