Monday 29 November 2021

FastAPI: Deprecate a query parameter

By setting the ‘deprecated’ argument of Query function to True, we can specify that this parameter is going to be deprecated in future.

 

deprecateQueryParam.py

from fastapi import FastAPI, Query
from pydantic import BaseModel
from typing import Optional

app = FastAPI()

@app.get("/products")
def productInfo(
    infoLevel : str,
    formatType : str = Query('JSON', min_length=2, deprecated=True),
    productId: str = Query(
        ...,
        min_length = 4, 
        max_length=50,  
        title='Product id',
        description = "Enter valid product id"
        ), 
    noOfItems : Optional[int] = Query(None, gt = 0, lt = 50, title='Number of items to return')
):
    return {
        'productId' : productId, 
        'noOfItems' : noOfItems, 
        'infoLevel' : infoLevel, 
        'formatType' : formatType
    }

 

Open terminal and execute the command ‘uvicorn deprecateQueryParam:app --reload’.

$uvicorn deprecateQueryParam:app --reload
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [93386] using statreload
INFO:     Started server process [93390]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     127.0.0.1:54262 - "GET /docs HTTP/1.1" 200 OK
INFO:     127.0.0.1:54262 - "GET /openapi.json HTTP/1.1" 200 OK

Open the url ‘http://127.0.0.1:8000/docs’ in browser.


 

Now, you can see that formatType is deprecated.

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment