mirror of
https://github.com/alexgo-io/stacks-puppet-node.git
synced 2026-04-23 03:20:19 +08:00
Merge branch 'master' into develop-mock-insight-api
This commit is contained in:
@@ -27,5 +27,6 @@ import s3
|
||||
import blockstack_resolver
|
||||
import blockstack_server
|
||||
import http
|
||||
import gaia_hub
|
||||
|
||||
from common import index_settings_get_index_manifest_url, ConcurrencyViolationException
|
||||
|
||||
88
blockstack_client/backend/drivers/gaia_hub.py
Normal file
88
blockstack_client/backend/drivers/gaia_hub.py
Normal file
@@ -0,0 +1,88 @@
|
||||
import requests, os
|
||||
from ConfigParser import SafeConfigParser
|
||||
from blockstack_client.logger import get_logger
|
||||
|
||||
ACCESS_TOKEN = None
|
||||
ACCESS_ADDRESS = None
|
||||
HUB_SERVER = None
|
||||
HUB_URL_PREFIX = None
|
||||
|
||||
log = get_logger()
|
||||
|
||||
def storage_init(conf, **kwargs):
|
||||
config_path = conf['path']
|
||||
|
||||
global ACCESS_TOKEN, ACCESS_ADDRESS, HUB_SERVER, HUB_URL_PREFIX
|
||||
|
||||
if os.path.exists( config_path ):
|
||||
parser = SafeConfigParser()
|
||||
try:
|
||||
parser.read(config_path)
|
||||
except Exception, e:
|
||||
log.exception(e)
|
||||
return False
|
||||
|
||||
if parser.has_section('gaia_hub'):
|
||||
if parser.has_option('gaia_hub', 'token'):
|
||||
ACCESS_TOKEN = parser.get('gaia_hub', 'token')
|
||||
if parser.has_option('gaia_hub', 'address'):
|
||||
ACCESS_ADDRESS = parser.get('gaia_hub', 'address')
|
||||
if parser.has_option('gaia_hub', 'server'):
|
||||
HUB_SERVER = parser.get('gaia_hub', 'server')
|
||||
if parser.has_option('gaia_hub', 'url_prefix'):
|
||||
HUB_URL_PREFIX = parser.get('gaia_hub', 'url_prefix')
|
||||
|
||||
return True
|
||||
|
||||
def handles_url( url ):
|
||||
return url.startswith(HUB_URL_PREFIX)
|
||||
|
||||
def data_id_to_hex( data_id ):
|
||||
return "".join(x.encode('hex') for x in data_id)
|
||||
|
||||
def make_mutable_url( data_id ):
|
||||
path = data_id_to_hex(data_id)
|
||||
url = "{}{}/{}".format(HUB_URL_PREFIX, ACCESS_ADDRESS, path)
|
||||
log.debug( "make_mutable_url: {}".format(url))
|
||||
return url
|
||||
|
||||
def put_mutable_handler( data_id, data_txt, **kw ):
|
||||
url = "{}/store/{}/{}".format(
|
||||
HUB_SERVER,
|
||||
ACCESS_ADDRESS,
|
||||
data_id_to_hex( data_id ))
|
||||
headers = {
|
||||
"Authorization" : "bearer {}".format(ACCESS_TOKEN)
|
||||
}
|
||||
log.debug( "put_mutable_url: {}".format(url))
|
||||
|
||||
resp = requests.post( url, headers = headers,
|
||||
data = data_txt )
|
||||
if resp.status_code != 202:
|
||||
log.error(resp)
|
||||
msg = "Error putting to mutable storage. Tried store at {}".format(url)
|
||||
log.error(msg)
|
||||
raise Exception(msg)
|
||||
log.debug(resp)
|
||||
resp_obj = resp.json()
|
||||
if 'publicURL' not in resp_obj:
|
||||
msg = "Expectin publicURL in JSON response"
|
||||
raise Exception(msg)
|
||||
elif resp_obj['publicURL'] != make_mutable_url(data_id):
|
||||
msg = "Unexpected publicURL. Expected '{}', Actual '{}'".format(
|
||||
make_mutable_url(data_id), resp_obj['publicURL'])
|
||||
raise Exception(msg)
|
||||
return True
|
||||
|
||||
def get_mutable_handler( data_url, **kw):
|
||||
log.debug("get_mutable: {}".format(data_url))
|
||||
resp = requests.get( data_url )
|
||||
if resp.status_code != 200:
|
||||
log.error(resp)
|
||||
msg = "Error getting from mutable storage. Tried store at {}".format(data_url)
|
||||
log.error(msg)
|
||||
raise Exception(msg)
|
||||
return resp.content
|
||||
|
||||
def get_classes():
|
||||
return ['read_public', 'write_private']
|
||||
@@ -2802,6 +2802,28 @@ class BlockstackAPIEndpointHandler(SimpleHTTPRequestHandler):
|
||||
|
||||
self._reply_json({'error' : 'Unimplemented'}, status_code = 405)
|
||||
|
||||
def GET_blockchain_num_names( self, ses, path_info, blockchain_name ):
|
||||
"""
|
||||
Handle GET /blockchains/:blockchainID/name_count
|
||||
Reply with the number of names on this blockchain
|
||||
"""
|
||||
if blockchain_name != 'bitcoin':
|
||||
# not supported
|
||||
self._reply_json({'error': 'Unsupported blockchain'}, status_code=401)
|
||||
return
|
||||
num_names = proxy.get_num_names()
|
||||
if json_is_error(num_names):
|
||||
if json_is_exception(info):
|
||||
status_code = 500
|
||||
else:
|
||||
status_code = 404
|
||||
|
||||
self._reply_json({'error': info['error']}, status_code=status_code)
|
||||
return
|
||||
|
||||
self._reply_json({'names_count': num_names})
|
||||
return
|
||||
|
||||
def GET_blockchain_consensus( self, ses, path_info, blockchain_name ):
|
||||
"""
|
||||
Handle GET /blockchain/:blockchainID/consensus
|
||||
@@ -3029,6 +3051,20 @@ class BlockstackAPIEndpointHandler(SimpleHTTPRequestHandler):
|
||||
},
|
||||
},
|
||||
},
|
||||
r'^/v1/blockchains/({})/name_count'.format(URLENCODING_CLASS) : {
|
||||
'routes': {
|
||||
'GET': self.GET_blockchain_num_names
|
||||
},
|
||||
'whitelist': {
|
||||
'GET': {
|
||||
'name': 'blockchain',
|
||||
'desc': 'read blockchain name blocks',
|
||||
'auth_session': False,
|
||||
'auth_pass': False,
|
||||
'need_data_key': False,
|
||||
},
|
||||
},
|
||||
},
|
||||
r'^/v1/blockchains/({})/operations/([0-9]+)$'.format(URLENCODING_CLASS): {
|
||||
'routes': {
|
||||
'GET': self.GET_blockchain_ops
|
||||
|
||||
Reference in New Issue
Block a user