diff options
author | romangraef <roman.graef@gmail.com> | 2018-06-09 12:28:18 +0200 |
---|---|---|
committer | romangraef <roman.graef@gmail.com> | 2018-06-09 12:28:18 +0200 |
commit | bb27efebb9ebb30e85e780758d259bcb72a699c4 (patch) | |
tree | fdeb8218f46c78ffd3800640308d4753f287af42 /api/v1/server.py | |
download | datapackmanagerapi-bb27efebb9ebb30e85e780758d259bcb72a699c4.tar.gz datapackmanagerapi-bb27efebb9ebb30e85e780758d259bcb72a699c4.tar.bz2 datapackmanagerapi-bb27efebb9ebb30e85e780758d259bcb72a699c4.zip |
Initial commit
Diffstat (limited to 'api/v1/server.py')
-rw-r--r-- | api/v1/server.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/api/v1/server.py b/api/v1/server.py new file mode 100644 index 0000000..96ff8da --- /dev/null +++ b/api/v1/server.py @@ -0,0 +1,51 @@ +from flask import Blueprint, jsonify, abort, make_response, request + +from .db import Category, DataPack +from .util import model_paginator, query_paginator, ConstrainFailed + +bp = Blueprint("api v1", __name__) + + +@bp.errorhandler(ConstrainFailed) +def handler(e: ConstrainFailed): + return make_response(jsonify({ + 'error': { + 'description': e.description, + 'parameter': e.parameter, + 'reason': e.reason, + } + }), 404) + + +@bp.route('/version/') +def version(): + return jsonify({ + 'version': '1.0.0', + 'deprecated': False, + }) + + +@bp.route('/list/categories/') +def list_categories(): + return model_paginator(Category, lambda cat: { + 'name': cat.name, + 'id': cat.id, + }) + + +@bp.route('/list/datapacks') +def list_datapacks(): + category = request.args.get('category', '') + query = DataPack.select() + if category != '': + query = query.join(Category).where(DataPack.category.id == category) + return query_paginator(query, lambda dp: { + 'name': dp.name, + 'id': dp.id, + 'category': dp.category.id, + }) + + +@bp.route('/<path:invalid_path>') +def invalid_path(invalid_path): + abort(404) |