diff --git a/pyproject.toml b/pyproject.toml index e690bd7..5d31a51 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,8 +15,8 @@ redis = { extras = ["hiredis"], version = "^5.0.1" } ariadne = "^0.21" starlette = "^0.32" uvicorn = "^0.24" -httpx = "^0.25.0" itsdangerous = "^2.1.2" +aiohttp = "^3.9.1" [tool.poetry.dev-dependencies] pytest = "^7.4.2" diff --git a/services/auth.py b/services/auth.py index 0a8fee0..988749d 100644 --- a/services/auth.py +++ b/services/auth.py @@ -1,5 +1,5 @@ from functools import wraps -from httpx import AsyncClient +import aiohttp from services.core import get_author from settings import AUTH_URL @@ -15,21 +15,21 @@ async def check_auth(req): operation = "GetUserId" gql = { - "query": query_type + " " + operation + " { " + query_name + " { user { id } } " + " }", + "query": query_type + " " + operation + " { " + query_name + " { user { id } } }", "operationName": operation, "variables": None, } - async with AsyncClient(timeout=30.0) as client: - response = await client.post(AUTH_URL, headers=headers, json=gql) - print(f"[services.auth] {AUTH_URL} response: {response.status_code}") - if response.status_code != 200: - return False, None - r = response.json() - if r: - user_id = r.get("data", {}).get(query_name, {}).get("user", {}).get("id", None) - is_authenticated = user_id is not None - return is_authenticated, user_id + async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30.0)) as session: + async with session.post(AUTH_URL, headers=headers, json=gql) as response: + print(f"[services.auth] {AUTH_URL} response: {response.status}") + if response.status != 200: + return False, None + r = await response.json() + if r: + user_id = r.get("data", {}).get(query_name, {}).get("user", {}).get("id", None) + is_authenticated = user_id is not None + return is_authenticated, user_id return False, None @@ -43,12 +43,9 @@ def login_required(f): if not is_authenticated: raise Exception("You are not logged in") else: - # Добавляем author_id в контекст - author = await get_author(user_id) - if author: - context["author_id"] = author.id - elif user_id: - context["user_id"] = user_id + # Добавляем author_id и user_id в контекст + context["author_id"] = await get_author(user_id) + context["user_id"] = user_id # Если пользователь аутентифицирован, выполняем резолвер return await f(*args, **kwargs) diff --git a/services/core.py b/services/core.py index 6114ae2..abe8733 100644 --- a/services/core.py +++ b/services/core.py @@ -1,24 +1,23 @@ -from httpx import AsyncClient +import aiohttp from settings import API_BASE from typing import List, Any from models.member import ChatMember - headers = {"Content-Type": "application/json"} async def _request_endpoint(query_name, body) -> Any: - async with AsyncClient() as client: + async with aiohttp.ClientSession() as session: try: - response = await client.post(API_BASE, headers=headers, json=body) - print(f"[services.core] {query_name}: [{response.status_code}] {len(response.text)} bytes") - if response.status_code != 200: - return [] - r = response.json() - if r: - return r.get("data", {}).get(query_name, {}) - else: - raise Exception("json response error") + async with session.post(API_BASE, headers=headers, json=body) as response: + print(f"[services.core] {query_name}: [{response.status}] {len(await response.text())} bytes") + if response.status != 200: + return [] + r = await response.json() + if r: + return r.get("data", {}).get(query_name, {}) + else: + raise Exception("json response error") except Exception: import traceback