forked from coinspark/python-OP_RETURN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_server.py
More file actions
executable file
·57 lines (44 loc) · 1.58 KB
/
api_server.py
File metadata and controls
executable file
·57 lines (44 loc) · 1.58 KB
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
#!/usr/bin/env python3
from flask import Flask, request, abort
from flask_restful import Resource, Api
from flask_restful.reqparse import RequestParser
from OP_RETURN import *
import re
import json
# user configuration
POST_AUTHORIZED_IPS = ['127.0.0.1']
from user_defined import *
# end of configuration area
app = Flask(__name__)
api = Api(app, prefix="/api/v1")
request_parser = RequestParser(bundle_errors=True)
class Blockchain(Resource):
def get(self, ref):
regexp_pattern = '^\d{1,8}-\d{6}$'
sanity_check = re.match(regexp_pattern, ref)
if sanity_check:
result = OP_RETURN_retrieve(ref)
# first we need to process the stored data for consumption
for idx, val in enumerate(result):
# convert data from string to data structure
decoded_data = result[idx]['data'].decode('utf-8')
result[idx]['data'] = json.loads(decoded_data)
return(result)
else:
return({"error": "Please provide a valid reference."})
class BlockchainStore(Resource):
def post(self):
authorized = False
for ip in POST_AUTHORIZED_IPS:
if ip == request.remote_addr:
authorized = True
if authorized == False:
abort(403)
json_data = request.get_json()
result = OP_RETURN_store(
json.dumps(json_data, separators=(',', ':')))
return(result)
api.add_resource(Blockchain, '/blockchain/<ref>')
api.add_resource(BlockchainStore, '/blockchain')
if __name__ == '__main__':
app.run(debug=True)