summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Williams <andy@andywilliams.me>2016-05-01 12:48:54 +0100
committerAndy Williams <andy@andywilliams.me>2016-05-01 12:48:54 +0100
commit16ed007edfb3024f06d033e1828041fbe460d8de (patch)
tree2efff185cd72fe99a5a06a04ba380010f97d8394
parentdbf4134b15e4c2135a9cb9d56a1cd41c2a83b6a1 (diff)
Create a simple restful API mounted at /v1/
Add a list of resources we can serve. Update themes page to list our resources.
-rw-r--r--app.py28
-rw-r--r--templates/themes.html7
2 files changed, 33 insertions, 2 deletions
diff --git a/app.py b/app.py
index 399205c..3127b5c 100644
--- a/app.py
+++ b/app.py
@@ -1,6 +1,9 @@
1from flask import Flask 1from flask import Flask
2from flask import render_template 2from flask import abort, render_template
3from flask_restful import Resource, Api, abort as api_abort
4
3app = Flask(__name__) 5app = Flask(__name__)
6api = Api(app)
4 7
5@app.route('/') 8@app.route('/')
6def welcome(): 9def welcome():
@@ -10,13 +13,34 @@ def welcome():
10def about(): 13def about():
11 return render_template('about.html') 14 return render_template('about.html')
12 15
16THEMES = {
17 '1': {'theme_id': '1'},
18 'a': {'theme_id': 'a'},
19}
20
21class ThemeList(Resource):
22 def get(self):
23 return THEMES
24
25class Theme(Resource):
26 def get(self, theme_id):
27 if theme_id not in THEMES:
28 api_abort(404, message="Theme {} not found".format(theme_id))
29
30 return THEMES[theme_id]
31
13@app.route('/themes/') 32@app.route('/themes/')
14@app.route('/themes/<theme_id>') 33@app.route('/themes/<theme_id>')
15def themes(theme_id=None): 34def themes(theme_id=None):
16 if theme_id: 35 if theme_id:
36 if theme_id not in THEMES:
37 abort(404)
17 return render_template('theme.html', theme_id=theme_id) 38 return render_template('theme.html', theme_id=theme_id)
18 else: 39 else:
19 return render_template('themes.html') 40 return render_template('themes.html', themes=THEMES)
41
42api.add_resource(ThemeList, '/v1/themes/')
43api.add_resource(Theme, '/v1/themes/<string:theme_id>')
20 44
21if __name__ == '__main__': 45if __name__ == '__main__':
22 app.run(debug=True) 46 app.run(debug=True)
diff --git a/templates/themes.html b/templates/themes.html
index efae47f..aa6eb4c 100644
--- a/templates/themes.html
+++ b/templates/themes.html
@@ -2,3 +2,10 @@
2{% block title %}Themes{% endblock %} 2{% block title %}Themes{% endblock %}
3{% block heading %}Themes{% endblock %} 3{% block heading %}Themes{% endblock %}
4 4
5{% block content %}
6<ul>
7{% for theme_id in themes %}
8 <li><a href="{{ theme_id }}">theme {{ themes[theme_id].theme_id }}</a></li>
9{% endfor %}
10</ul>
11{% endblock %}