-
-
Save up1/61127a09b7531867e1bb2e87a833e1e9 to your computer and use it in GitHub Desktop.
FastAPI :: OpenAPI or Swagger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI | |
app = FastAPI() | |
@app.get("/", tags=["Main"]) | |
async def root(): | |
return {"message": "Hello World"} | |
@app.get("/products/{product_id}", tags=["Product"]) | |
async def get_product(product_id: int): | |
return {"product_id": product_id} | |
@app.post("/products/", tags=["Product"]) | |
async def create_product(product: dict): | |
return {"product": product} | |
@app.put("/users/{user_id}", tags=["User"]) | |
async def update_user(user_id: int): | |
return {"user_id": user_id} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from enum import Enum | |
class Tags(Enum): | |
Main = "Main" | |
Product = "Product" | |
User = "User" | |
@app.get("/", tags=[Tags.Main]) | |
@app.get("/products/{product_id}", tags=[Tags.Product]) | |
@app.post("/products/", tags=[Tags.Product]) | |
@app.put("/users/{user_id}", tags=[Tags.User]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI | |
// Sub API | |
subapi = FastAPI() | |
@subapi.get("/extra/") | |
async def read_extra(): | |
return {"message": "Hello Extra"} | |
// Main app | |
app = FastAPI() | |
@app.get("/", tags=[Tags.Main]) | |
@app.get("/products/{product_id}", tags=[Tags.Product]) | |
// Mount | |
app.mount("/extra", subapi) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment