-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyFirstAPI.py
More file actions
94 lines (79 loc) · 3 KB
/
myFirstAPI.py
File metadata and controls
94 lines (79 loc) · 3 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
import fastapi
import uvicorn
from fastapi import FastAPI, Path
from typing import Optional, List
from pydantic import BaseModel #提供基本校验和特定数据类型
# 当前路径加入系统变量 ------------------------------------
import os
import sys
root_path = os.getcwd()
print(root_path)
sys.path.append(root_path)
app=FastAPI()
# write a basic python dictionary to store info in key-value pair
students={
1: {
"name":"john",
"age":"22",
"year":"year 12"
}
}
# we want to return the student specific information with the student ID
# for web address in the web server, we need to type in `http://127.0.0.1:8000/get/1`
class Student(BaseModel): #设置固定的datatype
name: str
age: int
year: str
class UpdateStudent(BaseModel):
name: Optional[str]=None
age: Optional[int]=None
year: Optional[str]=None
@app.get("/") #slash is simply the homepage
def index():
return {"name": "First Data"}
# later...written in python dictionary and automatically convert to json format
## Path Parameter
@app.get("/get-student/{student_id}") #put a dynamic variable here
def get_student(student_id: int): #= Path(None, description="The ID of the student you want to view", gt=0, lt=3)): #None:catch the error
return students[student_id]
##对key的数值进行限制:
# gt: greater than
# lt: less than
# ge: greater than or equal to
# le: less than or equal to
## Query Parameter
@app.get('/get-by-name/{student_id}') #@app.get('/get-by-name?john')
def get_student(*, name: Optional[str]=None, test: int): #the star symbol represent the default or other we need
for student_id in students:
if students[student_id]["name"]==name: #get the information by name
return students[student_id]
return {"Data":"Not Found"}
## in some situations, we can combine the query and path parameter together
@app.post("/create_student/{student_id}")
def create_student(student_id: int, student : Student):
if student_id in students:
return {"Error": "Student Exists"} #without store twice
students[student_id]=student
return students[student_id]
#BUT, the input generated by this function only exists in the memory. Namely, if we refresh the webpage, the new generated information will be lost.
@app.put('/update-student/{student_id}')
def update_student(student_id: int, student : Student):
if student_id not in students:
return {"Error": "Student does not exist"}
if student.name != None:
students[student_id].name=student.name
if student.age != None:
student[student_id].age=student.age
if student.year != None:
student[student_id].year=student.year
return students[students_id]
@app.delete("/delete-student/{student_id}")
def delete_student(student_id: int):
if student_id not in students:
return {"Error": "Student does not exist"}
del students[student_id]
return {"Message" : "Delete Successfully"}
if __name__=='__main__':
uvicorn.run(app='myFirstAPI:app', host="127.0.0.1", port=8000, reload=True, debug=True)
# Shell command:
# uvicorn Desktop.myFirstAPI:app --reload