aboutsummaryrefslogtreecommitdiff
path: root/umosapi/api.py
blob: 9729735fddb99de12df3c4e30a426823bad3f42b (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
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.1
    )

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('/objects')
class ObjectsList(Resource):
    @api.response(404, 'No UObjects in collection.')
    @api.marshal_list_with(uobject_model, mask=None)
    def get(self):
        """ Get uobjects list """
        return sanitize(uobject.all())

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


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

    @api.doc(params={'_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, _id):
        """ Remove an uobject """
        return uobject.remove(_id)


@api.route("/objects/<key>/<value>")
class ObjectsEq(Resource):
    @api.doc(params={
        '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, key, value):
        """ Get uobjects matching key/value """
        return sanitize(uobject.eq(key, value))


@api.route("/objects/reset")
class ObjectsClean(Resource):
    def get(self):
        """ Reset collection """
        uobject.reset()
        return "done"