This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from typing import Any, List
|
||||
|
||||
from aiohttp import ClientSession
|
||||
import requests
|
||||
from models.member import ChatMember
|
||||
from settings import API_BASE
|
||||
|
||||
@@ -9,19 +9,19 @@ authors_by_user = {}
|
||||
authors_by_id = {}
|
||||
|
||||
|
||||
async def _request_endpoint(query_name, body) -> Any:
|
||||
async with ClientSession() as session:
|
||||
async with session.post(API_BASE, headers=headers, json=body) as response:
|
||||
print(f"[services.core] {query_name} response: <{response.status}> {(await response.text())[:65]}..")
|
||||
if response.status == 200:
|
||||
r = await response.json()
|
||||
if r:
|
||||
return r.get("data", {}).get(query_name, {})
|
||||
return []
|
||||
def _request_endpoint(query_name, body) -> Any:
|
||||
response = requests.post(API_BASE, headers=headers, json=body)
|
||||
print(f"[services.core] {query_name} response: <{response.status_code}> {response.text[:65]}..")
|
||||
if response.status_code == 200:
|
||||
r = response.json()
|
||||
if r:
|
||||
return r.get("data", {}).get(query_name, {})
|
||||
return []
|
||||
|
||||
|
||||
async def get_all_authors() -> List[ChatMember]:
|
||||
def get_all_authors() -> List[ChatMember]:
|
||||
if len(authors_by_user.keys()) == 0:
|
||||
print("[services.core] precaching authors...")
|
||||
query_name = "get_authors_all"
|
||||
|
||||
# Check if authors are already cached
|
||||
@@ -34,20 +34,17 @@ async def get_all_authors() -> List[ChatMember]:
|
||||
}
|
||||
|
||||
# Make a request to load authors
|
||||
authors = await _request_endpoint(query_name, gql)
|
||||
authors = _request_endpoint(query_name, gql)
|
||||
|
||||
for a in authors:
|
||||
if a["user"] in authors_by_user:
|
||||
print("DOUBLE DOUBLE")
|
||||
print(a)
|
||||
authors_by_user[a["user"]] = a
|
||||
authors_by_id[a["id"]] = a
|
||||
print(f"[main] {len(authors)} authors precached")
|
||||
for a in list(authors):
|
||||
authors_by_user.__setitem__(a["user"], a)
|
||||
authors_by_id.__setitem__(a["id"], a)
|
||||
print(f"[services.core] {len(authors)} authors precached")
|
||||
|
||||
return list(authors_by_id.values())
|
||||
|
||||
|
||||
async def get_my_followed() -> List[ChatMember]:
|
||||
def get_my_followed() -> List[ChatMember]:
|
||||
query_name = "get_my_followed"
|
||||
|
||||
gql = {
|
||||
@@ -55,4 +52,4 @@ async def get_my_followed() -> List[ChatMember]:
|
||||
"variables": None,
|
||||
}
|
||||
|
||||
return await _request_endpoint(query_name, gql) or []
|
||||
return _request_endpoint(query_name, gql) or []
|
||||
|
Reference in New Issue
Block a user