I am writing APIs in Flask RESTful framework. For every end point, I need to do a check which are same in structure but process inside are different. Here is the snippet.
def get_all_users():
users = [{"name":"John","dob":"12-10-1990"}, {"name":"David","dob":"25-03-1995"}, {"name":"Maria","dob":"30-02-1998"}]
return users
Endpoint 1:
class ClassOne(Resource):
def get(self):
# Get required info from body
res_body = request.json
name = res_body.get('name', None)
item_list = []
if name is None:
users = get_all_users()
for user in users:
item_list.append(user['name'])
return {"nameList":item_list}
else:
user = get_user_data(user_info=name)
if user:
name = user['name']
return {"Message":"User {} exists!".format(name)}
else:
return {"Error":"User does not exist"}
return "output based on the above process"
Endpoint 2:
class ClassTwo(Resource):
def get(self):
# Get required info from body
res_body = request.json
name = res_body.get('name', None)
item_list = []
if name is None:
users = get_all_users()
for user in users:
item_list.append(user['dob'])
return {"nameList":item_list}
else:
user = get_user_data(user_info=name)
if user:
dob = user['dob']
return {"Message":"User dob is {}".format(dob)}
else:
return {"Error":"User does not exist"}
return "output based on the above process"
What is the best way to write these codes by avoiding code duplication.
CodePudding user response:
Assuming Resource
doesn't have a .get()
, your endpoint classes could inherit from a common Process
class or whatever you want to call it, in addition to Resource
:
class Process:
def get(self, user_key):
res_body = request.json
name = res_body.get("name")
if name is None:
item_list = []
users = get_all_users()
for user in users:
item_list.append(user.get(user_key))
return {"nameList": item_list}
else:
user = get_user_data(user_info=name)
if user:
info = user.get(user_key)
return {"Message": f"User {user_key} is {info}"}
return {"Error": "User does not exist"}
class ClassOne(Resource, Process):
def __init__(self):
super().__init__()
def get(self):
return super().get(user_key="name")
class ClassTwo(Resource, Process):
def __init__(self):
super().__init__()
def get(self):
return super().get(user_key="dob")
Here's a quick demo of this inheritance structure:
In [1]: class Grandparent: # Resource
...: pass
...:
In [2]: class Parent: # Process
...: def get(self, x, y):
...: print(x, y)
...:
In [3]: class ChildA(Grandparent, Parent): # Endpoint 1
...: def get(self):
...: super().get(1, 2)
...:
In [4]: class ChildB(Grandparent, Parent): # Endpoint 2
...: def get(self):
...: super().get("hello", "world")
...:
In [5]: a = ChildA()
In [6]: b = ChildB()
In [7]: a.get()
1 2
In [8]: b.get()
hello world
Alternatively, you could simply have Process
inherit from Resource
and then have your endpoints inherit from Process
.