Create a simple restful API mounted at /v1/

Add a list of resources we can serve.
Update themes page to list our resources.
This commit is contained in:
Andy Williams 2016-05-01 12:48:54 +01:00
parent dbf4134b15
commit 16ed007edf
2 changed files with 33 additions and 2 deletions

28
app.py
View File

@ -1,6 +1,9 @@
from flask import Flask
from flask import render_template
from flask import abort, render_template
from flask_restful import Resource, Api, abort as api_abort
app = Flask(__name__)
api = Api(app)
@app.route('/')
def welcome():
@ -10,13 +13,34 @@ def welcome():
def about():
return render_template('about.html')
THEMES = {
'1': {'theme_id': '1'},
'a': {'theme_id': 'a'},
}
class ThemeList(Resource):
def get(self):
return THEMES
class Theme(Resource):
def get(self, theme_id):
if theme_id not in THEMES:
api_abort(404, message="Theme {} not found".format(theme_id))
return THEMES[theme_id]
@app.route('/themes/')
@app.route('/themes/<theme_id>')
def themes(theme_id=None):
if theme_id:
if theme_id not in THEMES:
abort(404)
return render_template('theme.html', theme_id=theme_id)
else:
return render_template('themes.html')
return render_template('themes.html', themes=THEMES)
api.add_resource(ThemeList, '/v1/themes/')
api.add_resource(Theme, '/v1/themes/<string:theme_id>')
if __name__ == '__main__':
app.run(debug=True)

View File

@ -2,3 +2,10 @@
{% block title %}Themes{% endblock %}
{% block heading %}Themes{% endblock %}
{% block content %}
<ul>
{% for theme_id in themes %}
<li><a href="{{ theme_id }}">theme {{ themes[theme_id].theme_id }}</a></li>
{% endfor %}
</ul>
{% endblock %}