Skip to content

Commit

Permalink
🎨 优化在相关凭证未填写时的行为
Browse files Browse the repository at this point in the history
related: #53
  • Loading branch information
mix committed Jul 16, 2021
1 parent fb5fb19 commit 81da5e3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
27 changes: 18 additions & 9 deletions hibiapi/app/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List, Protocol, cast

from hibiapi.utils.config import APIConfig
from hibiapi.utils.exceptions import ExceptionReturn
from hibiapi.utils.log import logger
Expand All @@ -14,17 +16,24 @@
}
)

modules = [bilibili, netease, pixiv, qrcode, sauce, tieba]

class RouteInterface(Protocol):
router: SlashRouter
__mount__: str
__config__: APIConfig


modules = cast(List[RouteInterface], [bilibili, netease, pixiv, qrcode, sauce, tieba])

for module in modules:
route: SlashRouter = getattr(module, "router")
mount: str = getattr(module, "__mount__")
config: APIConfig = getattr(module, "__config__")
mount = mount if mount.startswith("/") else ("/" + mount)
if not config["enabled"].as_bool():
mount = (
mountpoint
if (mountpoint := module.__mount__).startswith("/")
else ("/" + mountpoint)
)
if not module.__config__["enabled"].as_bool():
logger.warning(
f"API Route <y><b>{mount}</b></y> <e>{route}</e> "
"has been <r><b>disabled</b></r> in config."
f"API Route <y><b>{mount}</b></y> has been <r><b>disabled</b></r> in config."
)
continue
router.include_router(route, prefix=mount)
router.include_router(module.router, prefix=mount)
5 changes: 4 additions & 1 deletion hibiapi/app/routes/pixiv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Callable, Coroutine, NoReturn, Optional

from fastapi import Depends, Request

from hibiapi.api.pixiv import (
EndpointsType,
IllustType,
Expand All @@ -19,6 +18,10 @@
from hibiapi.utils.log import logger
from hibiapi.utils.routing import SlashRouter, exclude_params

if not PixivConstants.CONFIG["account"]["token"].get():
logger.warning("Pixiv API token is not set, pixiv endpoint will be unavailable.")
PixivConstants.CONFIG["enabled"].set(False)

__mount__, __config__ = "pixiv", PixivConstants.CONFIG
router = SlashRouter(tags=["Pixiv"])

Expand Down
5 changes: 5 additions & 0 deletions hibiapi/app/routes/sauce.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional

from fastapi import Depends, File, Form
from loguru import logger

from hibiapi.api.sauce import (
DeduplicateType,
Expand All @@ -12,6 +13,10 @@
)
from hibiapi.utils.routing import SlashRouter

if not SauceConstants.API_KEY.strip():
logger.warning("Sauce API key not set, SauceNAO endpoint will be unavailable")
SauceConstants.CONFIG["enabled"].set(False)

__mount__, __config__ = "sauce", SauceConstants.CONFIG
router = SlashRouter(tags=["SauceNAO"])

Expand Down

1 comment on commit 81da5e3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cloc github.com/AlDanial/cloc v 1.82 T=0.10 s (802.7 files/s, 63111.0 lines/s)
Language files blank comment code
Python 56 1093 577 3769
YAML 13 72 30 283
JSON 4 0 0 132
Markdown 1 49 0 81
Mustache 1 15 1 42
TOML 1 4 0 37
Dockerfile 1 6 0 10
INI 1 2 0 7
SVG 1 0 0 1
-------- -------- -------- -------- --------
SUM: 79 1241 608 4362

Please sign in to comment.