Skip to content

Commit

Permalink
Merge pull request #46 from longsihua2003/main
Browse files Browse the repository at this point in the history
add fastapi support
  • Loading branch information
Undertone0809 authored May 12, 2024
2 parents b33011e + 0a43994 commit d483e4e
Show file tree
Hide file tree
Showing 14 changed files with 2,055 additions and 169 deletions.
62 changes: 61 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,67 @@ async def main():
asyncio.run(main())
```

## 提供 web 服务器,提供 API 服务

### 启动方法
1. 通过python代码启动
```py
from ecjtu.ecjtu_api.api import start_api_server

def main():
start_api_server(port=8080)

if __name__ == "__main__":
main()
```
2. 通过命令行启动
```shell
python ecjtu/server.py --port 8080
```

### 使用方法
1. 启动之后,命令行会显示如下内容
```shell
INFO: Started server process [2545]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8080 (Press CTRL+C to quit)
```
2. 此时通过浏览器访问 http://127.0.0.1:8080 可以看到api在线调试文档

### 本项目提供的api接口

详细信息可以参考源代码当中examples/ecjtu-api.md当中

1. 登录
* post /login
通过学号和密码进行登录,获取access_token和refresh_token,access_token用于之后的所有请求,refresh_token用于刷新access_token

* post /refresh_token
当access_token过期时,可以使用refresh_token刷新access_token。
2. gpa

* get /gpa
获取当前gpa情况
3. 课表
* get /schedule
获取今日课表
* get /schedule/{date}
获取指定日期课表 date格式为2024-05-01
* get /schedule/week
获取本周课表
4. 成绩
* get /score
获取目前成绩
* /score/{semester}
获取指定学期成绩 semester格式为2023.1
5. 选课情况
* get /elective_courses
获取当前选课信息
* get /elective_courses/{semester}
获取指定学期选课信息 semester格式为2023.1


## 🧰 本地开发

欢迎贡献代码与二次开发,你可以通过以下方式安装依赖,推荐使用 Conda 作为环境管理工具,首先创建一个新的环境并激活:
Expand All @@ -281,7 +342,6 @@ poetry install

下面列举了一些未来可能添加的功能,欢迎贡献代码,提出建议。

- [ ] 添加 web 服务器,提供 API 服务
- [ ] 提供 vercel 一键部署
- [ ] 提供 docker 快速服务部署
- [ ] 增加考试查询
Expand Down
23 changes: 11 additions & 12 deletions ecjtu/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ def has_login(self) -> bool:

class ECJTU(BaseClient[httpx.Client], httpx.Client):
def __init__(
self, stud_id: Optional[str] = None, password: Optional[str] = None, **kwargs
self,
stud_id: Optional[str] = None,
password: Optional[str] = None,
cookie: Optional[CookieTypes] = None,
**kwargs,
) -> None:
"""Initialize ECJTU client.
Expand All @@ -73,9 +77,12 @@ def __init__(
"""
super().__init__(verify=False, **kwargs)

self.stud_id: str = stud_id or os.environ.get("ECJTU_STUDENT_ID")
self.password: str = password or os.environ.get("ECJTU_PASSWORD")
self.enc_password: str = _get_enc_password(self.password)
if cookie:
self.cookies = cookie
else:
self.stud_id: str = stud_id or os.environ.get("ECJTU_STUDENT_ID")
self.password: str = password or os.environ.get("ECJTU_PASSWORD")
self.enc_password: str = _get_enc_password(self.password)

self.scheduled_courses = crud.ScheduledCourseCRUD(self)
self.scores = crud.ScoreCRUD(self)
Expand Down Expand Up @@ -217,10 +224,6 @@ def login(self) -> None:

logger.info("Login successful")

def start_api_server(self, port: int = 8000):
# TODO: Start a FastAPI server
pass


class AsyncECJTU(BaseClient[httpx.AsyncClient], httpx.AsyncClient):
def __init__(self, stud_id: str, password: str, **kwargs) -> None:
Expand Down Expand Up @@ -377,7 +380,3 @@ async def login(self) -> None:
)

logger.info("Login successful")

async def start_api_server(self):
# TODO: Start a FastAPI server
pass
Loading

0 comments on commit d483e4e

Please sign in to comment.