notifier/services/core.py

37 lines
1.2 KiB
Python
Raw Normal View History

2023-12-17 22:20:13 +00:00
from typing import Any, List
2023-11-30 06:42:41 +00:00
import aiohttp
2023-12-17 22:20:13 +00:00
2023-11-26 10:18:57 +00:00
from settings import API_BASE
2023-11-23 22:58:55 +00:00
2023-11-26 10:18:57 +00:00
headers = {"Content-Type": "application/json"}
2023-12-18 07:30:14 +00:00
api_base = API_BASE or "https://core.discours.io"
2023-11-23 22:58:55 +00:00
2023-12-18 07:30:14 +00:00
async def _request_endpoint(query_name, body) -> Any:
2023-11-30 06:42:41 +00:00
async with aiohttp.ClientSession() as session:
2023-12-18 07:30:14 +00: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 06:42:41 +00:00
r = await response.json()
if r:
return r.get("data", {}).get(query_name, {})
2023-12-18 07:30:14 +00:00
return []
2023-11-23 22:58:55 +00:00
2023-12-17 22:20:13 +00:00
async def get_followed_shouts(author_id: int):
2023-11-28 08:33:28 +00:00
query_name = "load_shouts_followed"
2023-11-26 10:18:57 +00:00
operation = "GetFollowedShouts"
2023-11-24 02:18:02 +00:00
2023-12-18 07:30:14 +00:00
query = f"""query {operation}($author_id: Int!, limit: Int, offset: Int) {{
{query_name}(author_id: $author_id, limit: $limit, offset: $offset) {{ id slug title }}
2023-11-28 08:33:28 +00:00
}}"""
2023-11-24 02:18:02 +00:00
2023-11-28 08:33:28 +00:00
body = {
"query": query,
"operationName": operation,
2023-11-28 16:04:45 +00:00
"variables": {"author_id": author_id, "limit": 1000, "offset": 0}, # FIXME: too big limit
2023-11-28 08:33:28 +00:00
}
2023-11-24 02:18:02 +00:00
2023-11-26 10:18:57 +00:00
return await _request_endpoint(query_name, body)