Skip to content

Commit

Permalink
feat: support Dotenv
Browse files Browse the repository at this point in the history
  • Loading branch information
Ljzd-PRO committed Nov 6, 2023
1 parent ded020c commit 153a8a2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
2 changes: 2 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KTOOLBOX_API__SCHEME=https
KTOOLBOX_API__NETLOC=kemono.su
10 changes: 5 additions & 5 deletions ktoolbox/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class APITenacityStop(stop_base):
"""APIs Stop strategies"""

def __call__(self, retry_state: RetryCallState) -> bool:
if config.api_retry_times is None:
if config.api.retry_times is None:
return stop_never(retry_state)
else:
return stop_after_attempt(config.api_retry_times)(retry_state)
return stop_after_attempt(config.api.retry_times)(retry_state)


class APIRet(BaseRet[_T]):
Expand All @@ -48,7 +48,7 @@ def retry(*args, **kwargs):
"""Wrap a function with a new `Retrying` object"""
wrapper = tenacity.retry(
stop=APITenacityStop(),
wait=wait_fixed(config.api_retry_interval),
wait=wait_fixed(config.api.retry_interval),
retry=retry_if_result(lambda x: not bool(x)),
reraise=True,
retry_error_callback=lambda x: x.outcome.result(), # type: Callable[[RetryCallState], Any]
Expand Down Expand Up @@ -94,14 +94,14 @@ async def request(cls, path: str = None, **kwargs) -> APIRet[_T]:
"""
if path is None:
path = cls.path
url_parts = [config.api_scheme, config.api_netloc, f"{config.api_path}{path}", '', '', '']
url_parts = [config.api.scheme, config.api.netloc, f"{config.api.path}{path}", '', '', '']
url = urlunparse(url_parts)
try:
async with httpx.AsyncClient() as client:
res = await client.request(
method=cls.method,
url=url,
timeout=config.api_timeout,
timeout=config.api.timeout,
follow_redirects=True,
**kwargs
)
Expand Down
4 changes: 2 additions & 2 deletions ktoolbox/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def get_creator_icon(creator_id: str, service: str) -> str:
:return: The icon URL.
"""
url_parts = [config.api_scheme, config.statics_host, f"/icons/{service}/{creator_id}", '', '', '']
url_parts = [config.api.scheme, config.api.statics_host, f"/icons/{service}/{creator_id}", '', '', '']
return urlunparse(url_parts)


Expand All @@ -21,5 +21,5 @@ def get_creator_banner(creator_id: str, service: str) -> str:
:return: The banner URL.
"""
url_parts = [config.api_scheme, config.statics_host, f"/banners/{service}/{creator_id}", '', '', '']
url_parts = [config.api.scheme, config.api.statics_host, f"/banners/{service}/{creator_id}", '', '', '']
return urlunparse(url_parts)
30 changes: 21 additions & 9 deletions ktoolbox/configuration.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
from typing import Literal

from pydantic import BaseModel
from pydantic_settings import BaseSettings, SettingsConfigDict

__all__ = ["config", "Configuration"]


class Configuration(BaseModel):
api_scheme: Literal["http", "https"] = "https"
class APIConfiguration(BaseModel):
scheme: Literal["http", "https"] = "https"
# noinspection SpellCheckingInspection
api_netloc: str = "kemono.su"
api_path: str = "/api/v1"
api_timeout: float = 5.0
api_retry_times: int = 3
api_retry_interval: float = 2.0

netloc: str = "kemono.su"
# noinspection SpellCheckingInspection
statics_host: str = "img.kemono.su"
path: str = "/api/v1"
timeout: float = 5.0
retry_times: int = 3
retry_interval: float = 2.0


class Configuration(BaseSettings):
api: APIConfiguration = APIConfiguration()

# noinspection SpellCheckingInspection
model_config = SettingsConfigDict(
env_prefix='ktoolbox_',
env_nested_delimiter='__',
env_file='.env',
env_file_encoding='utf-8'
)


config = Configuration()
config = Configuration(_env_file='prod.env')
Empty file added prod.env
Empty file.

0 comments on commit 153a8a2

Please sign in to comment.