fastapi api with input paramter, which accept the list of table name, accept mutil…
- User
fastapi api with input paramter, which accept the list of table name, accept mutil selection
- Assistant
Here's an example of a FastAPI endpoint that accepts a list of table names as input:
from fastapi import FastAPI from typing import List app = FastAPI() @app.get("/tables") async def get_tables(tables: List[str]): # Do something with the list of table names return {"tables": tables}In this example, the endpoint is defined with the
@app.get("/tables")decorator, which means that it will handle GET requests to the/tablesURL. Thetablesparameter is declared as aList[str], which means that it expects a list of string values to be passed in as input.When the endpoint is called, the list of table names is passed in as a query parameter, like this:
http://localhost:8000/tables?tables=table1&tables=table2&tables=table3The
tablesparameter is repeated for each value in the list.When the endpoint is called, the
get_tablesfunction is executed, which accepts the list of table names as input. The function can then do something with the list of table names and return a response.Note that because the
tablesparameter is declared as a list, users can provide multiple table names at once by including multiple query parameters in the URL. - User
it is possible to be a list of enum
- Assistant
Yes, it is possible to define a list of
Enumvalues as the input parameter to a FastAPI endpoint. Here's an example:from fastapi import FastAPI from enum import Enum from typing import List app = FastAPI() class TableName(str, Enum): users = "users" orders = "orders" products = "products" @app.get("/tables") async def get_tables(tables: List[TableName]): # Do something with the list of table names return {"tables": tables}In this example, we define an
Enumclass calledTableNamethat contains the names of the tables we want to accept as input. TheTableNameclass is declared by inheriting from bothstrandEnum, and each value in theEnumis a string representation of a table name.We then define an endpoint with the
@app.get("/tables")decorator that accepts a list ofTableNamevalues as input. Thetablesparameter is declared as a parameter, which means that it expects a list of enum values to be passed in as input.