Creates routes & handles
This commit is contained in:
parent
861ea1ea8c
commit
fb518b0fb6
4 changed files with 89 additions and 5 deletions
|
@ -1,6 +1,7 @@
|
||||||
from lbry_comment_server.settings import config
|
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 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.database import get_comments_by_id, get_comment_ids, create_comment
|
||||||
|
from lbry_comment_server.handles import api_endpoint
|
||||||
schema = config['path']['schema']
|
schema = config['path']['schema']
|
||||||
database_fp = config['path']['dev']
|
database_fp = config['path']['dev']
|
||||||
backup = config['path']['backup']
|
backup = config['path']['backup']
|
||||||
|
|
81
lbry_comment_server/handles.py
Normal file
81
lbry_comment_server/handles.py
Normal file
|
@ -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}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,11 +7,6 @@ from settings import config
|
||||||
from lbry_comment_server import database_fp
|
from lbry_comment_server import database_fp
|
||||||
from lbry_comment_server.database import obtain_connection
|
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:
|
class CommentServer:
|
||||||
|
|
|
@ -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)])
|
Loading…
Reference in a new issue