Skip to content

Commit

Permalink
✨ 使用 yarl.URL 处理 outer_url
Browse files Browse the repository at this point in the history
  • Loading branch information
AzideCupric committed Mar 8, 2024
1 parent 449475a commit 7b89973
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion nonebot_bison/admin_page/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def register_get_token_handler():
@get_token.handle()
async def send_token(bot: "Bot", event: PrivateMessageEvent, state: T_State):
token = tm.get_user_token((event.get_user_id(), event.sender.nickname))
await get_token.finish(f"请访问: {plugin_config.outer_url}auth/{token}")
await get_token.finish(f"请访问: {plugin_config.outer_url / 'auth' / token}")

get_token.__help__name__ = "获取后台管理地址" # type: ignore
get_token.__help__info__ = "获取管理bot后台的地址,该地址会在一段时间过后过期,请不要泄漏该地址" # type: ignore
Expand Down
8 changes: 6 additions & 2 deletions nonebot_bison/plugin_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import nonebot
from yarl import URL
from nonebot import get_plugin_config
from pydantic import Field, BaseModel

Expand Down Expand Up @@ -32,8 +33,11 @@ class PlugConfig(BaseModel):
bison_platform_theme: dict[PlatformName, ThemeName] = {}

@property
def outer_url(self) -> str:
return self.bison_outer_url or f"http://localhost:{global_config.port}/bison/"
def outer_url(self) -> URL:
if self.bison_outer_url:
return URL(self.bison_outer_url)
else:
return URL(f"http://localhost:{global_config.port}/bison/")


plugin_config = get_plugin_config(PlugConfig)
4 changes: 2 additions & 2 deletions tests/test_admin_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ async def test_command(app: App):
to_me=True,
)
ctx.receive_event(bot, event_1)
ctx.should_call_send(event_1, f"请访问: {plugin_config.outer_url}auth/test_token", True)
ctx.should_call_send(event_1, f"请访问: {plugin_config.outer_url / 'auth' / 'test_token'}", True)
ctx.should_finished()

event_2 = fake_private_message_event(message=Message("管理后台"), sender=fake_admin_user, to_me=True)
ctx.receive_event(bot, event_2)
ctx.should_call_send(event_2, f"请访问: {plugin_config.outer_url}auth/test_token", True)
ctx.should_call_send(event_2, f"请访问: {plugin_config.outer_url / 'auth' / 'test_token'}", True)
ctx.should_finished()


Expand Down

0 comments on commit 7b89973

Please sign in to comment.