pushes changes in case of power outage

This commit is contained in:
Oleg Silkin 2019-05-20 02:11:02 -04:00
parent e6da0a21e0
commit fb32bbefb1
5 changed files with 36 additions and 25 deletions

2
src/__init__.py Normal file
View file

@ -0,0 +1,2 @@
from src.database import DatabaseConnection
from src.conf import database_dir, anonymous

View file

@ -3,7 +3,7 @@ import typing
import re import re
import nacl.hash import nacl.hash
import time import time
from server.conf import * from src.conf import *
def validate_input(**kwargs): def validate_input(**kwargs):

View file

@ -1,8 +1,10 @@
import asyncio import asyncio
import json
from aiohttp import web from aiohttp import web
from server.database import DatabaseConnection from src import database_dir
from server.conf import database_dir from src.database import DatabaseConnection
ERRORS = { ERRORS = {
'INVALID_PARAMS': {'code': -32602, 'message': 'Invalid parameters'}, 'INVALID_PARAMS': {'code': -32602, 'message': 'Invalid parameters'},
@ -20,7 +22,7 @@ class CommentServer:
self.server = None self.server = None
self.db_conn = DatabaseConnection(database_dir) self.db_conn = DatabaseConnection(database_dir)
def ping(cls): def ping(self):
return 'pong' return 'pong'
methods = { methods = {
@ -31,10 +33,6 @@ class CommentServer:
'create_comment': None 'create_comment': None
} }
__methods = {
'ping'
}
__db_methods = { __db_methods = {
'get_claim_comments', 'get_claim_comments',
'get_comment_ids', 'get_comment_ids',
@ -47,11 +45,15 @@ class CommentServer:
if body['method'] in self.methods: if body['method'] in self.methods:
method = body['method'] method = body['method']
params = body.get('params', {}) params = body.get('params', {})
if method in self.__db_methods: try:
result = self.db_conn.__getattribute__(method).__call__(**params) if method in self.__db_methods:
else: result = self.db_conn.__getattribute__(method).__call__(**params)
result = self.__methods[method](self, **params) else:
response['result'] = result result = self.methods[method](self, **params)
response['result'] = result
except TypeError as te:
print(te)
response['error'] = ERRORS['INVALID_PARAMS']
else: else:
response['error'] = ERRORS['UNKNOWN'] response['error'] = ERRORS['UNKNOWN']
return response return response
@ -77,15 +79,22 @@ class CommentServer:
await self._stop() await self._stop()
async def api(self, request): async def api(self, request):
body = await request.json() try:
if type(body) is list or type(body) is dict: body = await request.json()
if type(body) is list: # batch request if type(body) is list or type(body) is dict:
response = [self.process_json(part) for part in body] if type(body) is list: # batch request
else: # single rpc request response = [self.process_json(part) for part in body]
response = self.process_json(body) else: # single rpc request
return web.json_response(response) response = self.process_json(body)
else: return web.json_response(response)
return web.json_response({'error': ERRORS['UNKNOWN']}) else:
return web.json_response({'error': ERRORS['UNKNOWN']})
except
except json.decoder.JSONDecodeError as jde:
return web.json_response({
'error': {'message': jde.msg, 'code': -1}
})
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -4,8 +4,8 @@ from faker.providers import internet
from faker.providers import lorem from faker.providers import lorem
from faker.providers import misc from faker.providers import misc
import server.conf import src.conf
import server.database as db import src.database as db
import sqlite3 import sqlite3
import faker import faker
from random import randint from random import randint
@ -21,7 +21,7 @@ class DatabaseTestCase(unittest.TestCase):
super().setUp() super().setUp()
self.db = db.DatabaseConnection('test.db') self.db = db.DatabaseConnection('test.db')
self.db.obtain_connection() self.db.obtain_connection()
self.db.generate_schema(server.conf.schema_dir) self.db.generate_schema(src.conf.schema_dir)
def tearDown(self) -> None: def tearDown(self) -> None:
curs = self.db.connection.execute('SELECT * FROM COMMENT') curs = self.db.connection.execute('SELECT * FROM COMMENT')