Pyd
Pyd

Reputation: 6159

How can I create Swagger docs for dynamic FastAPI endpoints?

I have a list of endpoints like below.

endpoints ["/endpoint1", "/endpoint2", "/endpoint3"]

I would like to create dynamic endpoints in my app and create swagger API docs for all the endpoints, how can I do this.

@app.route(<endpoint>):
  def process():

Upvotes: 5

Views: 1162

Answers (1)

JPG
JPG

Reputation: 88499

Use Enum--(FastAPI doc) classes

from enum import Enum
from fastapi import FastAPI


class ModelName(str, Enum):
    endpoint1 = "endpoint1"
    endpoint2 = "endpoint2"
    endpoint3 = "endpoint3"


app = FastAPI()


@app.get("/model/{model_name}")
async def process(model_name: ModelName):
    return {"model_name": model_name, "message": "Some message"}

and thus you will get the result as follows,

enter image description here

Upvotes: 1

Related Questions