-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
118 lines (82 loc) · 2.96 KB
/
main.py
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
108
109
110
111
112
113
114
115
116
117
118
#
# FastAPI is a framework and library for implementing REST web services in Python.
# https://fastapi.tiangolo.com/
#
from fastapi import FastAPI, Response, HTTPException
from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles
from typing import List, Union
# I like to launch directly and not use the standard FastAPI startup process.
# So, I include uvicorn
import uvicorn
from resources.students.students_resource import StudentsResource
from resources.students.students_data_service import StudentDataService
from resources.students.student_models import StudentModel, StudentRspModel
from resources.schools.school_models import SchoolRspModel, SchoolModel
from resources.schools.schools_resource import SchoolsResource
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
# ******************************
#
# DFF TODO Show the class how to do this with a service factory instead of hard code.
def get_data_service():
config = {
"data_directory": "/Users/donaldferguson/Dropbox/0-Examples/e6156-f23-template/data",
"data_file": "students.json"
}
ds = StudentDataService(config)
return ds
def get_student_resource():
ds = get_data_service()
config = {
"data_service": ds
}
res = StudentsResource(config)
return res
students_resource = get_student_resource()
schools_resource = SchoolsResource(config={"students_resource": students_resource})
#
# END TODO
# **************************************
@app.get("/")
async def root():
return RedirectResponse("/static/index.html")
@app.get("/students", response_model=List[StudentRspModel])
async def get_students(uni: str = None, last_name: str = None, school_code: str = None):
"""
Return a list of students matching a query string.
- **uni**: student's UNI
- **last_name**: student's last name
- **school_code**: student's school.
"""
result = students_resource.get_students(uni, last_name, school_code)
return result
@app.get("/students/{uni}", response_model=Union[StudentRspModel, None])
async def get_student(uni: str):
"""
Return a student based on UNI.
- **uni**: student's UNI
"""
result = None
result = students_resource.get_students(uni)
if len(result) == 1:
result = result[0]
else:
raise HTTPException(status_code=404, detail="Not found")
return result
@app.get("/schools", response_model=List[SchoolRspModel])
async def get_schools():
"""
Return a list of schools.
"""
result = schools_resource.get_schools()
return result
@app.get("/schools/{school_code}/students", response_model=List[StudentRspModel])
async def get_schools_students(school_code, uni=None, last_name=None):
"""
Return a list of schools.
"""
result = schools_resource.get_schools_students(school_code, uni, last_name)
return result
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8011)