Files
inbox/services/core.py

79 lines
2.4 KiB
Python
Raw Normal View History

2023-12-17 20:13:17 +03:00
from typing import Any, List
2023-11-30 09:49:23 +03:00
import aiohttp
2023-12-18 00:10:29 +03:00
from aiohttp import ClientResponse, ClientSession
2023-11-22 15:09:24 +03:00
from models.member import ChatMember
2023-12-17 20:13:17 +03:00
from settings import API_BASE
2023-10-04 23:42:39 +03:00
2023-10-11 22:12:55 +03:00
headers = {"Content-Type": "application/json"}
2023-11-14 21:25:55 +03:00
2023-10-04 23:42:39 +03:00
2023-11-28 12:05:39 +03:00
async def _request_endpoint(query_name, body) -> Any:
2023-11-30 09:49:23 +03:00
async with aiohttp.ClientSession() as session:
2023-12-18 02:34:10 +03:00
async with session.post(API_BASE, headers=headers, json=body) as response:
print(f"[services.core] {query_name} response: <{response.status}> {await response.text()}")
if response.status == 200:
2023-11-30 09:49:23 +03:00
r = await response.json()
if r:
return r.get("data", {}).get(query_name, {})
2023-12-18 02:34:10 +03:00
return []
2023-11-28 12:05:39 +03:00
2023-11-22 15:09:24 +03:00
async def get_all_authors() -> List[ChatMember]:
2023-11-14 21:20:45 +03:00
query_name = "authorsAll"
2023-11-06 19:25:28 +03:00
query_type = "query"
2023-11-14 21:20:45 +03:00
operation = "AuthorsAll"
2023-11-24 05:20:16 +03:00
query_fields = "id slug pic name"
2023-11-06 19:25:28 +03:00
2023-10-04 23:42:39 +03:00
gql = {
2023-11-06 19:25:28 +03:00
"query": query_type + " " + operation + " { " + query_name + " { " + query_fields + " } " + " }",
"operationName": operation,
2023-11-14 21:20:45 +03:00
"variables": None,
2023-10-04 23:42:39 +03:00
}
2023-11-22 15:09:24 +03:00
2023-12-18 00:54:44 +03:00
return await _request_endpoint(query_name, gql)
2023-10-04 23:42:39 +03:00
2023-10-11 22:12:55 +03:00
2023-11-28 11:33:50 +03:00
async def get_my_followed() -> List[ChatMember]:
query_name = "get_my_followed"
2023-11-14 21:20:45 +03:00
query_type = "query"
2023-11-28 11:33:50 +03:00
operation = "GetMyFollowed"
2023-11-24 05:20:16 +03:00
query_fields = "id slug pic name"
2023-10-11 22:12:55 +03:00
gql = {
2023-11-14 21:20:45 +03:00
"query": query_type + " " + operation + " { " + query_name + " { authors {" + query_fields + "} } " + " }",
"operationName": operation,
"variables": None,
2023-10-11 22:12:55 +03:00
}
2023-11-22 15:09:24 +03:00
2023-12-18 00:10:29 +03:00
async with ClientSession() as client:
2023-11-14 21:20:45 +03:00
try:
2023-12-18 00:10:29 +03:00
response: ClientResponse = await client.post(API_BASE, headers=headers, json=gql)
print(f"[services.core] {query_name}: [{response.status}] {len(response.text)} bytes")
2023-11-22 15:09:24 +03:00
if response.status_code != 200:
return []
r = response.json()
if r:
return r.get("data", {}).get(query_name, {}).get("authors", [])
2023-11-14 21:20:45 +03:00
except Exception:
import traceback
2023-11-14 21:25:55 +03:00
2023-11-14 21:20:45 +03:00
traceback.print_exc()
2023-11-22 15:09:24 +03:00
return []
2023-11-28 12:05:39 +03:00
2023-12-18 02:53:25 +03:00
async def get_author(user: str = ""):
2023-12-18 03:04:25 +03:00
query_name = "get_author_id"
operation = "GetAuthorId"
2023-11-28 12:05:39 +03:00
gql = {
2023-12-18 03:09:07 +03:00
"query": f"query {operation}($user: String!) {{ {query_name}(user: $user) {{ id slug pic name }} }}",
2023-11-28 12:05:39 +03:00
"operationName": operation,
2023-12-18 02:53:25 +03:00
"variables": {"user": user},
2023-11-28 12:05:39 +03:00
}
2023-12-18 02:34:10 +03:00
r = await _request_endpoint(query_name, gql)
2023-12-18 02:55:49 +03:00
if r and isinstance(r, list):
2023-12-18 02:45:31 +03:00
r = r.pop()
2023-12-18 02:57:14 +03:00
return r or None