2023-12-22 12:09:03 +03:00
|
|
|
from typing import Any
|
2023-12-18 01:20:13 +03:00
|
|
|
|
2023-11-30 09:42:41 +03:00
|
|
|
import aiohttp
|
2023-12-18 01:20:13 +03:00
|
|
|
|
2023-11-26 13:18:57 +03:00
|
|
|
from settings import API_BASE
|
2023-11-24 01:58:55 +03:00
|
|
|
|
2024-02-17 02:56:15 +03:00
|
|
|
|
|
|
|
headers = {'Content-Type': 'application/json'}
|
2023-11-24 01:58:55 +03:00
|
|
|
|
|
|
|
|
2024-01-15 11:19:37 +03:00
|
|
|
# TODO: rewrite to orm usage?
|
|
|
|
|
2024-02-04 07:58:44 +03:00
|
|
|
|
2023-12-18 10:30:14 +03:00
|
|
|
async def _request_endpoint(query_name, body) -> Any:
|
2023-11-30 09:42:41 +03:00
|
|
|
async with aiohttp.ClientSession() as session:
|
2023-12-18 10:30:14 +03:00
|
|
|
async with session.post(API_BASE, headers=headers, json=body) as response:
|
2024-02-17 02:56:15 +03:00
|
|
|
print(f'[services.core] {query_name} HTTP Response {response.status} {await response.text()}')
|
2023-12-18 10:30:14 +03:00
|
|
|
if response.status == 200:
|
2023-11-30 09:42:41 +03:00
|
|
|
r = await response.json()
|
|
|
|
if r:
|
2024-02-17 02:56:15 +03:00
|
|
|
return r.get('data', {}).get(query_name, {})
|
2023-12-18 10:30:14 +03:00
|
|
|
return []
|
2023-11-24 01:58:55 +03:00
|
|
|
|
|
|
|
|
2023-12-18 01:20:13 +03:00
|
|
|
async def get_followed_shouts(author_id: int):
|
2024-02-17 02:56:15 +03:00
|
|
|
query_name = 'load_shouts_followed'
|
|
|
|
operation = 'GetFollowedShouts'
|
2023-11-24 05:18:02 +03:00
|
|
|
|
2023-12-18 10:30:14 +03: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 11:33:28 +03:00
|
|
|
}}"""
|
2023-11-24 05:18:02 +03:00
|
|
|
|
2023-12-22 12:09:03 +03:00
|
|
|
gql = {
|
2024-02-17 02:56:15 +03:00
|
|
|
'query': query,
|
|
|
|
'operationName': operation,
|
|
|
|
'variables': {'author_id': author_id, 'limit': 1000, 'offset': 0}, # FIXME: too big limit
|
2023-11-28 11:33:28 +03:00
|
|
|
}
|
2023-11-24 05:18:02 +03:00
|
|
|
|
2023-12-22 12:09:03 +03:00
|
|
|
return await _request_endpoint(query_name, gql)
|
|
|
|
|
|
|
|
|
|
|
|
async def get_shout(shout_id):
|
2024-02-17 02:56:15 +03:00
|
|
|
query_name = 'get_shout'
|
|
|
|
operation = 'GetShout'
|
2023-12-22 12:09:03 +03: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-17 02:56:15 +03:00
|
|
|
gql = {'query': query, 'operationName': operation, 'variables': {'slug': None, 'shout_id': shout_id}}
|
2023-12-22 12:09:03 +03:00
|
|
|
|
|
|
|
return await _request_endpoint(query_name, gql)
|