Skip to content

Commit 9c13b25

Browse files
committed
fix: Improve plugin api;
1 parent 017b06a commit 9c13b25

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

Models/plugins.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ def on_unload(self) -> None:
2222
pass
2323

2424
@abstractmethod
25-
def search(self, keyword: str) -> list[BaseComicInfo]:
25+
def search(self, keyword: str, **kwargs) -> list[BaseComicInfo]:
2626
pass
2727

2828
@abstractmethod
29-
def album(self, album_id: str) -> ComicInfo:
29+
def album(self, album_id: str, **kwargs) -> ComicInfo:
3030
pass
3131

3232

Models/requests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ class SourceStorageReq(BaseModel):
99
class ComicSearchReq(BaseModel):
1010
sources: list[str]
1111
keyword: str
12+
extras: dict[str, str] | None = None

Routers/comic.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,50 @@
11
from fastapi import APIRouter, Depends, HTTPException
22

3+
from Models.comic import BaseComicInfo, ComicInfo
34
from Models.requests import ComicSearchReq
4-
from Models.response import StandardResponse
5+
from Models.response import BaseResponse, StandardResponse
56
from Models.user import User, UserData
67
from Services.Modulator.manager import plugin_manager
78
from Services.Security.user import get_current_user, get_user_data
89

910
comic_router = APIRouter(prefix="/comic")
1011

1112

12-
@comic_router.post("/search")
13-
async def search_comic(body: ComicSearchReq, user: User = Depends(get_current_user)):
14-
pass
13+
@comic_router.post("/search", response_model=BaseResponse[list[BaseComicInfo]])
14+
async def search_comic(
15+
body: ComicSearchReq, user: User = Depends(get_current_user)
16+
) -> StandardResponse[list[BaseComicInfo]]:
17+
# TODO: Performence imporvement
18+
result = []
19+
for source in plugin_manager.plugins:
20+
resp = source.instance.search(body.keyword, *body.extras)
21+
22+
result.extend(resp)
23+
24+
return StandardResponse[list[BaseComicInfo]](data=result)
25+
26+
27+
@comic_router.get("/{src_id}/album/{album_id}", response_model=BaseResponse[ComicInfo])
28+
async def get_album(src_id: str, album_id: str) -> StandardResponse[ComicInfo]:
29+
if (source := plugin_manager.get_source(src_id)) is None:
30+
raise HTTPException(status_code=404, detail="Source not found")
31+
32+
return StandardResponse[ComicInfo](data=source.instance.album(album_id))
1533

1634

17-
@comic_router.get("/{src_id}/favor")
35+
@comic_router.get("/{src_id}/favor", response_model=BaseResponse[list[BaseComicInfo]])
1836
async def get_favor(
1937
src_id: str,
2038
data: dict[str, str] | None = None,
2139
user_data: UserData = Depends(get_user_data),
22-
):
40+
) -> StandardResponse[list[BaseComicInfo]]:
2341
if (source := plugin_manager.get_source(src_id)) is None:
2442
raise HTTPException(status_code=404, detail="Source not found")
2543

2644
if (resp := source.try_call("get_favor", user_data, data)) is not None:
2745
return resp
2846

29-
return StandardResponse(status_code=404, message="Not Found")
30-
31-
32-
@comic_router.get("/{src_id}/album/{album_id}")
33-
async def get_album(src_id: str, album_id: str, user: User = Depends(get_current_user)):
34-
if (source := plugin_manager.get_source(src_id)) is None:
35-
raise HTTPException(status_code=404, detail="Source not found")
36-
37-
pass
47+
return StandardResponse(status_code=400, message="Source not support")
3848

3949

4050
@comic_router.get("/{src_id}/album/{album_id}/images/{chapter_id}")

0 commit comments

Comments
 (0)