I would like to have a dynamical mandatory body on FastApi.
I explain :
from fastapi import FastAPI, Body
from pydantic import BaseModel
app = FastAPI()
class Parameters(BaseModel):
platform: str
country: str
@app.put("/myroute")
async def provision_instance(*, parameters: Parameters = Body(...)):
do_something
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=80)
Here, my body is manually defined in the Parameters class with two attributes, platform and country. In the future, these attributes will come from a configuration file and there will be more than two attributes. So I will need to create them automatically on the fly.
For example, in the configuration file, I could have :
---
parameters:
application:
description: "Name of the application"
type: string
platform:
description: "Name of the platform"
type: string
country:
description: "Name of the country"
type: string
How could I have in this context a variable number of parameters required in the body ? Should I find a way to give to my Parameters class a variable number of attributes ?