notifier/services/core.py

53 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
2024-02-16 23:56:15 +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?
2024-02-04 04:58:44 +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:
2024-02-16 23:56:15 +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:
2024-02-16 23:56:15 +00:00
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):
2024-02-16 23:56:15 +00:00
query_name = 'load_shouts_followed'
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 = {
2024-02-16 23:56:15 +00:00
'query': query,
'operationName': operation,
'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):
2024-02-16 23:56:15 +00:00
query_name = 'get_shout'
operation = 'GetShout'
2023-12-22 09:09:03 +00:00
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 }} }}
}}"""
2024-02-16 23:56:15 +00:00
gql = {'query': query, 'operationName': operation, 'variables': {'slug': None, 'shout_id': shout_id}}
2023-12-22 09:09:03 +00:00
return await _request_endpoint(query_name, gql)