-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dc09611
Showing
5 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
flask api test | ||
|
||
use flask-restful | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/<string:username>') | ||
api.add_resource(UserList, '/users') | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/<username>', 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |