aboutsummaryrefslogtreecommitdiff
path: root/umosapi/api.py
diff options
context:
space:
mode:
Diffstat (limited to 'umosapi/api.py')
-rw-r--r--umosapi/api.py61
1 files changed, 37 insertions, 24 deletions
diff --git a/umosapi/api.py b/umosapi/api.py
index 6ef6ce6..9729735 100644
--- a/umosapi/api.py
+++ b/umosapi/api.py
@@ -5,6 +5,7 @@ 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)
@@ -34,53 +35,68 @@ class fieldsDict(fields.Raw):
__schema_example__ = {"key": "token"}
+class fieldsObjectId(fields.Raw):
+ __schema_type__ = ["ObjectId"]
+ __schema_example__ = sanitize(ObjectId("5d25a0f9e396fac104529444"))
+
+
uobject_model = api.model('UObject', {
- 'datas': fieldsDict(
+ '_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 """
- uobjects = uobject.all()
- if not uobjects:
- return loads('{"msg": "No uobjects"}'), 404
- return sanitize(uobjects), 200
+ 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)
- status = uobject.register(args.get('datas'))
-
- if '_id' in status:
- return {"_id": status['_id'], "msg": status['msg']}, status['code']
- else:
- return {"msg": status['msg']}, status['code']
+ return uobject.register(args.get('datas')), 201
@api.route("/objects/<_id>")
class Objects(Resource):
@api.expect(uobject_model)
- @api.doc(params={'_id': '5d244cc13f3d46cb739912ae'})
+ @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)
- status = uobject.update(_id, args.get('datas'))
-
- return {"msg": status['msg']}, status['code']
+ 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 """
- status = uobject.remove(_id)
-
- return {"msg": status['msg']}, status['code']
+ return uobject.remove(_id)
@api.route("/objects/<key>/<value>")
@@ -89,14 +105,11 @@ class ObjectsEq(Resource):
'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 """
- status = uobject.eq(key, value)
-
- return {
- "msg": status['msg'],
- "datas": sanitize(status['datas'])
- }, status['code']
+ return sanitize(uobject.eq(key, value))
@api.route("/objects/reset")