aboutsummaryrefslogtreecommitdiff
path: root/umosapi/api.py
blob: effd610576c6a7b10970dc035f3e2522c62ff859 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from flask import Flask, Blueprint, jsonify, request
from flask_restplus import Resource, Api, fields

from json import loads

from .utils import sanitize
from .app_db.uobject import UObject
from bson.objectid import ObjectId

app = Flask(__name__, instance_relative_config=True)

bp = Blueprint('api', __name__, url_prefix='/api')

env = app.config['DEBUG']

if env is not False:
    env = '/doc'

api = Api(
        bp,
        doc=env,
        title="UMoSApi",
        description="""
        Unity Mongo Save Api is a simple API for save Unity
        object in Mongo database. The terme uobject means Unity Object.
        """,
        version=0.3
    )

uobject = UObject(app)


class fieldsDict(fields.Raw):
    __schema_type__ = ["Dict"]
    __schema_example__ = {"key": "token"}


class fieldsObjectId(fields.Raw):
    __schema_type__ = ["ObjectId"]
    __schema_example__ = sanitize(ObjectId("5d25a0f9e396fac104529444"))


uobject_model = api.model('UObject', {
    '_id': fieldsObjectId(
        required=False,
        description="MongoDB _id, don't set it, this is only for \
                    documentation purpose"
    ),
    'datas': fieldsDict(
        required=True,
        description='Variables of the uobject in JSON format.',
        example={"key": "token"}
    ),
    'msg': fields.String(
        required=False,
        description="Debug on request, don't set it, this is only\
                    for documentation purpose",
        example="All is fine :)"
    )
})


@api.route('/<collections>')
class ObjectsList(Resource):
    @api.doc(params={'collections': 'players'})
    @api.response(404, 'No UObjects in collection.')
    @api.marshal_list_with(uobject_model, mask=None)
    def get(self, collections):
        """ Get uobjects list """
        return sanitize(uobject.all(collections))

    @api.expect(uobject_model)
    @api.doc(params={'collections': 'players'})
    @api.marshal_with(uobject_model, code=201, mask=None)
    def post(self, collections):
        """ Register new uobject """
        args = request.get_json(force=True)
        return uobject.register(collections, args.get('datas')), 201


@api.route("/<collections>/<_id>")
class Objects(Resource):
    @api.expect(uobject_model)
    @api.doc(params={'collections': 'players', '_id': '5d25a0f9e396fac104529444'})
    @api.response(200, 'UObjects updated.')
    @api.response(404, 'UObjects does not exist.')
    @api.marshal_with(uobject_model, mask=None)
    def patch(self, collections, _id):
        """ Edit an uobject. """
        args = request.get_json(force=True)
        return uobject.update(collections, _id, args.get('datas')),

    @api.doc(params={'collections': 'players', '_id': '5d244cc13f3d46cb739912ae'})
    @api.response(200, 'UObjects deleted.')
    @api.response(400, 'Something strange append, check msg value\
                in response.')
    @api.response(404, 'UObjects does not exist.')
    @api.marshal_with(uobject_model, mask=None)
    def delete(self, collections, _id):
        """ Remove an uobject """
        return uobject.remove(collections, _id)


@api.route("/<collections>/<key>/<value>")
class ObjectsEq(Resource):
    @api.doc(params={
        'collections': 'players',
        'key': 'kill or total.kill',
        'value': '12',
        })
    @api.response(404, 'No UObjects in collection.')
    @api.marshal_list_with(uobject_model, mask=None)
    def get(self, collections, key, value):
        """ Get uobjects matching key/value """
        return sanitize(uobject.eq(collections, key, value))


@api.route("/<collections>/reset")
class ObjectsClean(Resource):
    @api.doc(params={'collections': 'players'})
    def get(self, collections):
        """ Reset collection """
        uobject.reset(collections)
        return "done"