2019-01-06 22:58:33 -08:00
|
|
|
import requests
|
2019-03-14 13:29:02 -07:00
|
|
|
from flask import current_app
|
|
|
|
|
2019-03-13 16:36:06 -07:00
|
|
|
from grant.settings import BLOCKCHAIN_REST_API_URL, BLOCKCHAIN_API_SECRET, E2E_TESTING
|
2019-03-14 13:29:02 -07:00
|
|
|
from grant.utils.exceptions import ValidationException
|
2019-01-08 14:58:32 -08:00
|
|
|
|
2019-01-23 07:00:30 -08:00
|
|
|
|
2019-01-08 14:58:32 -08:00
|
|
|
### REST API ###
|
2019-01-06 22:58:33 -08:00
|
|
|
|
2019-01-08 09:44:54 -08:00
|
|
|
def handle_res(res):
|
2019-01-08 14:58:32 -08:00
|
|
|
j = res.json()
|
|
|
|
if j.get('error'):
|
|
|
|
raise Exception('Blockchain API Error: {}'.format(j['error']))
|
|
|
|
return j['data']
|
2019-01-06 22:58:33 -08:00
|
|
|
|
2019-01-23 07:00:30 -08:00
|
|
|
|
|
|
|
def blockchain_get(path, params=None):
|
2019-03-13 16:36:06 -07:00
|
|
|
if E2E_TESTING:
|
|
|
|
return blockchain_rest_e2e(path, params)
|
2019-03-14 13:29:02 -07:00
|
|
|
try:
|
|
|
|
res = requests.get(
|
|
|
|
f'{BLOCKCHAIN_REST_API_URL}{path}',
|
|
|
|
headers={'authorization': BLOCKCHAIN_API_SECRET},
|
|
|
|
params=params,
|
|
|
|
)
|
|
|
|
return handle_res(res)
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error(f"Unable to contact node: {e}")
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
2019-01-23 07:00:30 -08:00
|
|
|
def blockchain_post(path, data=None):
|
2019-03-13 16:36:06 -07:00
|
|
|
if E2E_TESTING:
|
|
|
|
return blockchain_rest_e2e(path, data)
|
2019-03-14 13:29:02 -07:00
|
|
|
try:
|
|
|
|
res = requests.post(
|
|
|
|
f'{BLOCKCHAIN_REST_API_URL}{path}',
|
|
|
|
headers={'authorization': BLOCKCHAIN_API_SECRET},
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
return handle_res(res)
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error(f"Unable to contact node: {e}")
|
|
|
|
raise e
|
2019-03-13 16:36:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
def blockchain_rest_e2e(path, data):
|
|
|
|
if '/bootstrap' in path:
|
|
|
|
return {
|
|
|
|
'startHeight': 123,
|
|
|
|
'currentHeight': 456,
|
|
|
|
}
|
|
|
|
if '/contribution/addresses' in path:
|
|
|
|
return {
|
|
|
|
'transparent': 't123',
|
|
|
|
}
|
|
|
|
|
|
|
|
raise Exception(f'blockchain_post_e2e does not recognize path: {path}')
|