From 5bcc1ed9df4e7a6b8086cac3f25a466aee3bebcd Mon Sep 17 00:00:00 2001 From: neodarz Date: Sat, 16 Jun 2018 17:52:31 +0200 Subject: Add first structure of the application --- src/lb_app/api.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/lb_app/api.py (limited to 'src/lb_app/api.py') 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 -- cgit v1.2.1