diff --git a/requirements.txt b/requirements.txt index 3663d67..d4ecca7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ asyncpg aiomysql uvicorn[standart] aiologger +httpx diff --git a/src/app/services/updaters/fl_updater.py b/src/app/services/updaters/fl_updater.py index 9c793ad..c79af8c 100644 --- a/src/app/services/updaters/fl_updater.py +++ b/src/app/services/updaters/fl_updater.py @@ -8,6 +8,7 @@ from aiologger import Logger from core.config import env_config from app.services.updaters.base import BaseUpdater +from app.services.webhook import WebhookSender async def run(cmd) -> tuple[bytes, bytes, Optional[int]]: @@ -474,6 +475,8 @@ class FlUpdater(BaseUpdater): self._update_books_genres() ) + await WebhookSender.send() + return True @classmethod diff --git a/src/app/services/webhook.py b/src/app/services/webhook.py new file mode 100644 index 0000000..3dad1a4 --- /dev/null +++ b/src/app/services/webhook.py @@ -0,0 +1,21 @@ +import httpx + +from core.config import env_config, WebhookConfig + + +class WebhookSender: + @classmethod + async def _make_request(cls, webhook: WebhookConfig): + async with httpx.AsyncClient() as client: + request_maker= getattr(client, webhook.method) + await request_maker(webhook.url, headers=webhook.headers) + + @classmethod + async def send(cls): + webhooks = env_config.WEBHOOKS + + if webhooks is None: + return + + for webhook in webhooks: + await cls._make_request(webhook) diff --git a/src/core/config.py b/src/core/config.py index 41b0df2..609640b 100644 --- a/src/core/config.py +++ b/src/core/config.py @@ -1,4 +1,11 @@ -from pydantic import BaseSettings +from typing import Optional, Union, Literal +from pydantic import BaseModel, BaseSettings + + +class WebhookConfig(BaseModel): + method: Union[Literal['get'], Literal['post']] + url: str + headers: dict[str, str] class EnvConfig(BaseSettings): @@ -18,5 +25,7 @@ class EnvConfig(BaseSettings): FL_BASE_URL: str + WEBHOOKS: Optional[list[WebhookConfig]] + env_config = EnvConfig()