-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
85 lines (73 loc) · 2.42 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from flask import Flask
from flask_restful import Api, Resource, reqparse
import hashlib
app = Flask(__name__)
api = Api(app)
users = [
{
'id': '111',
'username': 'guest',
'password': 'qwertyuiop',
'email': '[email protected]',
'gallons': '5',
'threshold': '10',
'numberOfPeople': 4
}
]
def encrypt_string(hash_string):
sha_signature = \
hashlib.sha256(hash_string.encode()).hexdigest()
return sha_signature
class User(Resource):
def get(self, userid):
for user in users:
if(userid == user['id']):
return user, 200
elif(userid == 'all'):
return users, 200
return 'User not found', 404
def post(self, userid):
parser = reqparse.RequestParser()
parser.add_argument('password')
parser.add_argument('email')
parser.add_argument('gallons')
parser.add_argument('threshold')
parser.add_argument('numberOfPeople')
args = parser.parse_args()
for user in users:
if(userid == user['username']):
return 'Username is taken', 400
user = {
'id': (args['password'] + userid),
'username': userid,
'password': args['password'],
'email': args['email'],
'gallons': args['gallons'],
'threshold': args['threshold'],
'numberOfPeople': args['numberOfPeople']
}
users.append(user)
return user, 201
def put(self, userid):
parser = reqparse.RequestParser()
parser.add_argument('password')
parser.add_argument('email')
parser.add_argument('gallons')
parser.add_argument('threshold')
args = parser.parse_args()
user = {
'id': (args['password'] + userid),
'username': userid,
'password': args['password'],
'email': args['email'],
'gallons': args['gallons'],
'threshold': args['threshold']
}
for i in range(len(users)):
if(userid == users[i]['username']):
users[i] = user
return user, 201
users.append(user)
return user, 201
api.add_resource(User, '/user/<string:userid>')
app.run(debug=True)