Last active
January 3, 2021 09:27
-
-
Save sarveshkumargupta/a91fe551b0d125de51584f2be9698fb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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