diff options
author | neodarz <neodarz@neodarz.net> | 2019-07-08 15:46:39 +0200 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2019-07-08 15:50:24 +0200 |
commit | bcde164af6aeccc13ee77c0b5e505399994095d3 (patch) | |
tree | bb4bc6b2150d06f1cca935a8af637f95094dad3e /umosapi | |
download | umosapi-bcde164af6aeccc13ee77c0b5e505399994095d3.tar.xz umosapi-bcde164af6aeccc13ee77c0b5e505399994095d3.zip |
Initial commit
All code are mainly copied then adjusted from
https://git.neodarz.net/pro/liberationCenter.git/
Diffstat (limited to 'umosapi')
-rw-r--r-- | umosapi/__init__.py | 36 | ||||
-rw-r--r-- | umosapi/api.py | 68 | ||||
-rw-r--r-- | umosapi/app_db/__init__.py | 0 | ||||
-rw-r--r-- | umosapi/app_db/db.py | 31 | ||||
-rw-r--r-- | umosapi/app_db/uobject.py | 48 | ||||
-rw-r--r-- | umosapi/utils.py | 9 |
6 files changed, 192 insertions, 0 deletions
diff --git a/umosapi/__init__.py b/umosapi/__init__.py new file mode 100644 index 0000000..ac93e1d --- /dev/null +++ b/umosapi/__init__.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +""" + UMoSApi + ~~~~~~~ + + Unity Mongo Save Api is a simple API for save Unity object in Mongo + database. + + :copyright: (c) 2019 by neodarz. + :licence: GPLv3, see LICENSE for more details. +""" +import os + +from flask import Flask, render_template + +def create_app(test_config=None): + app = Flask(__name__, instance_relative_config=True) + app.config.from_mapping( + SECRET_KEY='dev', + MONGO_URI="mongodb://localhost:27017/umosapi", + ) + + if test_config is None: + app.config.from_pyfile('config.py', silent=True) + else: + app.config.from_mapping(test_config) + + try: + os.makedirs(app.instance_path) + except OSError: + pass + + from . import api + app.register_blueprint(api.bp, url_prefix='/api') + + return app diff --git a/umosapi/api.py b/umosapi/api.py new file mode 100644 index 0000000..0b48079 --- /dev/null +++ b/umosapi/api.py @@ -0,0 +1,68 @@ +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) + +user_model = api.model('UObject', { + 'name': fields.String( + required=True, + description='Name of the uobject', + example='Player' + ) +}) + + +@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')) + + return loads('{"msg": "'+status['msg']+'"}'), status['code'] + + +@api.route('/objects/<name>') +@api.doc(params={'name': 'Player'}) +class Remove(Resource): + def delete(self, name): + """ Remove an uobject """ + status = uobject.remove(name) + + return loads('{"msg": "'+status['msg']+'"}'), status['code'] diff --git a/umosapi/app_db/__init__.py b/umosapi/app_db/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/umosapi/app_db/__init__.py diff --git a/umosapi/app_db/db.py b/umosapi/app_db/db.py new file mode 100644 index 0000000..70b8f7c --- /dev/null +++ b/umosapi/app_db/db.py @@ -0,0 +1,31 @@ +from flask import current_app, _app_ctx_stack +from flask_pymongo import PyMongo + + +class MongoDB(object): + def __init__(self, app=None): + self.app = app + if app is not None: + self.init_app(app) + + def init_app(self, app): + app.teardown_appcontext(self.teardown) + + def connect(self): + return PyMongo(self.app, current_app.config['MONGO_URI']) + + def teardown(self, exception): + ctx = _app_ctx_stack.top + if hasattr(ctx, 'mongo_db'): + ctx.mongo_db.cx.close() + + def connection(self): + ctx = _app_ctx_stack.top + if ctx is not None: + if not hasattr(ctx, 'mongodb'): + ctx.mongo_db = self.connect() + return ctx.mongo_db + + def set_up(self): + mongo = PyMongo(self.app) + mongo.cx.drop_database("umosapi") diff --git a/umosapi/app_db/uobject.py b/umosapi/app_db/uobject.py new file mode 100644 index 0000000..175ac52 --- /dev/null +++ b/umosapi/app_db/uobject.py @@ -0,0 +1,48 @@ +from flask import current_app + +from .db import MongoDB + + +class UObject(object): + def __init__(self, app=None): + self.app = app + + def all(self): + db = MongoDB(self.app) + mongo = db.connection() + return list(mongo.db.objects.find({})) + + def register(self, name): + db = MongoDB(self.app) + mongo = db.connection() + error = {} + + if not name: + error = {'msg': 'Object name is required.', 'code': 400} + elif len(list(mongo.db.objects.find({"name": name}))) > 0: + error = { + 'msg': 'Object {} is already registered.'.format(name), + 'code': 409 + } + if not error: + mongo.db.objects.insert({"name": name}) + return {'msg': 'Object {} added.'.format(name), 'code': 201} + return error + + def remove(self, name): + db = MongoDB(self.app) + mongo = db.connection() + error = {} + + if not name: + error = {'msg': 'Object name is required', 'code': 400} + elif len(list(mongo.db.objects.find({"name": name}))) == 0: + error = { + 'msg': "User {} not exist. So it's good".format(name), + 'code': 404 + } + + if not error: + mongo.db.objects.remove({'name': name}) + return {'msg': 'Object {} deleted'.format(name), 'code': 200} + return error diff --git a/umosapi/utils.py b/umosapi/utils.py new file mode 100644 index 0000000..bd5f655 --- /dev/null +++ b/umosapi/utils.py @@ -0,0 +1,9 @@ +from bson.json_util import dumps +from json import loads + + +def sanitize(uobjects): + """ + Simply transform a list of uobject into a list of uobject stringised + """ + return loads(dumps(uobjects)) |