-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fastapi demo #46
Merged
Merged
fastapi demo #46
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f083173
fastapi demo
longsihua2003 8a2ccca
add server end
longsihua2003 4016736
lint and formatting
longsihua2003 cf14f2b
ecjtu-api end
longsihua2003 6747343
Add web apis, refine code comments, add readme information
longsihua2003 0a43994
Add comments
longsihua2003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
import datetime | ||
import re | ||
|
||
from fastapi import FastAPI, Header | ||
from fastapi.responses import RedirectResponse | ||
|
||
from ecjtu.client import ECJTU | ||
|
||
from . import auth, middle, respose_result, schema | ||
|
||
app = FastAPI(title="ECJTU API", description="API for ECJTU") | ||
|
||
app.add_middleware(middle.MyMiddleware) | ||
|
||
|
||
@app.get("/", include_in_schema=False) | ||
def push_docs(): | ||
respose = RedirectResponse(url="/docs") | ||
return respose | ||
|
||
|
||
@app.post( | ||
"/login", | ||
tags=["登录"], | ||
summary="登录", | ||
description="登录获取access_token和refresh_token,access_token用于之后的所有请求,refresh_token用于刷新access_token", | ||
) | ||
def login(user: schema.UserLoginSchema): | ||
try: | ||
access_token, refresh_token = auth.create_tokens(user.stud_id, user.password) | ||
except Exception as e: | ||
return respose_result.ResponseResult.error(str(e)) | ||
|
||
return respose_result.ResponseResult.success( | ||
{"access_token": access_token, "refresh_token": refresh_token} | ||
) | ||
|
||
|
||
@app.post( | ||
"/refresh_token", | ||
tags=["登录"], | ||
summary="刷新access_token", | ||
description="刷新access_token", | ||
) | ||
def refresh_token(data: str = None): | ||
try: | ||
access_token = auth.refresh_access_token(data) | ||
except Exception as e: | ||
return respose_result.ResponseResult.error(str(e)) | ||
return respose_result.ResponseResult.success({"access_token": access_token}) | ||
|
||
|
||
# gpa接口 | ||
Undertone0809 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@app.get("/gpa", tags=["GPA"], summary="获取GPA", description="获取当学期GPA") | ||
def gpa(token: str = Header(None)): | ||
stud_id = auth.get_stud_id(token) | ||
cookie = auth.get_cookie(stud_id) | ||
client = ECJTU(cookie=cookie) | ||
try: | ||
gpa = client.gpa.today() | ||
except Exception as e: | ||
return respose_result.ResponseResult.error(str(e)) | ||
return respose_result.ResponseResult.success(dict(gpa)) | ||
|
||
|
||
# 课表接口 | ||
@app.get("/schedule", tags=["课表"], summary="获取当天课表", description="获取当天课表") | ||
def schedule(token: str = Header(None)): | ||
stud_id = auth.get_stud_id(token) | ||
cookie = auth.get_cookie(stud_id) | ||
client = ECJTU(cookie=cookie) | ||
try: | ||
schedule = client.scheduled_courses.today() | ||
except Exception as e: | ||
return respose_result.ResponseResult.error(str(e)) | ||
schedule_list = [] | ||
for sublist in schedule: | ||
schedule_list.append(dict(sublist)) | ||
return respose_result.ResponseResult.success(schedule_list) | ||
|
||
|
||
@app.get( | ||
"/schedule/{date}", | ||
tags=["课表"], | ||
summary="获取指定日期课表", | ||
description="获取指定日期课表,指定日期格式为yyyy-mm-dd", | ||
) | ||
def schedule_date(token: str = Header(None), date: str = None): | ||
# date(str): The date to filter, eg: 2023-01-01 | ||
try: | ||
valid_date = datetime.datetime.strptime(date, "%Y-%m-%d").date() | ||
except ValueError: | ||
return respose_result.ResponseResult.param_error("日期格式错误") | ||
|
||
stud_id = auth.get_stud_id(token) | ||
cookie = auth.get_cookie(stud_id) | ||
client = ECJTU(cookie=cookie) | ||
try: | ||
scheduled_courses = client.scheduled_courses.filter(date=valid_date) | ||
except Exception as e: | ||
return respose_result.ResponseResult.error(str(e)) | ||
schedule_list = [] | ||
for sublist in scheduled_courses: | ||
schedule_list.append(dict(sublist)) | ||
return respose_result.ResponseResult.success(schedule_list) | ||
|
||
|
||
@app.get( | ||
"/schedule_week", tags=["课表"], summary="获取本周课表", description="获取本周课表" | ||
) | ||
def schedule_week(token: str = Header(None)): | ||
stud_id = auth.get_stud_id(token) | ||
cookie = auth.get_cookie(stud_id) | ||
client = ECJTU(cookie=cookie) | ||
try: | ||
schedule = client.scheduled_courses.this_week() | ||
except Exception as e: | ||
return respose_result.ResponseResult.error(str(e)) | ||
dict_list = [] | ||
for sublist in schedule: | ||
dict_sublist = [] | ||
for item in sublist: | ||
dict_sublist.append(dict(item)) | ||
dict_list.append(dict_sublist) | ||
return respose_result.ResponseResult.success(dict_list) | ||
|
||
|
||
# 成绩接口 | ||
@app.get("/score", tags=["成绩"], summary="获取当前成绩", description="获取当学期成绩") | ||
def score(token: str = Header(None)): | ||
stud_id = auth.get_stud_id(token) | ||
cookie = auth.get_cookie(stud_id) | ||
client = ECJTU(cookie=cookie) | ||
try: | ||
score = client.scores.today() | ||
except Exception as e: | ||
return respose_result.ResponseResult.error(str(e)) | ||
score_list = [] | ||
for sublist in score: | ||
score_list.append(dict(sublist)) | ||
return respose_result.ResponseResult.success(score_list) | ||
|
||
|
||
@app.get( | ||
"/score/{semester}", | ||
tags=["成绩"], | ||
summary="获取指定学期成绩", | ||
description="获取指定学期成绩,semester格式为yyyy.1或yyyy.2", | ||
) | ||
def score_semester(token: str = Header(None), semester: str = None): | ||
# semester(Optional[str]): The semester to filter, eg: 2023.1, 2023.2 | ||
if not re.match(r"\d{4}\.[12]", semester): | ||
return respose_result.ResponseResult.param_error("学期格式错误") | ||
stud_id = auth.get_stud_id(token) | ||
cookie = auth.get_cookie(stud_id) | ||
client = ECJTU(cookie=cookie) | ||
try: | ||
scores = client.scores.filter(semester=semester) | ||
except Exception as e: | ||
return respose_result.ResponseResult.error(str(e)) | ||
score_list = [] | ||
for sublist in scores: | ||
score_list.append(dict(sublist)) | ||
return respose_result.ResponseResult.success(score_list) | ||
|
||
|
||
# 选课情况接口 | ||
@app.get( | ||
"/elective_courses", | ||
tags=["选课情况"], | ||
summary="获取当前选课情况", | ||
description="获取当前学期选课情况", | ||
) | ||
def elective_courses(token: str = Header(None)): | ||
stud_id = auth.get_stud_id(token) | ||
cookie = auth.get_cookie(stud_id) | ||
client = ECJTU(cookie=cookie) | ||
try: | ||
elective_courses = client.elective_courses.today() | ||
except Exception as e: | ||
return respose_result.ResponseResult.error(str(e)) | ||
elective_courses_list = [] | ||
for sublist in elective_courses: | ||
elective_courses_list.append(dict(sublist)) | ||
return respose_result.ResponseResult.success(elective_courses_list) | ||
|
||
|
||
@app.get( | ||
"/elective_courses/{semester}", | ||
tags=["选课情况"], | ||
summary="获取指定学期选课情况", | ||
description="获取指定学期选课情况,semester格式为yyyy.1或yyyy.2", | ||
) | ||
def elective_courses_semester(token: str = Header(None), semester: str = None): | ||
# semester(Optional[str]): The semester to filter, eg: 2023.1, 2023.2 | ||
if not re.match(r"\d{4}\.[12]", semester): | ||
return respose_result.ResponseResult.param_error("学期格式错误") | ||
stud_id = auth.get_stud_id(token) | ||
cookie = auth.get_cookie(stud_id) | ||
client = ECJTU(cookie=cookie) | ||
try: | ||
elective_courses = client.elective_courses.filter(semester=semester) | ||
except Exception as e: | ||
return respose_result.ResponseResult.error(str(e)) | ||
elective_courses_list = [] | ||
for sublist in elective_courses: | ||
elective_courses_list.append(dict(sublist)) | ||
return respose_result.ResponseResult.success(elective_courses_list) | ||
|
||
|
||
# 启动api服务 | ||
def start_api_server(port=8080): | ||
import uvicorn | ||
|
||
uvicorn.run(app, host="127.0.0.1", port=port) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from ecjtu.ecjtu_API import...