Last active
September 21, 2020 07:28
-
-
Save jamescalam/cca7a08699ed892457be55a69c5c52a8 to your computer and use it in GitHub Desktop.
Example snippet showing additional logic added to Flask API POST method.
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
... | |
# 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) | |
data.to_csv('users.csv', index=False) # save back to CSV | |
return {'data': data.to_dict()}, 200 # return data with 200 OK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment