-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
92 lines (80 loc) · 2.15 KB
/
app.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
86
87
88
89
90
91
92
from flask import Flask
from flask_restful import Api, Resource, reqparse
app = Flask(__name__)
api = Api(app)
# some sample users
users = [
{
"name": "Nicholas",
"age": 42,
"occupation": "Network Engineer"
},
{
"name": "Elvin",
"age": 32,
"occupation": "Doctor"
},
{
"name": "Jass",
"age": 22,
"occupation": "Web Developer"
}
]
class User(Resource):
'''
GET - retrieves information about a user.
return the user and 200 status code if found, 404 otherwise
'''
def get(self, name):
for user in users:
if(name == user["name"]):
return user, 200
return "User not found", 404
'''
POST - adds a user to our database, returns the user and
201 status code
'''
def post(self, name):
parser = reqparse.RequestParser()
parser.add_argument("age")
parser.add_argument("occupation")
args = parser.parse_args()
for user in users:
if(name == user["name"]):
return "User with name {} already exists".format(name), 400
user = {
"name": name,
"age": args["age"],
"occupation": args["occupation"]
}
users.append(user)
return user, 201
'''
PUT - updates a user and returns the user info and a 201 status code
'''
def put(self, name):
parser = reqparse.RequestParser()
parser.add_argument("age")
parser.add_argument("occupation")
args = parser.parse_args()
'''
Implement this! A put request simply updates a user
'''
users.append(user)
return user, 201
'''
Deletes a user and returns a 200 status code or 404 if user
not found
'''
def delete(self, name):
global users
'''
Implement this! This deletes a user from our app
'''
return "{} is deleted.".format(name), 200
'''
Our endpoint, to access a uer go to 127.0.0.1/user/<string:name>
ex - 127.0.0.1/user/jas
'''
api.add_resource(User, "/user/<string:name>")
app.run(debug=True)