inbox/services/core.py

92 lines
2.8 KiB
Python
Raw Normal View History

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