Merge pull request #1024 from bigchaindb/1021/http_api_root

1021/http api root
This commit is contained in:
libscott 2017-01-12 16:13:30 +01:00 committed by GitHub
commit 8510f47ed4
4 changed files with 75 additions and 8 deletions

View File

@ -22,6 +22,7 @@ def r(*args, **kwargs):
ROUTES_API_V1 = [
r('/', info.ApiV1Index),
r('statuses/', statuses.StatusApi),
r('transactions/<string:tx_id>', tx.TransactionApi),
r('transactions', tx.TransactionListApi),
@ -30,6 +31,6 @@ ROUTES_API_V1 = [
API_SECTIONS = [
(None, [r('/', info.IndexApi)]),
(None, [r('/', info.RootIndex)]),
('/api/v1/', ROUTES_API_V1),
]

View File

@ -1,8 +1,11 @@
from flask import jsonify
"""
Common classes and methods for API handlers
"""
from flask import jsonify, request
def make_error(status_code, message=None):
if status_code == 404 and message is None:
message = 'Not found'
@ -12,3 +15,8 @@ def make_error(status_code, message=None):
})
response.status_code = status_code
return response
def base_url():
return '%s://%s/' % (request.environ['wsgi.url_scheme'],
request.environ['HTTP_HOST'])

View File

@ -4,14 +4,42 @@ import flask
from flask_restful import Resource
import bigchaindb
from bigchaindb.web.views.base import base_url
from bigchaindb import version
class IndexApi(Resource):
class RootIndex(Resource):
def get(self):
docs_url = [
'https://docs.bigchaindb.com/projects/server/en/',
version.__short_version__ + '/'
]
api_v1_url = base_url() + 'api/v1/'
return flask.jsonify({
'_links': {
'docs': ''.join(docs_url),
'api_v1': api_v1_url,
},
'software': 'BigchainDB',
'version': version.__version__,
'public_key': bigchaindb.config['keypair']['public'],
'keyring': bigchaindb.config['keyring']
})
class ApiV1Index(Resource):
def get(self):
api_root = base_url() + 'api/v1/'
docs_url = [
'https://docs.bigchaindb.com/projects/server/en/',
version.__short_version__,
'/drivers-clients/http-client-server-api.html',
]
return {
"_links": {
"docs": ''.join(docs_url),
"self": api_root,
"statuses": api_root + "statuses/",
"transactions": api_root + "transactions/",
},
}

View File

@ -1,5 +1,35 @@
def test_api_root_url_shows_basic_info(client):
from bigchaindb import version
from unittest import mock
@mock.patch('bigchaindb.version.__short_version__', 'tst')
@mock.patch('bigchaindb.version.__version__', 'tsttst')
@mock.patch('bigchaindb.config', {'keyring': ['abc'], 'keypair': {'public': 'def'}})
def test_api_root_endpoint(client):
res = client.get('/')
assert res.json['software'] == 'BigchainDB'
assert res.json['version'] == version.__version__
assert res.json == {
'_links': {
'docs': 'https://docs.bigchaindb.com/projects/server/en/tst/',
'api_v1': 'http://localhost/api/v1/',
},
'version': 'tsttst',
'keyring': ['abc'],
'public_key': 'def',
'software': 'BigchainDB',
}
@mock.patch('bigchaindb.version.__short_version__', 'tst')
def test_api_v1_endpoint(client):
res = client.get('/api/v1')
docs_url = ['https://docs.bigchaindb.com/projects/server/en/',
'tst',
'/drivers-clients/http-client-server-api.html',
]
assert res.json == {
'_links': {
'docs': ''.join(docs_url),
'self': 'http://localhost/api/v1/',
'statuses': 'http://localhost/api/v1/statuses/',
'transactions': 'http://localhost/api/v1/transactions/',
}
}