This commit is contained in:
Untone 2023-11-30 09:49:23 +03:00
parent 37f8dd2f53
commit 64db057706
3 changed files with 27 additions and 31 deletions

View File

@ -15,8 +15,8 @@ redis = { extras = ["hiredis"], version = "^5.0.1" }
ariadne = "^0.21" ariadne = "^0.21"
starlette = "^0.32" starlette = "^0.32"
uvicorn = "^0.24" uvicorn = "^0.24"
httpx = "^0.25.0"
itsdangerous = "^2.1.2" itsdangerous = "^2.1.2"
aiohttp = "^3.9.1"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
pytest = "^7.4.2" pytest = "^7.4.2"

View File

@ -1,5 +1,5 @@
from functools import wraps from functools import wraps
from httpx import AsyncClient import aiohttp
from services.core import get_author from services.core import get_author
from settings import AUTH_URL from settings import AUTH_URL
@ -15,17 +15,17 @@ async def check_auth(req):
operation = "GetUserId" operation = "GetUserId"
gql = { gql = {
"query": query_type + " " + operation + " { " + query_name + " { user { id } } " + " }", "query": query_type + " " + operation + " { " + query_name + " { user { id } } }",
"operationName": operation, "operationName": operation,
"variables": None, "variables": None,
} }
async with AsyncClient(timeout=30.0) as client: async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30.0)) as session:
response = await client.post(AUTH_URL, headers=headers, json=gql) async with session.post(AUTH_URL, headers=headers, json=gql) as response:
print(f"[services.auth] {AUTH_URL} response: {response.status_code}") print(f"[services.auth] {AUTH_URL} response: {response.status}")
if response.status_code != 200: if response.status != 200:
return False, None return False, None
r = response.json() r = await response.json()
if r: if r:
user_id = r.get("data", {}).get(query_name, {}).get("user", {}).get("id", None) user_id = r.get("data", {}).get(query_name, {}).get("user", {}).get("id", None)
is_authenticated = user_id is not None is_authenticated = user_id is not None
@ -43,11 +43,8 @@ def login_required(f):
if not is_authenticated: if not is_authenticated:
raise Exception("You are not logged in") raise Exception("You are not logged in")
else: else:
# Добавляем author_id в контекст # Добавляем author_id и user_id в контекст
author = await get_author(user_id) context["author_id"] = await get_author(user_id)
if author:
context["author_id"] = author.id
elif user_id:
context["user_id"] = user_id context["user_id"] = user_id
# Если пользователь аутентифицирован, выполняем резолвер # Если пользователь аутентифицирован, выполняем резолвер

View File

@ -1,20 +1,19 @@
from httpx import AsyncClient import aiohttp
from settings import API_BASE from settings import API_BASE
from typing import List, Any from typing import List, Any
from models.member import ChatMember from models.member import ChatMember
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
async def _request_endpoint(query_name, body) -> Any: async def _request_endpoint(query_name, body) -> Any:
async with AsyncClient() as client: async with aiohttp.ClientSession() as session:
try: try:
response = await client.post(API_BASE, headers=headers, json=body) async with session.post(API_BASE, headers=headers, json=body) as response:
print(f"[services.core] {query_name}: [{response.status_code}] {len(response.text)} bytes") print(f"[services.core] {query_name}: [{response.status}] {len(await response.text())} bytes")
if response.status_code != 200: if response.status != 200:
return [] return []
r = response.json() r = await response.json()
if r: if r:
return r.get("data", {}).get(query_name, {}) return r.get("data", {}).get(query_name, {})
else: else: