cached-request-9
Some checks failed
deploy / deploy (push) Failing after 1m1s

This commit is contained in:
2023-12-19 20:19:16 +03:00
parent 2380e88168
commit 2253bcf956
6 changed files with 26 additions and 56 deletions

View File

@@ -2,7 +2,7 @@ from functools import wraps
from aiohttp import ClientSession
from starlette.exceptions import HTTPException
from services.core import get_all_authors, cached_authors
from services.core import get_author
from settings import AUTH_URL
@@ -60,8 +60,7 @@ def login_required(f):
user_id = await check_auth(req)
if user_id:
context["user_id"] = user_id
authors_by_user, authors_by_id = get_all_authors() if not cached_authors else cached_authors
author = authors_by_user.get(user_id)
author = get_author(user_id)
if author and "id" in author:
context["author_id"] = author["id"]
return await f(*args, **kwargs)

View File

@@ -29,15 +29,6 @@ def _request_endpoint(query_name, body) -> dict:
def get_all_authors():
global cached_authors, last_update_time
# Check if cached data is available and not expired
if cached_authors is not None and (datetime.now() - last_update_time) < update_interval:
print("[services.core] Returning cached authors data")
return cached_authors
authors_by_user = {}
authors_by_id = {}
query_name = "get_authors_all"
gql = {
@@ -45,18 +36,21 @@ def get_all_authors():
"variables": None,
}
# Make a request to load authors
authors = _request_endpoint(query_name, gql)
return _request_endpoint(query_name, gql)
for a in list(authors):
authors_by_user[a["user"]] = a
authors_by_id[a["id"]] = a
# Cache the authors data and update the last update time
cached_authors = authors_by_user, authors_by_id
last_update_time = datetime.now()
def get_author_by_user(user: str):
operation = "GetAuthorId"
query_name = "get_author_id"
gql = {
"query": f"query {operation}($user: String!) {{ {query_name}(user: $user){{ id }} }}",
"operationName": operation,
"variables": {"user": user},
}
return authors_by_user, authors_by_id
author = _request_endpoint(query_name, gql).popitem()
return author
def get_my_followed() -> List[ChatMember]:
@@ -67,6 +61,5 @@ def get_my_followed() -> List[ChatMember]:
"variables": None,
}
return _request_endpoint(query_name, gql).get(
"authors", []
) # Ensure you're returning the correct part of the response
result = _request_endpoint(query_name, gql)
return result.get("authors", [])