Skip to content

Commit

Permalink
增加 timeout 配置
Browse files Browse the repository at this point in the history
  • Loading branch information
Tosd0 committed Nov 2, 2024
1 parent 840e964 commit 9e7e0ca
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<a href="https://pypi.python.org/pypi/nonebot-plugin-osu-match-monitor">
<img src="https://img.shields.io/pypi/v/nonebot-plugin-osu-match-monitor.svg" alt="pypi">
</a>
<a href="https://www.codefactor.io/repository/github/sevenyine/nonebot-plugin-osu-match-monitor">
<img src="https://www.codefactor.io/repository/github/sevenyine/nonebot-plugin-osu-match-monitor/badge" alt="CodeFactor" /></a>

![GitHub Release](https://img.shields.io/github/v/release/Sevenyine/nonebot-plugin-osu-match-monitor)
<img src="https://img.shields.io/badge/python-3.9+-blue.svg" alt="python">

</div>
Expand Down Expand Up @@ -64,6 +68,7 @@ Welcome to osu! <img src="https://github.com/ppy/osu/blob/master/assets/lazer.pn
|:-----:|:----:|:----:|:----:|
| osu_api_key || "" | str |
| osu_refresh_interval || 2 | int |
| osu_api_timeout || 5 | int |

### 如何获取 osu! API Key?

Expand Down
7 changes: 4 additions & 3 deletions nonebot_plugin_osu_match_monitor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
plugin_config = get_plugin_config(Config)
api_key = plugin_config.osu_api_key
refresh_interval = plugin_config.osu_refresh_interval
api_timeout = plugin_config.osu_api_timeout
API_URL_MATCH = "https://osu.ppy.sh/api/get_match"
API_URL_USER = "https://osu.ppy.sh/api/get_user"
API_URL_BEATMAP = "https://osu.ppy.sh/api/get_beatmaps"
Expand Down Expand Up @@ -203,7 +204,7 @@ async def get_match_info(room_id: str) -> Dict:
"mp": room_id
}
try:
async with httpx.AsyncClient() as client:
async with httpx.AsyncClient(timeout=api_timeout) as client:
response = await client.get(API_URL_MATCH, params=params)
logger.debug(f"请求房间 {room_id} 的比赛信息,状态码:{response.status_code}")
if response.status_code == 200:
Expand All @@ -228,7 +229,7 @@ async def get_user_info(user_id: str) -> Dict:
"type": "id"
}
try:
async with httpx.AsyncClient() as client:
async with httpx.AsyncClient(timeout=api_timeout) as client:
response = await client.get(API_URL_USER, params=params)
if response.status_code == 200:
data = response.json()
Expand All @@ -251,7 +252,7 @@ async def get_beatmap_info(beatmap_id: str) -> Dict:
"limit": 1
}
try:
async with httpx.AsyncClient() as client:
async with httpx.AsyncClient(timeout=api_timeout) as client:
response = await client.get(API_URL_BEATMAP, params=params)
if response.status_code == 200:
data = response.json()
Expand Down
1 change: 1 addition & 0 deletions nonebot_plugin_osu_match_monitor/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
class Config(BaseModel):
osu_api_key: str = ""
osu_refresh_interval: int = 2
osu_api_timeout: int = 5

0 comments on commit 9e7e0ca

Please sign in to comment.