diff --git a/common_lib/services/achievement_service.py b/common_lib/services/achievement_service.py new file mode 100644 index 0000000..c7181c7 --- /dev/null +++ b/common_lib/services/achievement_service.py @@ -0,0 +1,41 @@ +import asyncio +import httpx +from fastapi import HTTPException, status +from config import settings +from common_lib.utils.http_client import client + + +async def send_update_increment_to_achievement_service( + user_id: str, + achievement_code: str, + increment: int, + with_httpexception=False, + sleep: int = None +) -> dict: + if sleep: + await asyncio.sleep(sleep) + + try: + response = await client.post( + f"{settings.ACHIEVEMENT_SERVICE}/internal/progress-update", + json={ + "user_id": str(user_id), + "achievement_code": str(achievement_code), + "increment": int(increment) + }) + + if response.status_code != 200: + if with_httpexception: + raise HTTPException( + status_code=response.status_code, + detail=f"achievement_service: {response.text}" + ) + return response.status_code, f"achievement_service: {response.text}" + + wrapped = response.json() + return 200, wrapped + + except httpx.RequestError as e: + if with_httpexception: + raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=f"achievement_service unreachable: {str(e)}") + return 503, f"achievement_service unreachable: {str(e)}" diff --git a/pyproject.toml b/pyproject.toml index 343e53e..e60641d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "common-lib" -version = "0.0.48" +version = "0.0.49" description = "Библиотека общих компонентов для микросервисов yobble" authors = [{ name = "cheykrym", email = "you@example.com" }] license = "MIT"