From 2c8cbe753e31220ac8ec9d4eba12e7c3ee5fc199 Mon Sep 17 00:00:00 2001 From: Redsandyg Date: Sun, 22 Jun 2025 14:39:14 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=20call=5Fvalidation=5Fpromo=5Fapi.py=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=BC=D0=BE=D0=BA=D0=BE=D0=B4=D0=BE=D0=B2?= =?UTF-8?q?=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20API.=20=D0=A0=D0=B5=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=B8=D0=BA=D0=B0=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20JWT=20=D1=82=D0=BE=D0=BA=D0=B5=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=B8=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=BC=D0=BE=D0=BA=D0=BE=D0=B4=D0=B0.=20?= =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8=20=D0=B2=20integration=5Fmodels.?= =?UTF-8?q?py=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80?= =?UTF-8?q?=D0=B6=D0=BA=D0=B8=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=B8=20=D0=BE=D1=82=D0=B2=D0=B5=D1=82=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8E=20=D0=BF=D1=80=D0=BE=D0=BC=D0=BE=D0=BA=D0=BE=D0=B4=D0=BE?= =?UTF-8?q?=D0=B2.=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=8D=D0=BD=D0=B4=D0=BF=D0=BE?= =?UTF-8?q?=D0=B8=D0=BD=D1=82=20/validationPromo=20=D0=B2=20integration=5F?= =?UTF-8?q?api.py=20=D0=B4=D0=BB=D1=8F=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=BA=D0=B8=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=BD=D0=B0=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8E=20=D0=BF=D1=80=D0=BE=D0=BC=D0=BE=D0=BA?= =?UTF-8?q?=D0=BE=D0=B4=D0=BE=D0=B2.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- call_validation_promo_api.py | 63 ++++++++++++++++++++++++++++++++++++ integration_api.py | 21 +++++++++++- integration_models.py | 8 ++++- 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 call_validation_promo_api.py diff --git a/call_validation_promo_api.py b/call_validation_promo_api.py new file mode 100644 index 0000000..9022c9b --- /dev/null +++ b/call_validation_promo_api.py @@ -0,0 +1,63 @@ +import requests +import json + +# Конфигурация API +BASE_URL = "http://127.0.0.1:8001" +API_KEY = "9efb2df0-03d7-4212-93db-0ae6418667e1" # API-ключ +PROMOCODE = "brw0OMOz" + +# Данные для запроса на валидацию промокода +validation_data = { + "promocode": PROMOCODE, +} + +# Эндпоинты +token_endpoint = f"{BASE_URL}/token" +validation_endpoint = f"{BASE_URL}/validationPromo" + +# Шаг 1: Получение JWT токена +print(f"Отправка запроса на получение токена на {token_endpoint}") +token_headers = { + "X-API-Key": API_KEY, + "Content-Type": "application/json" +} + +try: + token_response = requests.post(token_endpoint, headers=token_headers) + token_response.raise_for_status() + + token_data = token_response.json() + jwt_token = token_data["access_token"] + print("JWT токен успешно получен.") + +except requests.exceptions.RequestException as e: + print(f"Произошла ошибка при получении токена: {e}") + if hasattr(e, 'response') and e.response is not None: + try: + print("Тело ответа с ошибкой:", e.response.json()) + except json.JSONDecodeError: + print("Тело ответа с ошибкой (не JSON):", e.response.text) + exit() # Прерываем выполнение, если не удалось получить токен + +# Шаг 2: Вызов эндпоинта /validationPromo с использованием полученного JWT токена +headers_with_jwt = { + "Authorization": f"Bearer {jwt_token}", + "Content-Type": "application/json" +} + +print(f"Отправка запроса на {validation_endpoint} с данными: {validation_data}") + +try: + validation_response = requests.post(validation_endpoint, headers=headers_with_jwt, data=json.dumps(validation_data)) + validation_response.raise_for_status() # Вызовет исключение для ошибок HTTP (4xx или 5xx) + + print("Статус ответа:", validation_response.status_code) + print("Тело ответа:", validation_response.json()) + +except requests.exceptions.RequestException as e: + print(f"Произошла ошибка при вызове API validationPromo: {e}") + if hasattr(e, 'response') and e.response is not None: + try: + print("Тело ответа с ошибкой:", e.response.json()) + except json.JSONDecodeError: + print("Тело ответа с ошибкой (не JSON):", e.response.text) \ No newline at end of file diff --git a/integration_api.py b/integration_api.py index 1d2b41f..41ae069 100644 --- a/integration_api.py +++ b/integration_api.py @@ -8,7 +8,7 @@ from random import choices import string from sql_models import Company, IntegrationToken, Ref, Sale, AgentTransaction, PartnerTransaction, AgentBalance, TgAgent, CompanyBalance, SaleCategory -from integration_models import Token, SaleCreateRequest, SaleCreateResponse, TransactionStatus, WithdrawRequest, WithdrawResponse +from integration_models import Token, SaleCreateRequest, SaleCreateResponse, TransactionStatus, WithdrawRequest, WithdrawResponse, PromoValidationRequest, PromoValidationResponse from bff_models import RegisterResponse, TgAuthResponse from tg_models import RefAddRequest, RefResponse, RefAddResponse, RefStatResponse, RegisterRequest, StatResponse from helpers_bff import AUTH_DB_ENGINE, get_integration_db, create_integration_jwt_token, get_current_company_from_jwt, get_tg_agent_by_tg_id, get_current_tg_agent @@ -289,6 +289,25 @@ async def create_sale( "crediting": new_sale.crediting } +@app.post("/validationPromo", tags=["integration"], response_model=PromoValidationResponse) +async def validate_promocode( + req: PromoValidationRequest, + company: Company = Depends(get_current_company_from_jwt), + db: Session = Depends(get_integration_db) +): + """ + Проверяет валидность промокода для текущей компании. + """ + referral = db.exec(select(Ref).where(Ref.promocode == req.promocode)).first() + if not referral: + return {"validation": False} + + tg_agent = db.exec(select(TgAgent).where(TgAgent.id == referral.tg_agent_id, TgAgent.company_id == company.id)).first() + if not tg_agent: + return {"validation": False} + + return {"validation": True} + @app.post("/register", tags=["agent-tg"], response_model=RegisterResponse) def register(req: RegisterRequest, db: Session = Depends(get_integration_db)): """ diff --git a/integration_models.py b/integration_models.py index 77e6194..db995ec 100644 --- a/integration_models.py +++ b/integration_models.py @@ -42,4 +42,10 @@ class TransactionStatus(str, Enum): WAITING = "waiting" DONE = "done" CANCELED = "canceled" - ERROR = "error" \ No newline at end of file + ERROR = "error" + +class PromoValidationRequest(BaseModel): + promocode: str + +class PromoValidationResponse(BaseModel): + validation: bool \ No newline at end of file