2019-05-20 12:49:08 +02:00
|
|
|
import json
|
2019-05-21 13:54:52 +02:00
|
|
|
import logging
|
|
|
|
|
2019-05-20 12:49:08 +02:00
|
|
|
import asyncio
|
|
|
|
from aiohttp import web
|
2019-05-21 13:54:52 +02:00
|
|
|
from aiojobs.aiohttp import atomic
|
|
|
|
|
|
|
|
import lbry_comment_server.writes as writes
|
2019-05-21 11:00:01 +02:00
|
|
|
from lbry_comment_server import get_claim_comments
|
|
|
|
from lbry_comment_server import get_comments_by_id, get_comment_ids
|
2019-05-21 13:54:52 +02:00
|
|
|
from lbry_comment_server.database import obtain_connection
|
2019-05-21 12:56:27 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-05-20 12:49:08 +02:00
|
|
|
ERRORS = {
|
|
|
|
'INVALID_PARAMS': {'code': -32602, 'message': 'Invalid parameters'},
|
|
|
|
'INTERNAL': {'code': -32603, 'message': 'An internal error'},
|
|
|
|
'UNKNOWN': {'code': -1, 'message': 'An unknown or very miscellaneous error'},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-21 11:00:01 +02:00
|
|
|
def ping(*args):
|
2019-05-20 12:49:08 +02:00
|
|
|
return 'pong'
|
|
|
|
|
|
|
|
|
2019-05-21 11:00:01 +02:00
|
|
|
def handle_get_comment_ids(app, **kwargs):
|
|
|
|
with obtain_connection(app['db_path']) as conn:
|
|
|
|
return get_comment_ids(conn, **kwargs)
|
2019-05-20 12:49:08 +02:00
|
|
|
|
|
|
|
|
2019-05-21 11:00:01 +02:00
|
|
|
def handle_get_claim_comments(app, **kwargs):
|
|
|
|
with obtain_connection(app['db_path']) as conn:
|
|
|
|
return get_claim_comments(conn, **kwargs)
|
2019-05-20 12:49:08 +02:00
|
|
|
|
|
|
|
|
2019-05-21 11:00:01 +02:00
|
|
|
def handle_get_comments_by_id(app, **kwargs):
|
|
|
|
with obtain_connection(app['db_path']) as conn:
|
|
|
|
return get_comments_by_id(conn, **kwargs)
|
2019-05-20 12:49:08 +02:00
|
|
|
|
|
|
|
|
2019-05-21 11:00:01 +02:00
|
|
|
async def handle_create_comment(scheduler, **kwargs):
|
|
|
|
job = await scheduler.spawn(writes.write_comment(**kwargs))
|
|
|
|
return await job.wait()
|
2019-05-20 12:49:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
METHODS = {
|
|
|
|
'ping': ping,
|
|
|
|
'get_claim_comments': handle_get_claim_comments,
|
|
|
|
'get_comment_ids': handle_get_comment_ids,
|
|
|
|
'get_comments_by_id': handle_get_comments_by_id,
|
|
|
|
'create_comment': handle_create_comment
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-21 11:00:01 +02:00
|
|
|
async def process_json(app, body: dict) -> dict:
|
2019-05-20 12:49:08 +02:00
|
|
|
response = {'jsonrpc': '2.0', 'id': body['id']}
|
|
|
|
if body['method'] in METHODS:
|
|
|
|
method = body['method']
|
|
|
|
params = body.get('params', {})
|
|
|
|
try:
|
2019-05-21 11:00:01 +02:00
|
|
|
if asyncio.iscoroutinefunction(METHODS[method]):
|
|
|
|
result = await METHODS[method](app['comment_scheduler'], **params)
|
2019-05-20 12:49:08 +02:00
|
|
|
else:
|
2019-05-21 11:00:01 +02:00
|
|
|
result = METHODS[method](app, **params)
|
2019-05-20 12:49:08 +02:00
|
|
|
response['result'] = result
|
|
|
|
except TypeError as te:
|
2019-05-21 12:56:27 +02:00
|
|
|
logger.exception('Got TypeError: %s', te)
|
2019-05-20 12:49:08 +02:00
|
|
|
response['error'] = ERRORS['INVALID_PARAMS']
|
|
|
|
else:
|
|
|
|
response['error'] = ERRORS['UNKNOWN']
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2019-05-21 11:00:01 +02:00
|
|
|
@atomic
|
|
|
|
async def api_endpoint(request: web.Request):
|
2019-05-20 12:49:08 +02:00
|
|
|
try:
|
|
|
|
body = await request.json()
|
2019-05-21 12:56:27 +02:00
|
|
|
logger.info('Received POST request from %s', request.remote)
|
2019-05-20 12:49:08 +02:00
|
|
|
if type(body) is list or type(body) is dict:
|
|
|
|
if type(body) is list:
|
2019-05-21 11:00:01 +02:00
|
|
|
return web.json_response(
|
|
|
|
[await process_json(request.app, part) for part in body]
|
|
|
|
)
|
2019-05-20 12:49:08 +02:00
|
|
|
else:
|
2019-05-21 11:00:01 +02:00
|
|
|
return web.json_response(await process_json(request.app, body))
|
2019-05-20 12:49:08 +02:00
|
|
|
else:
|
|
|
|
return web.json_response({'error': ERRORS['UNKNOWN']})
|
|
|
|
except json.decoder.JSONDecodeError as jde:
|
2019-05-21 12:56:27 +02:00
|
|
|
logger.exception('Received malformed JSON from %s: %s', request.remote, jde.msg)
|
|
|
|
logger.debug('Request headers: %s', request.headers)
|
2019-05-20 12:49:08 +02:00
|
|
|
return web.json_response({
|
|
|
|
'error': {'message': jde.msg, 'code': -1}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|