moves database utilities from server.py to database.py
This commit is contained in:
parent
c9acbedb13
commit
cdfd62a1c0
2 changed files with 60 additions and 40 deletions
43
database.py
Normal file
43
database.py
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
import sqlite3
|
||||||
|
import typing
|
||||||
|
|
||||||
|
connection = sqlite3.connect('example.db')
|
||||||
|
|
||||||
|
connection.execute("""
|
||||||
|
CREATE TABLE IF NOT EXISTS TEST(
|
||||||
|
NAME TEXT NOT NULL,
|
||||||
|
CONSTRAINT TEST_PK PRIMARY KEY(NAME)
|
||||||
|
ON CONFLICT IGNORE
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
|
||||||
|
|
||||||
|
def get_claim_comments(claim_id: str, parent_id: int = None, page: int = 1, page_size: int = 50):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def create_comment(claim_id: str, message: str, channel_name: str = None,
|
||||||
|
channel_claim_id: str = None, reply_to: int = None) -> typing.Union[int, dict, None]:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def get_comment_ids(claim_id: str, parent_id: int = None, get_all: bool = False):
|
||||||
|
""" Just return a list of the comment IDs that are associated with the given claim_id.
|
||||||
|
If get_all is specified then it returns all the IDs, otherwise only the IDs at that level.
|
||||||
|
if parent_id is left null then it only returns the top level comments.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def get_comment_data(comment_ids: list) -> typing.Union[dict, None]:
|
||||||
|
""" Returns a list containing the comment data associated with each ID within the list"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
connection.execute("INSERT INTO TEST VALUES (?), (?), (?)", ['Don Hockett', 'james cayo', 'MERIANNA'])
|
||||||
|
connection.commit()
|
||||||
|
curs = connection.execute('SELECT * FROM TEST')
|
||||||
|
print(curs.fetchall())
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
|
57
server.py
57
server.py
|
@ -1,10 +1,18 @@
|
||||||
import typing
|
import typing
|
||||||
import random
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
|
|
||||||
|
import database as db
|
||||||
|
|
||||||
class FakedCommentServer:
|
|
||||||
|
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:
|
||||||
def __init__(self, port=2903):
|
def __init__(self, port=2903):
|
||||||
self.port = port
|
self.port = port
|
||||||
self.app = web.Application(debug=True)
|
self.app = web.Application(debug=True)
|
||||||
|
@ -12,42 +20,11 @@ class FakedCommentServer:
|
||||||
self.runner = None
|
self.runner = None
|
||||||
self.server = None
|
self.server = None
|
||||||
|
|
||||||
def get_claim_comments(self, uri: str, better_keys: bool) -> typing.Union[dict, list, None]:
|
|
||||||
return [self.get_comment(i) for i in range(75)]
|
|
||||||
|
|
||||||
def get_comment(self, comment_id: int, parent_id: int = None) -> dict:
|
|
||||||
return {
|
|
||||||
'comment_id': comment_id,
|
|
||||||
'parent_id': parent_id,
|
|
||||||
'author': f'Person{comment_id}',
|
|
||||||
'message': f'comment {comment_id}',
|
|
||||||
'claim_id': random.randint(1, 2**16),
|
|
||||||
'time_posted': random.randint(2**16, 2**32 - 1),
|
|
||||||
'upvotes': random.randint(0, 9999), 'downvotes': random.randint(0, 9999)
|
|
||||||
}
|
|
||||||
|
|
||||||
def comment(self, uri: str, poster: str, message: str) -> typing.Union[int, dict, None]:
|
|
||||||
if not uri.startswith('lbry://'):
|
|
||||||
return {'error': self.ERRORS['INVALID_URI']}
|
|
||||||
return random.randint(1, 9999)
|
|
||||||
|
|
||||||
def reply(self, parent_id: int, poster: str, message: str) -> dict:
|
|
||||||
if 2 <= len(message) <= 2000 and 2 <= len(poster) <= 127 and parent_id > 0:
|
|
||||||
return random.randint(parent_id + 1, 2**32 - 1)
|
|
||||||
return {'error': self.ERRORS['INVALID_PARAMS']}
|
|
||||||
|
|
||||||
def get_comment_data(self, comm_index: int, better_keys: bool = False) -> typing.Union[dict, None]:
|
|
||||||
return self.get_comment(comm_index)
|
|
||||||
|
|
||||||
def get_comment_replies(self, comm_index: int) -> typing.Union[list, None]:
|
|
||||||
return [random.randint(comm_index, comm_index+250) for _ in range(75)]
|
|
||||||
|
|
||||||
methods = {
|
methods = {
|
||||||
'get_claim_comments': get_claim_comments,
|
'get_claim_comments': db.get_claim_comments,
|
||||||
'get_comment_data': get_comment_data,
|
'get_comment_ids': db.get_comment_ids,
|
||||||
'get_comment_replies': get_comment_replies,
|
'get_comment_data': db.get_comment_data,
|
||||||
'comment': comment,
|
'create_comment': db.create_comment,
|
||||||
'reply': reply
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def process_json(self, body) -> dict:
|
def process_json(self, body) -> dict:
|
||||||
|
@ -60,7 +37,7 @@ class FakedCommentServer:
|
||||||
else:
|
else:
|
||||||
response['result'] = result
|
response['result'] = result
|
||||||
else:
|
else:
|
||||||
response['error'] = self.ERRORS['UNKNOWN']
|
response['error'] = ERRORS['UNKNOWN']
|
||||||
return response
|
return response
|
||||||
|
|
||||||
async def _start(self):
|
async def _start(self):
|
||||||
|
@ -90,9 +67,9 @@ class FakedCommentServer:
|
||||||
response = self.process_json(body)
|
response = self.process_json(body)
|
||||||
return web.json_response(response)
|
return web.json_response(response)
|
||||||
else:
|
else:
|
||||||
return web.json_response({'error': self.ERRORS['UNKNOWN']})
|
return web.json_response({'error': ERRORS['UNKNOWN']})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = FakedCommentServer()
|
app = CommentServer()
|
||||||
asyncio.run(app.run())
|
asyncio.run(app.run())
|
||||||
|
|
Loading…
Reference in a new issue