diff options
author | Stas Medvedev <medvedevsa97@gmail.com> | 2024-06-12 16:05:12 +0300 |
---|---|---|
committer | Stas Medvedev <medvedevsa97@gmail.com> | 2024-06-12 16:05:12 +0300 |
commit | ed49bb17b9e93a1406ab51f7dca5906661863627 (patch) | |
tree | fd5dfa78e46c9b29dcd1fdaf3897fbd117cf7c2b /utils | |
parent | 3805407b0868aeccf7462a68c1d86540764bc02f (diff) |
refactore utils
refactore html about, client_geo
Diffstat (limited to 'utils')
-rw-r--r-- | utils/__init__.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..22d7058 --- /dev/null +++ b/utils/__init__.py @@ -0,0 +1,32 @@ +from pathlib import Path +from typing import Annotated + +from fastapi import Request, Header, Depends +import httpx + + +def get_avatar_urls() -> list[str]: + path = Path("./static") / "avatars" + if not path.exists(): + path.mkdir() + + return sorted([str(jpg_avatar) for jpg_avatar in path.glob("*.jpg")]) + + +def get_client_host( + request: Request, x_real_ip: Annotated[str | None, Header()] = None +) -> str: + if x_real_ip: + return x_real_ip + return request.client.host + + +async def get_client_geo(client_host: Annotated[str, Depends(get_client_host)]) -> dict: + async with httpx.AsyncClient() as client: + response = await client.get( + # использование https платная опция сервиса, инфо тут https://members.ip-api.com/ + url="http://ip-api.com/json/{}".format(client_host), + params={"lang": "ru"}, + ) + data = response.json() + return data |