Skip to content

Instantly share code, notes, and snippets.

@up1
Last active October 14, 2024 16:46
Show Gist options
  • Save up1/61127a09b7531867e1bb2e87a833e1e9 to your computer and use it in GitHub Desktop.
Save up1/61127a09b7531867e1bb2e87a833e1e9 to your computer and use it in GitHub Desktop.
FastAPI :: OpenAPI or Swagger
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}
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])
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