-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdid_factom_driver.py
114 lines (90 loc) · 4.59 KB
/
did_factom_driver.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import bottle
import json
from bottle import error, get, hook, request, run
from harmony_connect_client.rest import ApiException
from src import consts
from src import factomd_jsonrpc_connection
from src import harmony_connect_connection
from src import tfa_explorer_connection
from src.config import DriverConfig
from src.models import IdentityNotFoundException
driver_config = DriverConfig()
@hook('before_request')
def strip_path():
"""Strip trailing '/' on all requests. '/foo' and /foo/' are two unique endpoints in bottle"""
request.environ['PATH_INFO'] = request.environ['PATH_INFO'].rstrip('/')
@get('/health')
def health_check():
return {'data': 'Healthy!'}
@get('/1.0/identifiers/did\:factom\:<chain_id:re:[0-9A-Fa-f]{64}>')
def resolve(chain_id):
did = '{}{}'.format(consts.DID_PREFIX, chain_id)
identity = None
try:
if driver_config.factom_connection == DriverConfig.FACTOMD:
identity = factomd_jsonrpc_connection.get_identity(driver_config, did, chain_id)
elif driver_config.factom_connection == DriverConfig.TFA_EXPLORER:
identity = tfa_explorer_connection.get_identity(driver_config, did, chain_id)
elif driver_config.factom_connection == DriverConfig.HARMONY:
identity = harmony_connect_connection.get_identity(driver_config, did, chain_id)
else:
bottle.abort(500) # Invalid connection type. This should never be executed.
except IdentityNotFoundException:
bottle.abort(404)
except ApiException:
bottle.abort(500) # Failed to make API call for some reason
return {'didDocument': identity.get_did_document(), 'methodMetadata': identity.get_method_metadata()}
@get('/1.0/identifiers/did\:factom\:mainnet\:<chain_id:re:[0-9A-Fa-f]{64}>')
def resolve_mainnet(chain_id):
did = '{}{}{}'.format(consts.DID_PREFIX, consts.NETWORK_MAINNET, chain_id)
identity = None
try:
if driver_config.factom_connection == DriverConfig.FACTOMD:
identity = factomd_jsonrpc_connection.get_identity(driver_config, did, chain_id)
elif driver_config.factom_connection == DriverConfig.TFA_EXPLORER:
identity = tfa_explorer_connection.get_identity(driver_config, did, chain_id)
elif driver_config.factom_connection == DriverConfig.HARMONY:
identity = harmony_connect_connection.get_identity(driver_config, did, chain_id)
else:
bottle.abort(500) # Invalid connection type. This should never be executed.
except IdentityNotFoundException:
bottle.abort(404)
except ApiException:
bottle.abort(500) # Failed to make API call for some reason
return {'didDocument': identity.get_did_document(), 'methodMetadata': identity.get_method_metadata()}
@get('/1.0/identifiers/did\:factom\:testnet\:<chain_id:re:[0-9A-Fa-f]{64}>')
def resolve_testnet(chain_id):
did = '{}{}{}'.format(consts.DID_PREFIX, consts.NETWORK_TESTNET, chain_id)
identity = None
try:
if driver_config.factom_connection == DriverConfig.FACTOMD:
identity = factomd_jsonrpc_connection.get_identity(driver_config, did, chain_id, testnet=True)
elif driver_config.factom_connection == DriverConfig.TFA_EXPLORER:
identity = tfa_explorer_connection.get_identity(driver_config, did, chain_id, testnet=True)
elif driver_config.factom_connection == DriverConfig.HARMONY:
# TODO: switch this to use the Harmony connection if Harmony ever spins up a community testnet environment
# For now, fall back to using a factomd connection
identity = factomd_jsonrpc_connection.get_identity(driver_config, did, chain_id, testnet=True)
else:
bottle.abort(500) # Invalid connection type. This should never be executed.
except IdentityNotFoundException:
bottle.abort(404)
except ApiException:
bottle.abort(500) # Failed to make API call for some reason
return {'didDocument': identity.get_did_document(), 'methodMetadata': identity.get_method_metadata()}
@error(404)
def error404(e):
body = {'errors': {'detail': 'Page not found'}}
return json.dumps(body, separators=(',', ':'))
@error(405)
def error405(e):
body = {'errors': {'detail': 'Method not allowed'}}
return json.dumps(body, separators=(',', ':'))
@error(500)
def error500(e):
body = {'errors': {'detail': 'Internal server error'}}
return json.dumps(body, separators=(',', ':'))
# Entry point ONLY when run locally. The docker setup uses gunicorn and this block will not be executed.
if __name__ == '__main__':
run(host='localhost', port=8080)
app = bottle.default_app()