Skip to content

Instantly share code, notes, and snippets.

@sarveshkumargupta
Last active January 3, 2021 09:27
Show Gist options
  • Save sarveshkumargupta/a91fe551b0d125de51584f2be9698fb3 to your computer and use it in GitHub Desktop.
Save sarveshkumargupta/a91fe551b0d125de51584f2be9698fb3 to your computer and use it in GitHub Desktop.
from fastapi import FastAPI
app = FastAPI() # creating instance of FastAPI
# list of data (i.e fruits)
fruits = [
{
"fruit_id": 1,
"name": "mango",
"color": "yellow",
"description": None
},
{
"fruit_id": 2,
"name": "apple",
"color": "red",
"description": 'good for health'
}
]
@app.get("/")
def welcome():
return {"Welcome To": "FastAPI Blog"}
# returns all fruit list
@app.get("/fruits")
def all_fruits():
return {"All Fruits": fruits}
# finds fruit by fruit_id
@app.get("/find/fruit/{fruit_id}")
def find_fruit(fruit_id: int):
queried_fruit_id = fruit_id
for fruit in fruits:
exist_fruit_id = fruit.get('fruit_id')
if exist_fruit_id == queried_fruit_id:
return {"Result": fruit}
return {"Result": "Fruit is Not Available"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment