question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] The router is unable to add Starlette's HTTPEndpoint

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
stefanondisponibilecommented, Jun 18, 2019

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"}
3reactions
HarrySkycommented, Jun 20, 2019

@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)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Routing - Starlette
A class that implements the ASGI interface, such as Starlette's HTTPEndpoint. Path Parameters. Paths can use URI templating style to capture path components....
Read more >
Resolving common Linksys cloud account error messages
On the Linksys Smart Wi-Fi Sign In page, you may see an error message that says Internet connection is down. This happens when...
Read more >
I'm getting a Router Not Found error when I try to install my ...
If you use the NETGEAR Nighthawk or Orbi app to install your router or mesh system, and the app can't detect your NETGEAR...
Read more >
[Troubleshooting] Failed Internet access through router - ASUS
When you found out your devices like computers, phones which couldn't get internet access after connecting to the router, could try ...
Read more >
Use routers secured with HomeKit - Apple Support
Add more protection to your HomeKit accessories by controlling which services and devices they communicate with on your home Wi-Fi network ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found