inbox/services/core.py
Untone 6c7f269206
All checks were successful
deploy / deploy (push) Successful in 1m3s
cached-request
2023-12-19 18:58:26 +03:00

60 lines
1.7 KiB
Python

from functools import lru_cache
from typing import Any, List
import requests
from models.member import ChatMember
from settings import API_BASE
@lru_cache(maxsize=128, typed=True)
def _request_endpoint(query_name, body) -> Any:
print(f"[services.core] requesting {query_name}...")
response = requests.post(API_BASE, headers={"Content-Type": "application/json"}, json=body)
print(f"[services.core] {query_name} response: <{response.status_code}> {response.text[:65]}..")
if response.status_code == 200:
try:
r = response.json()
result = r.get("data", {}).get(query_name, {})
print(f"[services.core] entries amount in result: {len(result)} ")
return result
except ValueError as e:
print(f"[services.core] Error decoding JSON response: {e}")
return []
def get_all_authors():
authors_by_user = {}
authors_by_id = {}
query_name = "get_authors_all"
# Check if authors are already cached
if "authors" in authors_by_user:
return authors_by_user["authors"]
gql = {
"query": "query { " + query_name + "{ id slug pic name user } }",
"variables": None,
}
# Make a request to load authors
authors = _request_endpoint(query_name, gql)
for a in list(authors):
authors_by_user[a["user"]] = a
authors_by_id[a["id"]] = a
return authors_by_user, authors_by_id
def get_my_followed() -> List[ChatMember]:
query_name = "get_my_followed"
gql = {
"query": "query { " + query_name + " { authors { id slug pic name } } }",
"variables": None,
}
return _request_endpoint(query_name, gql) or []