summaryrefslogtreecommitdiff
path: root/api/v1/server.py
blob: 96ff8dae64395c4c85e40f014e2bead89b0ed47f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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)