-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
119 lines (92 loc) · 3.61 KB
/
app.py
File metadata and controls
119 lines (92 loc) · 3.61 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python3
from flask import Flask
from flask_restful import Resource, Api, reqparse
import pandas as pd
import ast
app = Flask(__name__)
api = Api(app)
class Users(Resource):
#methods go here
def get(self):
data = pd.read_csv('users.csv') #read csv
data = data.to_dict() #conert dataframe to dictionary
return {'data':data}, 200
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('userId', required=True)
parser.add_argument('name', required=True)
parser.add_argument('city', required=True)
args = parser.parse_args()
# read our CSV
data = pd.read_csv('users.csv')
if args['userId'] in list(data['userId']):
return {
'message': f"'{args['userId']}' already exists."
}, 401
else:
# create new dataframe containing new values
new_data = pd.DataFrame({
'userId': args['userId'],
'name': args['name'],
'city': args['city'],
'locations': [[]]
})
# add the newly provided values
data = data.append(new_data, ignore_index=True)
# save back to csv
data.to_csv('users.csv', index=False) #saves the data
return {'data': data.to_dict()}, 200
def put(self):
parser = reqparse.RequestParser() #initialize
parser.add_argument('userId', required=True) # add args
parser.add_argument('location', required=True)
args = parser.parse_args() #parse arguments to dictionary
#read our csv
data = pd.read_csv('users.csv')
if args['userId'] in list(data['userId']):
# evaluate strings of lists to lists
data['locations'] = data['locations'].apply(
lambda x: ast.literal_eval(x)
)
# select our user
user_data = data[data['userId'] == args['userId']]
# update user's locations
user_data['locations'] = user_data['locations'].values[0]\
.append(args['location'])
# save back to csv
data.to_csv('users.csv', index=False)
#return data and 200 OK
return {'data': data.to_dict()}, 200
else:
# otherwise the userId does not exist
return {
'message': f"'{args['userId']}' user not found."
}, 404
def delete(self):
parser = reqparse.RequestParser() # initialize
parser.add_argument('userId', required=True) # add userId arg
args = parser.parse_args() # parse arguments to dictionary
# read our CSV
data = pd.read_csv('users.csv')
if args['userId'] in list(data['userId']):
# remove data entry matching given userId
data = data[data['userId'] != args['userId']]
# save back to CSV
data.to_csv('users.csv', index=False)
# return data and 200 OK
return {'data': data.to_dict()}, 200
else:
# otherwise we return 404 because userId does not exist
return {
'message': f"'{args['userId']}' user not found."
}, 404
class Locations(Resource):
#methods go here
def get(self):
data = pd.read_csv('locations.csv')
data = data.to_dict()
return {'data':data}, 200
api.add_resource(Users, '/users') # '/users' is our entry point for Users
api.add_resource(Locations, '/locations') # and '/locations' iis our entry point
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7000, debug=True)