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"
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"

View File

@ -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)

View File

@ -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