Remove host+port from / and /api/v1/

This commit is contained in:
tim 2017-06-14 11:09:42 +02:00
parent dda6517451
commit 673062f892
3 changed files with 32 additions and 34 deletions

View File

@ -21,14 +21,11 @@ def make_error(status_code, message=None):
return response
def base_url():
return '%s://%s/' % (request.environ['wsgi.url_scheme'],
request.environ['HTTP_HOST'])
def base_ws_uri():
"""Base websocket uri."""
# TODO Revisit as this is a workaround to address issue
# https://github.com/bigchaindb/bigchaindb/issues/1465.
# NOTE: This is now only for the WS API. Env-variable should be renamed to
# e.g. WS_HOST or WEBSOCKET_HOST
host = request.environ['HTTP_HOST'].split(':')[0]
return 'ws://{}:{}'.format(host, config['wsserver']['port'])

View File

@ -4,7 +4,7 @@ import flask
from flask_restful import Resource
import bigchaindb
from bigchaindb.web.views.base import base_url, base_ws_uri
from bigchaindb.web.views.base import base_ws_uri
from bigchaindb import version
from bigchaindb.web.websocket_server import EVENTS_ENDPOINT
@ -17,7 +17,7 @@ class RootIndex(Resource):
]
return flask.jsonify({
'api': {
'v1': get_api_v1_info()
'v1': get_api_v1_info('/api/v1/')
},
'docs': ''.join(docs_url),
'software': 'BigchainDB',
@ -29,15 +29,14 @@ class RootIndex(Resource):
class ApiV1Index(Resource):
def get(self):
return flask.jsonify(get_api_v1_info())
return flask.jsonify(get_api_v1_info('/'))
def get_api_v1_info():
def get_api_v1_info(api_prefix):
"""
Return a dict with all the information specific for the v1 of the
api.
"""
api_root = base_url() + 'api/v1/'
websocket_root = base_ws_uri() + EVENTS_ENDPOINT
docs_url = [
'https://docs.bigchaindb.com/projects/server/en/v',
@ -47,9 +46,9 @@ def get_api_v1_info():
return {
'docs': ''.join(docs_url),
'transactions': api_root + 'transactions/',
'statuses': api_root + 'statuses/',
'assets': api_root + 'assets/',
'outputs': api_root + 'outputs/',
'transactions': '{}transactions/'.format(api_prefix),
'statuses': '{}statuses/'.format(api_prefix),
'assets': '{}assets/'.format(api_prefix),
'outputs': '{}outputs/'.format(api_prefix),
'streams': websocket_root
}

View File

@ -1,31 +1,23 @@
from unittest import mock
import pytest
@pytest.fixture
def api_v1_info():
docs_url = ['https://docs.bigchaindb.com/projects/server/en/vtsttst',
'/http-client-server-api.html',
]
return {
'docs': ''.join(docs_url),
'transactions': 'http://localhost/api/v1/transactions/',
'statuses': 'http://localhost/api/v1/statuses/',
'assets': 'http://localhost/api/v1/assets/',
'outputs': 'http://localhost/api/v1/outputs/',
'streams': 'ws://localhost:9985/api/v1/streams/valid_transactions',
}
@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, api_v1_info):
def test_api_root_endpoint(client):
res = client.get('/')
docs_url = ['https://docs.bigchaindb.com/projects/server/en/vtsttst',
'/http-client-server-api.html']
assert res.json == {
'api': {
'v1': api_v1_info
'v1': {
'docs': ''.join(docs_url),
'transactions': '/api/v1/transactions/',
'statuses': '/api/v1/statuses/',
'assets': '/api/v1/assets/',
'outputs': '/api/v1/outputs/',
'streams': 'ws://localhost:9985/api/v1/streams/valid_transactions',
}
},
'docs': 'https://docs.bigchaindb.com/projects/server/en/vtsttst/',
'version': 'tsttst',
@ -37,6 +29,16 @@ def test_api_root_endpoint(client, api_v1_info):
@mock.patch('bigchaindb.version.__short_version__', 'tst')
@mock.patch('bigchaindb.version.__version__', 'tsttst')
def test_api_v1_endpoint(client, api_v1_info):
def test_api_v1_endpoint(client):
docs_url = ['https://docs.bigchaindb.com/projects/server/en/vtsttst',
'/http-client-server-api.html']
api_v1_info = {
'docs': ''.join(docs_url),
'transactions': '/transactions/',
'statuses': '/statuses/',
'assets': '/assets/',
'outputs': '/outputs/',
'streams': 'ws://localhost:9985/api/v1/streams/valid_transactions',
}
res = client.get('/api/v1')
assert res.json == api_v1_info