Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Replace aiohttp with httpx in enkanetwork.py #635

Merged
merged 5 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions core/services/players/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@

from aiohttp import ClientConnectorError
from enkanetwork import (
EnkaNetworkAPI,
VaildateUIDError,
HTTPException,
EnkaPlayerNotFound,
PlayerInfo as EnkaPlayerInfo,
TimedOut,
)

from core.base_service import BaseService
from core.config import config
from core.dependence.redisdb import RedisDB
from core.services.players.models import PlayersDataBase as Player, PlayerInfoSQLModel, PlayerInfo
from core.services.players.repositories import PlayerInfoRepository
from utils.enkanetwork import RedisCache
from utils.enkanetwork import RedisCache, EnkaNetworkAPI
from utils.log import logger
from utils.patch.aiohttp import AioHttpTimeoutException

from gram_core.services.players.services import PlayersService

Expand Down Expand Up @@ -50,7 +49,7 @@ async def get_player_info_from_enka(self, player_id: int) -> Optional[EnkaPlayer
return response.player
except (VaildateUIDError, EnkaPlayerNotFound, HTTPException) as exc:
logger.warning("EnkaNetwork 请求失败: %s", str(exc))
except AioHttpTimeoutException as exc:
except TimedOut as exc:
logger.warning("EnkaNetwork 请求超时: %s", str(exc))
except ClientConnectorError as exc:
logger.warning("EnkaNetwork 请求错误: %s", str(exc))
Expand Down
7 changes: 3 additions & 4 deletions plugins/genshin/player_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from enkanetwork import (
DigitType,
EnkaNetworkAPI,
EnkaNetworkResponse,
EnkaServerError,
Equipments,
Expand All @@ -16,6 +15,7 @@
EnkaServerUnknown,
EnkaServerRateLimit,
EnkaPlayerNotFound,
TimedOut,
)
from pydantic import BaseModel
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
Expand All @@ -33,10 +33,9 @@
from metadata.shortname import roleToName
from modules.playercards.file import PlayerCardsFile
from modules.playercards.helpers import ArtifactStatsTheory
from utils.enkanetwork import RedisCache
from utils.enkanetwork import RedisCache, EnkaNetworkAPI
from utils.helpers import download_resource
from utils.log import logger
from utils.patch.aiohttp import AioHttpTimeoutException
from utils.uid import mask_number

if TYPE_CHECKING:
Expand Down Expand Up @@ -77,7 +76,7 @@ async def _update_enka_data(self, uid) -> Union[EnkaNetworkResponse, str]:
data = await self.player_cards_file.merge_info(uid, data)
await self.cache.set(uid, data)
return EnkaNetworkResponse.parse_obj(data)
except AioHttpTimeoutException:
except TimedOut:
error = "Enka.Network 服务请求超时,请稍后重试"
except EnkaServerRateLimit:
error = "Enka.Network 已对此API进行速率限制,请稍后重试"
Expand Down
99 changes: 97 additions & 2 deletions utils/enkanetwork.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import logging
import warnings
from typing import Dict, Any, Optional, TYPE_CHECKING

from enkanetwork import Cache
from cachetools import TTLCache
from enkanetwork.assets import Assets
from enkanetwork.cache import Cache
from enkanetwork.client import EnkaNetworkAPI as _EnkaNetworkAPI
from enkanetwork.config import Config
from enkanetwork.exception import TimedOut, NetworkError, EnkaServerError, ERROR_ENKA
from enkanetwork.http import HTTPClient as _HTTPClient, Route
from httpx import AsyncClient, TimeoutException, HTTPError, Timeout

try:
import ujson as jsonlib
except ImportError:
import json as jsonlib


if TYPE_CHECKING:
from redis import asyncio as aioredis

Expand Down Expand Up @@ -43,3 +51,90 @@ async def exists(self, key) -> int:
async def ttl(self, key) -> int:
qname = self.get_qname(key)
return await self.redis.ttl(qname)


class HTTPClient(_HTTPClient):
async def close(self) -> None:
if not self.client.is_closed:
await self.client.aclose()

def __init__(
self, *, key: Optional[str] = None, agent: Optional[str] = None, timeout: Optional[Any] = None
) -> None:
if timeout is None:
timeout = Timeout(
connect=5.0,
read=5.0,
write=5.0,
pool=1.0,
)

if agent is not None:
Config.init_user_agent(agent)
agent = agent or Config.USER_AGENT
if key is None:
warnings.warn("'key' has depercated.")
self.client = AsyncClient(timeout=timeout, headers={"User-Agent": agent})

async def request(self, route: Route, **kwargs: Any) -> Any:
method = route.method
url = route.url
username = route.username

try:
response = await self.client.request(method, url, **kwargs)
except TimeoutException as e:
raise TimedOut from e
except HTTPError as e:
raise NetworkError from e

_host = response.url.host

if response.is_error:
if _host == Config.ENKA_URL:
err = ERROR_ENKA.get(response.status_code, None)
if err:
raise err[0](err[1].format(uid=username))
raise EnkaServerError(f"Server error status code: {response.status_code}")

return {"status": response.status_code, "content": response.content}


class StaticCache(Cache):
def __init__(self, maxsize: int, ttl: int) -> None:
self.cache = TTLCache(maxsize, ttl)

async def get(self, key) -> Dict[str, Any]:
data = self.cache.get(key)
return jsonlib.loads(data) if data is not None else data

async def set(self, key, value) -> None:
self.cache[key] = jsonlib.dumps(value)


class EnkaNetworkAPI(_EnkaNetworkAPI):
def __init__(
self,
*,
lang: str = "en",
debug: bool = False,
key: str = "",
cache: bool = True,
user_agent: str = "",
timeout: int = 10,
) -> None: # noqa: E501
# Logging
logging.basicConfig()
logging.getLogger("enkanetwork").setLevel(logging.DEBUG if debug else logging.ERROR) # noqa: E501

# Set language and load config
self.assets = Assets(lang)

# Cache
self._enable_cache = cache
if self._enable_cache:
Config.init_cache(StaticCache(1024, 60 * 1))

# http client
self.__http = HTTPClient(key=key, agent=user_agent, timeout=timeout) # skipcq: PTC-W0037
self._closed = False
Loading