-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptional_parameter.py
More file actions
64 lines (43 loc) · 1.16 KB
/
optional_parameter.py
File metadata and controls
64 lines (43 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional
app = FastAPI()
#creating a model using pydantic
class Item(BaseModel):
id:int
name:str
description:str
price:int
on_offer:bool
#this only return the simple jason response
@app.get("/")
def root():
return {"message": "Hello World"}
#giving item id as query parameter
@app.put("/items/{item_id}")
def update_items(item_id:int,item:Item):
#returing the data from the model
return{
'name':item.name,
'description' : item.description,
'price' : item.price,
'on_offer' : item.on_offer
}
#to give data we have to provide it in its body
#like this
'''
{
"id":1,
"name":"milk",
"description":"1 litre milk ",
"price":2000,
"on_offer":true
}
'''
#this is a optional parameter api
#in of we acess the url with /greet it will give us Hello user as the
#default value of the name is user
#but if we do /greet/?name=fasih it will return Hello fasih
@app.get('/greet')
def greet_optional_name(name:Optional[str] = "user"):
return {"message":f"Hello {name}"}