2019-01-06 22:58:33 -08:00
|
|
|
import requests
|
|
|
|
import json
|
2019-01-09 11:07:50 -08:00
|
|
|
from grant.settings import BLOCKCHAIN_REST_API_URL, BLOCKCHAIN_API_SECRET
|
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-08 09:44:54 -08:00
|
|
|
def blockchain_get(path, params = None):
|
2019-01-08 14:58:32 -08:00
|
|
|
res = requests.get(
|
|
|
|
f'{BLOCKCHAIN_REST_API_URL}{path}',
|
|
|
|
headers={ 'authorization': BLOCKCHAIN_API_SECRET },
|
|
|
|
params=params,
|
|
|
|
)
|
|
|
|
return handle_res(res)
|
2019-01-06 22:58:33 -08:00
|
|
|
|
2019-01-08 09:44:54 -08:00
|
|
|
def blockchain_post(path, data = None):
|
2019-01-08 14:58:32 -08:00
|
|
|
res = requests.post(
|
|
|
|
f'{BLOCKCHAIN_REST_API_URL}{path}',
|
|
|
|
headers={ 'authorization': BLOCKCHAIN_API_SECRET },
|
|
|
|
data=json.dumps(data),
|
|
|
|
)
|
|
|
|
return handle_res(res)
|