notifier/services/core.py

51 lines
1.6 KiB
Python
Raw Normal View History

2023-12-22 09:09:03 +00:00
from typing import Any
2023-12-17 22:20:13 +00:00
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-11-23 22:58:55 +00:00
2024-01-15 08:19:37 +00:00
# TODO: rewrite to orm usage?
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:
2023-12-22 09:09:03 +00:00
print(f"[services.core] {query_name} HTTP Response {response.status} {await response.text()}")
2023-12-18 07:30:14 +00:00
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-12-22 09:09:03 +00:00
gql = {
2023-11-28 08:33:28 +00:00
"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-12-22 09:09:03 +00:00
return await _request_endpoint(query_name, gql)
async def get_shout(shout_id):
query_name = "get_shout"
operation = "GetShout"
query = f"""query {operation}($slug: String, $shout_id: Int) {{
{query_name}(slug: $slug, shout_id: $shout_id) {{ id slug title authors {{ id slug name pic }} }}
}}"""
gql = {"query": query, "operationName": operation, "variables": {"slug": None, "shout_id": shout_id}}
return await _request_endpoint(query_name, gql)