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 nacl.hash
import time
from server.conf import *
from src.conf import *
def validate_input(**kwargs):

View file

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

View file

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