diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a5c8936 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +.env \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ad07f12 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +flask api test + +use flask-restful + diff --git a/app.py b/app.py new file mode 100644 index 0000000..1de501f --- /dev/null +++ b/app.py @@ -0,0 +1,91 @@ +from flask import Flask, request +from flask_restful import Resource, Api, reqparse + +app = Flask(__name__) +api = Api(app) + +user_list = [] + +def min_length_str(min_length): + def validate(s): + if s is None: + raise Exception('password required') + if not isinstance(s, (int, str)): + raise Exception('password format error') + s = str(s) + if len(s) >= min_length: + return s + raise Exception("String must be at least %i characters long" % min_length) + return validate + +class HelloWorld(Resource): + def get(self): + return 'hello world' + +class User(Resource): + + parser = reqparse.RequestParser() + parser.add_argument( + 'password', type=min_length_str(5), required=True, + help='{error_msg}' + ) + + def get(self, username): + """ + get user detail infomation + """ + for user in user_list: + if user["username"] == username: + return user + return {'message': 'user not found'}, 404 + + def post(self, username): + """ + create a user + """ + data = User.parser.parse_args() + user = { + 'username': username, + 'password': data.get('password') + } + user_list.append(user) + return user, 201 + + def delete(self, username): + """delete user""" + user_find = None + for user in user_list: + if user['username'] == username: + user_find = user + if user_find: + user_list.remove(user_find) + return user_find + else: + return {'message': 'user not found'} + + def put(self, username): + """update user""" + user_find = None + for user in user_list: + if user['username'] == username: + user_find = user + if user_find: + data = User.parser.parse_args() + user_list.remove(user_find) + user_find['password'] = data['password'] + user_list.append(user_find) + return user_find + else: + return {'message': 'user not found'} + +class UserList(Resource): + + def get(self): + return user_list + +api.add_resource(HelloWorld, '/') +api.add_resource(User, '/user/') +api.add_resource(UserList, '/users') + +if __name__ == '__main__': + app.run(debug=True) diff --git a/raw_main.py b/raw_main.py new file mode 100644 index 0000000..d7a5ce7 --- /dev/null +++ b/raw_main.py @@ -0,0 +1,71 @@ +from flask import Flask, jsonify, request + +app = Flask(__name__) + +user_list = [ + { + 'username': 'abc', + 'password': 'abc' + }, + { + 'username': '123', + 'password': '123' + } +] + +@app.route('/') +def helloworld(): + return "hello world" + +@app.route('/users', methods=['GET']) +def get_user(): + return jsonify(user_list) + +@app.route('/user', methods=['POST']) +def create_user(): + user = request.get_json() + user_check = list( + filter( + lambda x: user.get('username') == x['username'], + user_list + ) + ) + if not user_check: + user_list.append(user) + return jsonify({ + 'message': 'user created' + }) + else: + return jsonify({ + 'message': 'user exist' + }) + +@app.route('/user/', methods=['DELETE', 'PUT']) +def delete_user(username): + user_find = None + for user in user_list: + if user['username'] == username: + user_find = user + if not user_find: + return jsonify({ + 'message': 'user not found' + }) + if request.method == 'DELETE': + user_list.remove(user_find) + return jsonify({ + "message": "user deleted" + }) + elif request.method == 'PUT': + # new_passwd = {"password": "xxxxx"} + new_password = request.get_json() + user_list.remove(user_find) + user_list.append({ + 'username': username, + 'password': new_password['password'] + }) + return jsonify({ + 'message': 'user password updated' + }) + +if __name__ == "__main__": + app.run(debug=True) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..59de748 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,17 @@ +aniso8601==7.0.0 +astroid==2.2.5 +Click==7.0 +Flask==1.1.1 +Flask-RESTful==0.3.7 +isort==4.3.21 +itsdangerous==1.1.0 +Jinja2==2.10.1 +lazy-object-proxy==1.4.1 +MarkupSafe==1.1.1 +mccabe==0.6.1 +pylint==2.3.1 +pytz==2019.1 +six==1.12.0 +typed-ast==1.4.0 +Werkzeug==0.15.5 +wrapt==1.11.2