From 0a45919f222c3ee623441c228875fa2ad058dad7 Mon Sep 17 00:00:00 2001 From: "M307 (Mac)" Date: Mon, 6 Feb 2023 22:05:21 +0700 Subject: [PATCH] Add config.py and move some variable to config --- enkanetwork/config.py | 31 +++++++++++++++++++++++++++++++ enkanetwork/utils.py | 22 +++++++++++++++------- 2 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 enkanetwork/config.py diff --git a/enkanetwork/config.py b/enkanetwork/config.py new file mode 100644 index 0000000..18cc1f2 --- /dev/null +++ b/enkanetwork/config.py @@ -0,0 +1,31 @@ +from typing import ( + ClassVar +) +from .utils import get_user_agent +from .cache import Cache + +class Config: + # HTTP Config + ENKA_PROTOCOL: ClassVar[str] = "https" + ENKA_URL: ClassVar[str] = "dev.enka.network" + # Assets + ASSETS_PROTOCOL: ClassVar[str] = "https" + ASSETS_URL: ClassVar[str] = "raw.githubusercontent.com" + # Header Config + USER_AGENT: ClassVar[str] = get_user_agent() + # Client config + CACHE_ENABLED: ClassVar[bool] = True + CACHE: ClassVar[Cache] = Cache(1024, 60 * 3) + + @classmethod + def init_cache( + cls, + cache: Cache, + enabled: bool = True + ): + cls.CACHE = cache + cls.CACHE_ENABLED = enabled + + @classmethod + def init_user_agent(cls, agent: str): + cls.USER_AGENT = agent \ No newline at end of file diff --git a/enkanetwork/utils.py b/enkanetwork/utils.py index 181c6cc..412497f 100644 --- a/enkanetwork/utils.py +++ b/enkanetwork/utils.py @@ -66,14 +66,22 @@ def get_default_header(): python_version = sys.version_info return { - "User-Agent": "EnkaNetwork.py/{version} (Python {major}.{minor}.{micro})".format( - version=__version__, - major=python_version.major, - minor=python_version.minor, - micro=python_version.micro - ), + "User-Agent": get_user_agent(), } + +def get_user_agent(): + # Get python version + python_version = sys.version_info + + return "EnkaNetwork.py/{version} (Python {major}.{minor}.{micro})".format( + version=__version__, + major=python_version.major, + minor=python_version.minor, + micro=python_version.micro + ) + + class _MissingSentinel: __slots__ = () @@ -92,6 +100,7 @@ class _MissingSentinel: MISSING: Any = _MissingSentinel() + async def to_data(response: ClientResponse) -> Dict[str, Any]: data = bytearray() @@ -113,4 +122,3 @@ async def to_data(response: ClientResponse) -> Dict[str, Any]: "content": data } return content -