Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
joyc committed Jul 21, 2019
0 parents commit dc09611
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
.env
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
flask api test

use flask-restful

91 changes: 91 additions & 0 deletions app.py
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)
71 changes: 71 additions & 0 deletions raw_main.py
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)
17 changes: 17 additions & 0 deletions requirements.txt
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

0 comments on commit dc09611

Please sign in to comment.