Browse Source

the api/index is always visible, not a plugable blueprint

master
Julio Biason 11 years ago
parent
commit
897e80b397
  1. 37
      luncho/blueprints/index.py
  2. 37
      luncho/server.py

37
luncho/blueprints/index.py

@ -1,37 +0,0 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""The index blueprint. There is nothing here, we just show a summary of
the current API requests."""
from operator import itemgetter
from flask import Blueprint
from flask import jsonify
from flask import current_app
index = Blueprint('index', __name__)
@index.route('', methods=['GET'])
def show_routes():
"""List the current API"""
routes = []
for rule in current_app.url_map.iter_rules():
endpoint = rule.endpoint
if endpoint == 'static':
# the server does not have a static path, but Flask automatically
# registers it. so we just ignore it.
continue
path = str(rule)
# methods = rule.methods
doc = current_app.view_functions[endpoint].__doc__
routes.append([
path,
doc
])
routes.sort(key=itemgetter(0))
return jsonify(status='OK', api=routes)

37
luncho/server.py

@ -6,7 +6,10 @@ import json
import hmac
import datetime
from operator import itemgetter
from flask import Flask
from flask import jsonify
from luncho.exceptions import LunchoException
@ -98,17 +101,47 @@ class Group(db.Model):
# ----------------------------------------------------------------------
# Blueprints
# ----------------------------------------------------------------------
from blueprints.index import index
from blueprints.users import users
from blueprints.token import token
from blueprints.groups import groups
app.register_blueprint(index, url_prefix='/')
app.register_blueprint(token, url_prefix='/token/')
app.register_blueprint(users, url_prefix='/user/')
app.register_blueprint(groups, url_prefix='/group/')
# ----------------------------------------------------------------------
# The index is a special case
# ----------------------------------------------------------------------
@app.route('/', methods=['GET'])
def show_api():
"""Return the list of APIs."""
routes = []
for rule in app.url_map.iter_rules():
endpoint = rule.endpoint
if endpoint == 'static':
# the server does not have a static path, but Flask automatically
# registers it. so we just ignore it.
continue
path = str(rule)
# methods = rule.methods
doc = app.view_functions[endpoint].__doc__
# make the doc a little more... pretty
summary = doc.split('\n\n')[0]
summary = ' '.join(line.strip() for line in summary.split('\n'))
routes.append([
path,
summary
])
routes.sort(key=itemgetter(0))
return jsonify(status='OK', api=routes)
# ----------------------------------------------------------------------
# Error management
# ----------------------------------------------------------------------

Loading…
Cancel
Save