From c27883b177b06c1483e16eb0175c31652036454e Mon Sep 17 00:00:00 2001 From: ITQ Date: Thu, 23 Jan 2025 21:40:51 +0300 Subject: [PATCH] feat: added antifraud interactor --- solution/config/integrations/__init__.py | 73 ++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/solution/config/integrations/__init__.py b/solution/config/integrations/__init__.py index e69de29..6e5ba8d 100644 --- a/solution/config/integrations/__init__.py +++ b/solution/config/integrations/__init__.py @@ -0,0 +1,73 @@ +from datetime import datetime +from http import HTTPStatus as status +from typing import ClassVar + +import httpx +from django.core.cache import cache +from django.utils import timezone + + +class AntifraudServiceInteractor: + HEADERS: ClassVar[dict[str, str]] = {"Content-Type": "application/json"} + CACHE_PREFIX = "antifraud_cache" + + def __init__(self, endpoint: str) -> None: + self.antifraud_endpoint = f"{endpoint}/api/validate" + + @classmethod + def get_cache_key(cls, user_email: str, promo_id: str) -> None: + return f"{cls.CACHE_PREFIX}:{user_email}:{promo_id}" + + @classmethod + def is_cache_valid(cls, cache_until: str) -> bool: + if cache_until: + return ( + datetime.fromisoformat(cache_until).replace( + tzinfo=timezone.utc, + ) + > timezone.now() + ) + return False + + @classmethod + def validate(cls, user_email: str, promo_id: str) -> dict[str, bool | str]: + cache_key = cls.get_cache_key(user_email, promo_id) + cached_result = cache.get(cache_key) + + if cached_result and cls.is_cache_valid( + cached_result.get("cache_until"), + ): + return cached_result + + payload = {"user_email": user_email, "promo_id": promo_id} + try: + with httpx.Client(timeout=5) as client: + response = client.post( + cls.antifraud_endpoint, + json=payload, + headers=cls.HEADERS, + ) + + if response.status_code == status.OK: + result = response.json() + + if "cache_until" in result: + cache.set(cache_key, result) + + return result + + retry_response = client.post( + cls.antifraud_endpoint, + json=payload, + headers=cls.HEADERS, + ) + if retry_response.status_code == status.OK: + result = retry_response.json() + if "cache_until" in result: + cache.set(cache_key, result) + return result + + except httpx.HTTPError: + pass + + return {"ok": False}