From fb518b0fb61e5fd30b0f64246722af056c375988 Mon Sep 17 00:00:00 2001 From: Oleg Silkin Date: Mon, 20 May 2019 06:49:08 -0400 Subject: [PATCH] Creates routes & handles --- lbry_comment_server/__init__.py | 1 + lbry_comment_server/handles.py | 81 +++++++++++++++++++++++++++++++++ lbry_comment_server/main.py | 5 -- lbry_comment_server/routes.py | 7 +++ 4 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 lbry_comment_server/handles.py diff --git a/lbry_comment_server/__init__.py b/lbry_comment_server/__init__.py index f4f8efa..888dd4c 100644 --- a/lbry_comment_server/__init__.py +++ b/lbry_comment_server/__init__.py @@ -1,6 +1,7 @@ from lbry_comment_server.settings import config from lbry_comment_server.database import obtain_connection, validate_input, get_claim_comments from lbry_comment_server.database import get_comments_by_id, get_comment_ids, create_comment +from lbry_comment_server.handles import api_endpoint schema = config['path']['schema'] database_fp = config['path']['dev'] backup = config['path']['backup'] diff --git a/lbry_comment_server/handles.py b/lbry_comment_server/handles.py new file mode 100644 index 0000000..f74a0d3 --- /dev/null +++ b/lbry_comment_server/handles.py @@ -0,0 +1,81 @@ +import json +import asyncio +from aiojobs.aiohttp import atomic +from aiohttp import web +from lbry_comment_server import create_comment, get_claim_comments +from lbry_comment_server import get_comments_by_id, get_comment_ids + +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'}, +} + + +def ping(): + return 'pong' + + +@atomic +async def handle_create_comment(**kwargs): + pass + + +def handle_get_comment_ids(**kwargs): + pass + + +def handle_get_claim_comments(**kwargs): + pass + + +def handle_get_comments_by_id(**kwargs): + pass + + +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 +} + + +def process_json(body: dict) -> dict: + response = {'jsonrpc': '2.0', 'id': body['id']} + if body['method'] in METHODS: + method = body['method'] + params = body.get('params', {}) + try: + if method in self.__db_methods: + result = self.db_conn.__getattribute__(method).__call__(**params) + else: + result = self.methods[method](self, **params) + response['result'] = result + except TypeError as te: + print(te) + response['error'] = ERRORS['INVALID_PARAMS'] + else: + response['error'] = ERRORS['UNKNOWN'] + return response + + +async def api_endpoint(request): + try: + body = await request.json() + if type(body) is list or type(body) is dict: + if type(body) is list: + return web.json_response([process_json(part) for part in body]) + else: + return web.json_response(process_json(body)) + else: + return web.json_response({'error': ERRORS['UNKNOWN']}) + except json.decoder.JSONDecodeError as jde: + return web.json_response({ + 'error': {'message': jde.msg, 'code': -1} + }) + + + + diff --git a/lbry_comment_server/main.py b/lbry_comment_server/main.py index 07ced75..4027178 100644 --- a/lbry_comment_server/main.py +++ b/lbry_comment_server/main.py @@ -7,11 +7,6 @@ from settings import config from lbry_comment_server import database_fp from lbry_comment_server.database import obtain_connection -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'}, -} class CommentServer: diff --git a/lbry_comment_server/routes.py b/lbry_comment_server/routes.py index e69de29..744e9d3 100644 --- a/lbry_comment_server/routes.py +++ b/lbry_comment_server/routes.py @@ -0,0 +1,7 @@ +from aiohttp import web + +from lbry_comment_server import api_endpoint + + +def add_routes(app: web.Application): + app.add_routes([web.post('/api', api_endpoint)])