Merge pull request #1022 from bigchaindb/flask_routes
Collect http routes into single module
This commit is contained in:
commit
f9ed5690a7
|
@ -0,0 +1,30 @@
|
|||
""" API routes definition """
|
||||
from flask_restful import Api
|
||||
from bigchaindb.web.views import info, transactions as tx, unspents
|
||||
|
||||
|
||||
def add_routes(app):
|
||||
""" Add the routes to an app """
|
||||
for (prefix, routes) in API_SECTIONS:
|
||||
api = Api(app, prefix=prefix)
|
||||
for ((pattern, resource, *args), kwargs) in routes:
|
||||
kwargs.setdefault('strict_slashes', False)
|
||||
api.add_resource(resource, pattern, *args, **kwargs)
|
||||
|
||||
|
||||
def r(*args, **kwargs):
|
||||
return (args, kwargs)
|
||||
|
||||
|
||||
ROUTES_API_V1 = [
|
||||
r('transactions/<string:tx_id>', tx.TransactionApi),
|
||||
r('transactions/<string:tx_id>/status', tx.TransactionStatusApi),
|
||||
r('transactions', tx.TransactionListApi),
|
||||
r('unspents/', unspents.UnspentListApi),
|
||||
]
|
||||
|
||||
|
||||
API_SECTIONS = [
|
||||
(None, [r('/', info.IndexApi)]),
|
||||
('/api/v1/', ROUTES_API_V1),
|
||||
]
|
|
@ -11,9 +11,7 @@ import gunicorn.app.base
|
|||
|
||||
from bigchaindb import utils
|
||||
from bigchaindb import Bigchain
|
||||
from bigchaindb.web.views.info import info_views
|
||||
from bigchaindb.web.views.transactions import transaction_views
|
||||
from bigchaindb.web.views.unspents import unspent_views
|
||||
from bigchaindb.web.routes import add_routes
|
||||
|
||||
from bigchaindb.monitor import Monitor
|
||||
|
||||
|
@ -69,9 +67,8 @@ def create_app(*, debug=False, threads=4):
|
|||
app.config['bigchain_pool'] = utils.pool(Bigchain, size=threads)
|
||||
app.config['monitor'] = Monitor()
|
||||
|
||||
app.register_blueprint(info_views, url_prefix='/')
|
||||
app.register_blueprint(transaction_views, url_prefix='/api/v1')
|
||||
app.register_blueprint(unspent_views, url_prefix='/api/v1')
|
||||
add_routes(app)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
|
|
|
@ -1,24 +1,17 @@
|
|||
"""This module provides the blueprint for some basic API endpoints.
|
||||
|
||||
For more information please refer to the documentation on ReadTheDocs:
|
||||
- https://docs.bigchaindb.com/projects/server/en/latest/drivers-clients/http-client-server-api.html
|
||||
"""
|
||||
""" API Index endpoint """
|
||||
|
||||
import flask
|
||||
from flask import Blueprint
|
||||
from flask_restful import Resource
|
||||
|
||||
import bigchaindb
|
||||
from bigchaindb import version
|
||||
|
||||
|
||||
info_views = Blueprint('info_views', __name__)
|
||||
|
||||
|
||||
@info_views.route('/')
|
||||
def home():
|
||||
return flask.jsonify({
|
||||
'software': 'BigchainDB',
|
||||
'version': version.__version__,
|
||||
'public_key': bigchaindb.config['keypair']['public'],
|
||||
'keyring': bigchaindb.config['keyring']
|
||||
})
|
||||
class IndexApi(Resource):
|
||||
def get(self):
|
||||
return flask.jsonify({
|
||||
'software': 'BigchainDB',
|
||||
'version': version.__version__,
|
||||
'public_key': bigchaindb.config['keypair']['public'],
|
||||
'keyring': bigchaindb.config['keyring']
|
||||
})
|
||||
|
|
|
@ -4,8 +4,8 @@ For more information please refer to the documentation on ReadTheDocs:
|
|||
- https://docs.bigchaindb.com/projects/server/en/latest/drivers-clients/
|
||||
http-client-server-api.html
|
||||
"""
|
||||
from flask import current_app, request, Blueprint
|
||||
from flask_restful import Resource, Api
|
||||
from flask import current_app, request
|
||||
from flask_restful import Resource
|
||||
|
||||
from bigchaindb.common.exceptions import (
|
||||
AmountError,
|
||||
|
@ -25,32 +25,6 @@ from bigchaindb.models import Transaction
|
|||
from bigchaindb.web.views.base import make_error
|
||||
|
||||
|
||||
transaction_views = Blueprint('transaction_views', __name__)
|
||||
transaction_api = Api(transaction_views)
|
||||
|
||||
|
||||
# TODO: Do we really need this?
|
||||
# Unfortunately I cannot find a reference to this decorator.
|
||||
# This answer on SO is quite useful tho:
|
||||
# - http://stackoverflow.com/a/13432373/597097
|
||||
@transaction_views.record
|
||||
def record(state):
|
||||
"""This function checks if the blueprint can be initialized
|
||||
with the provided state."""
|
||||
|
||||
bigchain_pool = state.app.config.get('bigchain_pool')
|
||||
monitor = state.app.config.get('monitor')
|
||||
|
||||
if bigchain_pool is None:
|
||||
raise Exception('This blueprint expects you to provide '
|
||||
'a pool of Bigchain instances called `bigchain_pool`')
|
||||
|
||||
if monitor is None:
|
||||
raise ValueError('This blueprint expects you to provide '
|
||||
'a monitor instance to record system '
|
||||
'performance.')
|
||||
|
||||
|
||||
class TransactionApi(Resource):
|
||||
def get(self, tx_id):
|
||||
"""API endpoint to get details about a transaction.
|
||||
|
@ -145,14 +119,3 @@ class TransactionListApi(Resource):
|
|||
bigchain.write_transaction(tx_obj)
|
||||
|
||||
return tx
|
||||
|
||||
|
||||
transaction_api.add_resource(TransactionApi,
|
||||
'/transactions/<string:tx_id>',
|
||||
strict_slashes=False)
|
||||
transaction_api.add_resource(TransactionStatusApi,
|
||||
'/transactions/<string:tx_id>/status',
|
||||
strict_slashes=False)
|
||||
transaction_api.add_resource(TransactionListApi,
|
||||
'/transactions',
|
||||
strict_slashes=False)
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
from flask import current_app, Blueprint
|
||||
from flask_restful import reqparse, Resource, Api
|
||||
|
||||
|
||||
unspent_views = Blueprint('unspent_views', __name__)
|
||||
unspent_api = Api(unspent_views)
|
||||
from flask import current_app
|
||||
from flask_restful import reqparse, Resource
|
||||
|
||||
|
||||
class UnspentListApi(Resource):
|
||||
|
@ -25,8 +21,3 @@ class UnspentListApi(Resource):
|
|||
unspents = bigchain.get_owned_ids(args['public_key'])
|
||||
# NOTE: We pass '..' as a path to create a valid relative URI
|
||||
return [u.to_uri('..') for u in unspents]
|
||||
|
||||
|
||||
unspent_api.add_resource(UnspentListApi,
|
||||
'/unspents/',
|
||||
strict_slashes=False)
|
||||
|
|
Loading…
Reference in New Issue