Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (76 loc) · 1.87 KB
/
Copy pathmain.py
File metadata and controls
107 lines (76 loc) · 1.87 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# from fastapi import FastAPI
# app = FastAPI()
# @app.get("/")
# def home():
# return{"message": "Hello world from FastApI VENV"}
# from fastapi import FastAPI
# app = FastAPI()
# # Home Route
# @app.get("/") # ye blackslash ye dikha rha hai ku ki home route ke liye simple blackslash lagan hai
# # ek route ke saath ek function attach hota hai
# def home():
# return{"message": "welcome to FastAPI"}
# # About route
# @app.get("/about")
# def about():
# return{"message": "this is a about page"}
# # users route
# @app.get("/users")
# def user():
# return{
# "users" : ["Mohit","swati","rahul"]
# }
# Dynamic Routes
# from fastapi import FastAPI
# app = FastAPI()
# @app.get("/users/{user_id}") # ye path parameter hai {} ka use hua hai
# def get_users(user_id):
# return{
# "user_id":user_id
# }
# @app.get("/users/{user_name}")
# def get_users(user_name:str):
# return{
# "user_id": user_name
# }
# from fastapi import FastAPI
# app = FastAPI()
# @app.post("/create-users")
# def create_user(name:str,age:int):
# return{
# "Name" : name,
# "Age" : age
# }
# @app.post("/create-user")
# def create_user(user:dict):
# return{
# "message" : "user created",
# "data" : user
# }
# Pydnatic model
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Users(BaseModel):
name : str
age: int
@app.post("/created_user")
def created_user(user:Users):
return{
"message" : "user_created",
"data" : user
}
# nested pydantic model
from pydantic import BaseModel
from fastapi import FastAPI
app = FastAPI()
class Address(BaseModel):
city: str
pincode : int
class Users(BaseModel):
name: str
age: int
address: Address
@app.post("/created_user")
def created_user(User:Users):
return User