diff options
Diffstat (limited to 'app/utils.py')
-rw-r--r-- | app/utils.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/app/utils.py b/app/utils.py new file mode 100644 index 0000000..08373e0 --- /dev/null +++ b/app/utils.py @@ -0,0 +1,31 @@ +from pathlib import Path +from fastapi import Request +import httpx + + +def get_avatar_urls(): + 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): + return request.client.host + + +async def get_client_geo(client_host: str): + 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 + |