aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2019-07-10 15:37:21 +0200
committerneodarz <neodarz@neodarz.net>2019-07-10 15:37:21 +0200
commitbca82155adfbd99455c639faab18274c52f3428c (patch)
treed07ae4adf86cd599e56bb6969764f49b324c9350
parentfdfa73cf2bdb9801dfd0c50e3514c63442d019b8 (diff)
downloadumosapi-0.2.tar.xz
umosapi-0.2.zip
Clean apiv0.2
-rw-r--r--umosapi/api.py61
-rw-r--r--umosapi/app_db/uobject.py61
2 files changed, 58 insertions, 64 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")
diff --git a/umosapi/app_db/uobject.py b/umosapi/app_db/uobject.py
index 78b8bc8..cffd9ba 100644
--- a/umosapi/app_db/uobject.py
+++ b/umosapi/app_db/uobject.py
@@ -1,3 +1,4 @@
+import re
from flask import current_app
from bson.objectid import ObjectId
@@ -16,79 +17,59 @@ class UObject(object):
def register(self, datas):
db = MongoDB(self.app)
mongo = db.connection()
- error = {}
if type(datas) != dict:
error = {
- "msg": "UObject datas type is not dict (JSON).",
- "code": 400
+ "msg": "UObject datas type is not dict (JSON)."
}
+ return error, 400
- if not error:
- _id = mongo.db.uobjects.insert({"datas": datas})
- return {"_id": str(_id), "msg": "UObject added.", "code": 201}
- return error
+ _id = mongo.db.uobjects.insert({"datas": datas})
+ return {"_id": str(_id)}
def remove(self, _id):
db = MongoDB(self.app)
mongo = db.connection()
- error = {}
- if not _id:
- error = {"msg": "UObject _id is required", "code": 400}
- elif len(list(mongo.db.uobjects.find({"_id": ObjectId(_id)}))) == 0:
+ if not re.match('[0-9a-z]{24}', _id):
error = {
- "msg": "UObject {} not exist. So it's good.".format(_id),
- "code": 404
+ "msg": "UObject _id is must match following regex [0-9a-z]{24}"
}
+ return error, 400
+ if len(list(mongo.db.uobjects.find({"_id": ObjectId(_id)}))) == 0:
+ return '', 404
- if not error:
- mongo.db.uobjects.remove({"_id": ObjectId(_id)})
- return {"msg": "UObject {} deleted".format(_id), "code": 200}
- return error
+ mongo.db.uobjects.remove({"_id": ObjectId(_id)})
+ return {"_id": _id}, 200
def update(self, _id, datas):
db = MongoDB(self.app)
mongo = db.connection()
- error = {}
if not _id:
- error = {"msg": "UObject _id is required", "code": 400}
+ error = {"msg": "UObject _id is required"}
+ return error, 400
elif len(list(mongo.db.uobjects.find({"_id": ObjectId(_id)}))) == 0:
error = {
- "msg": "UObject {} does not exist.".format(_id),
- "code": 404
+ "msg": "UObject {} does not exist.".format(_id)
}
+ return error, 404
datas = dict(
("datas.{}".format(key), value) for (key, value) in datas.items()
)
- if not error:
- mongo.db.uobjects.update_one(
- {"_id": ObjectId(_id)}, {"$set": datas}
- )
- return {"msg": "UObject updated.", "code": 200}
- return error
+ mongo.db.uobjects.update_one(
+ {"_id": ObjectId(_id)}, {"$set": datas}
+ )
+ return {"msg": "UObject updated.", "_id": _id}
def eq(self, key, value):
db = MongoDB(self.app)
mongo = db.connection()
- datas = list(mongo.db.uobjects.find(
+ return list(mongo.db.uobjects.find(
{"datas.{}".format(key): {"$eq": value}}
))
- if datas:
- return {
- "msg": "UObjects found",
- "datas": datas,
- "code": 200
- }
- else:
- return {
- "msg": "UObjects not found",
- "datas": datas,
- "code": 404
- }
def reset(self):
db = MongoDB(self.app)