Created
December 24, 2018 00:02
-
-
Save thechaudharysab/2672fe543c67a128eb875ff8a99e9a2d to your computer and use it in GitHub Desktop.
Get, Post, Update & Delete using Flask
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
class User(Resource): | |
def get(self, name): | |
for user in users: | |
if(name == user["name"]): | |
return user, 200 | |
return "User not found", 404 | |
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 | |
def put(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"]): | |
user["age"] = args["age"] | |
user["occupation"] = args["occupation"] | |
return user, 200 | |
user = { | |
"name": name, | |
"age": args["age"], | |
"occupation": args["occupation"] | |
} | |
user.ppend(user) | |
return user, 201 | |
def delete(self, name): | |
global users | |
users = [user for user in users if user["name"] != name] | |
return "{} is deleted.".format(name),200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment