authorid-context
All checks were successful
deploy / deploy (push) Successful in 1m18s

This commit is contained in:
2023-11-28 19:04:45 +03:00
parent 2d89a4ec86
commit 68fce283bc
10 changed files with 47 additions and 300 deletions

View File

@@ -1,5 +1,8 @@
from functools import wraps
from httpx import AsyncClient, HTTPError
from httpx import AsyncClient
from orm.author import Author
from services.db import local_session
from settings import AUTH_URL
@@ -42,23 +45,14 @@ def login_required(f):
raise Exception("You are not logged in")
else:
# Добавляем author_id в контекст
context["user_id"] = user_id
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
if author:
context["author_id"] = author.id
if user_id:
context["user_id"] = user_id
# Если пользователь аутентифицирован, выполняем резолвер
return await f(*args, **kwargs)
return decorated_function
def auth_request(f):
@wraps(f)
async def decorated_function(*args, **kwargs):
req = args[0]
is_authenticated, user_id = await check_auth(req)
if not is_authenticated:
raise HTTPError("please, login first")
else:
req["user_id"] = user_id
return await f(*args, **kwargs)
return decorated_function

View File

@@ -1,9 +1,5 @@
from typing import List
from typing import List, Any
from httpx import AsyncClient
from orm.author import Author
from orm.shout import Shout
from settings import API_BASE
headers = {"Content-Type": "application/json"}
@@ -27,22 +23,7 @@ async def _request_endpoint(query_name, body):
traceback.print_exc()
async def get_author(author_id) -> Author:
query_name = "get_author"
query_type = "query"
operation = "GetAuthor"
query_fields = "id slug pic name"
gql = {
"query": query_type + " " + operation + " { " + query_name + " { " + query_fields + "} " + " }",
"operationName": operation,
"variables": None,
}
return await _request_endpoint(query_name, gql)
async def get_followed_shouts(author_id: int) -> List[Shout]:
async def get_followed_shouts(author_id: int) -> List[Any]:
query_name = "load_shouts_followed"
query_type = "query"
operation = "GetFollowedShouts"
@@ -55,7 +36,7 @@ async def get_followed_shouts(author_id: int) -> List[Shout]:
body = {
"query": query,
"operationName": operation,
"variables": {"author_id": author_id, "limit": 1000, "offset": 0}, # FIXME: too big
"variables": {"author_id": author_id, "limit": 1000, "offset": 0}, # FIXME: too big limit
}
return await _request_endpoint(query_name, body)