From 16ed007edfb3024f06d033e1828041fbe460d8de Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Sun, 1 May 2016 12:48:54 +0100 Subject: [PATCH] Create a simple restful API mounted at /v1/ Add a list of resources we can serve. Update themes page to list our resources. --- app.py | 28 ++++++++++++++++++++++++++-- templates/themes.html | 7 +++++++ 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 @@ 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/') 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/') if __name__ == '__main__': 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 @@ {% block title %}Themes{% endblock %} {% block heading %}Themes{% endblock %} +{% block content %} + +{% endblock %}