-
Notifications
You must be signed in to change notification settings - Fork 28
/
poe_server.py
93 lines (71 loc) · 3.35 KB
/
poe_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import binascii
import json as JSON
from base64 import b64encode, b64decode
from datetime import datetime
from sanic import Sanic
from sanic.response import json
from sanic_cors import CORS, cross_origin
from poe_libs import Document
port = 8000
class Server(object):
def __init__(self):
self.app = Sanic()
CORS(self.app)
self.document = Document()
self.app.add_route(self.verify, '/verify', methods=['GET'])
self.app.add_route(self.publish, '/publish', methods=['POST'])
self.app.add_route(self.details, '/details', methods=['GET'])
async def verify(self, request):
"""returns details about verified document"""
digest = request.args.get('digest')
verified_docs = self.document.verify(digest)
response_data = []
for doc in verified_docs:
meta_data = JSON.loads(b64decode(binascii.a2b_hex(doc.get('data'))).decode())
doc = {"digest": digest,
"transaction_id": doc.get('txid'),
"confirmations": doc.get('confirmations'),
"blocktime": doc.get('blocktime'),
"name": meta_data.get('name'),
"email": meta_data.get('email'),
"message": meta_data.get('message'),
"recorded_timestamp_UTC": doc.get('blocktime'),
"readable_time_UTC": datetime.fromtimestamp(int(doc.get('blocktime'))).
strftime("%c")}
response_data.append(doc)
return json(response_data)
async def publish(self, request):
"""publishes document detail and returns its block info"""
try:
json_data = {'name': request.form.get('name'),
'email': request.form.get('email'),
'message': request.form.get('message'),
'digest': request.form.get('digest')}
json_string = JSON.dumps(json_data)
encoded = b64encode(json_string.encode('utf-8'))
hex_encoded = binascii.b2a_hex(encoded).decode()
tx_id = self.document.publish(json_data['digest'], hex_encoded)
tx_info = self.document.fetch_by_txid(tx_id)
response_data = {'long_url': None,
'short_url': None,
'digest': json_data['digest'],
'transaction_id': tx_id,
'confirmations': tx_info.get('confirmations'),
'blockhash': tx_info.get('blockhash'),
'blocktime': tx_info.get('blocktime'),
'name': json_data['name'],
'email': json_data['email'],
'message': json_data['message'],
'timestamp': datetime.now().timestamp(),
'status': True}
except Exception as e:
response_data = {'status': False}
return json(response_data)
async def details(self, request):
"""returns details of latest inserted documents"""
latest_docs = self.document.fetch_latest(int(request.args.get('count')))
return json(latest_docs)
if __name__ == '__main__':
"""main function to serve the api"""
server = Server()
server.app.run(host='0.0.0.0', port=port, debug=True)