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 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"} user_model = api.model('UObject', { 'name': fields.String( required=True, description='Name of the uobject', example='Player' ), 'datas': fieldsDict( required=False, description='Datas of the uobject in JSON format', example={"key": "token"} ) }) @api.route('/objects', endpoint='objetcs') class Objects(Resource): def get(self): """ Get uobjects list """ uobjects = uobject.all() if not uobjects: return loads('{"msg": "No uobjects"}'), 404 return sanitize(uobjects), 200 @api.route("/objects/register") class Register(Resource): @api.expect(user_model) def post(self): """ Register new uobject """ args = request.get_json(force=True) status = uobject.register(args.get('name'), args.get('datas')) return {"msg": status['msg']}, status['code'] @api.route('/objects/') @api.doc(params={'name': 'Player'}) class Remove(Resource): def delete(self, name): """ Remove an uobject """ status = uobject.remove(name) return {"msg": status['msg']}, status['code']