aboutsummaryrefslogtreecommitdiff
path: root/src/lb_app/api.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/lb_app/api.py')
-rw-r--r--src/lb_app/api.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/lb_app/api.py b/src/lb_app/api.py
new file mode 100644
index 0000000..6dfe2e8
--- /dev/null
+++ b/src/lb_app/api.py
@@ -0,0 +1,43 @@
+from flask import (Flask, Blueprint, g, redirect, request, session, jsonify, url_for)
+from flask_restplus import Resource, Api, fields
+
+from bson.json_util import dumps
+from json import loads
+
+from .app_db.user import User
+
+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)
+
+user = User(app)
+
+user_model = api.model('User', {
+ 'username': fields.String(required=True, description='Username of account', example='Jean')
+})
+
+@api.route('/users', endpoint='users')
+class Users(Resource):
+ def get(self):
+ """ Get user list """
+ return loads(dumps(user.all()))
+
+
+@api.route('/user/register')
+class Register(Resource):
+ @api.expect(user_model)
+ def post(self):
+ """ Register new user """
+ args = request.get_json(force=True)
+ status = user.register(args.get('username'))
+
+ if status is None:
+ return loads('{"msg": "done"}'), 201
+ return loads('{"msg": "'+status+'"}'), 200