Skip to content

Xuan Li Leong #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 163 additions & 19 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 76 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@

app = Flask(__name__)

ENDPOINT_MAIN = "/"
ENDPOINT_MIRROR = "/mirror"
ENDPOINT_USERS = "/users"
ENDPOINT_USERS_BY_ID = "/users/<int:user_id>"

TYPE_USER = "users"


def create_response(
data: dict = None, status: int = 200, message: str = ""
) -> Tuple[Response, int]:
"""Wraps response in a consistent format throughout the API.

Format inspired by https://medium.com/@shazow/how-i-design-json-api-responses-71900f00f2db
Modifications included:
- make success a boolean since there's only 2 values
Expand Down Expand Up @@ -41,7 +48,7 @@ def create_response(
"""


@app.route("/")
@app.route(ENDPOINT_MAIN)
def hello_world():
return create_response({"content": "hello world!"})

Expand All @@ -52,7 +59,73 @@ def mirror(name):
return create_response(data)


# TODO: Implement the rest of the API here!
@app.route(ENDPOINT_USERS, methods=["GET"])
def get_users():
"""
Query parameters
----------------
team: Get list of users belonging to that team. If no users belong to that team,
responds with an empty list.
"""
team = request.args.get("team")
if team is None:
return create_response({"users": db.get(TYPE_USER)})
else:
return create_response({"users": db.getByTeam(TYPE_USER, team)})


@app.route(ENDPOINT_USERS_BY_ID, methods=["GET"])
def get_user_by_id(user_id):
user = db.getById(TYPE_USER, user_id)
if user is None:
return create_response(
status=404, message="Could not find a user with id {}".format(user_id)
)
return create_response({"user": user})


@app.route(ENDPOINT_USERS, methods=["POST"])
def create_user():
data = request.get_json()
if data is None:
return create_response(status=422, message="No JSON body provided")

required_keys = ["name", "age", "team"]
missing_keys = [k for k in required_keys if k not in data]
if len(missing_keys) > 0:
msg = "Body lacks the following data: {}".format(", ".join(missing_keys))
return create_response(status=422, message=msg)

user_data = {k: data[k] for k in required_keys}
created_user = db.create(TYPE_USER, user_data)
return create_response(data={"user": created_user}, status=201)


@app.route(ENDPOINT_USERS_BY_ID, methods=["PUT"])
def update_user_by_id(user_id):
"""
Only name, age and team can be updated.
"""
data = request.get_json()
allowed_keys = ["name", "age", "team"]
user_data = {k: data[k] for k in allowed_keys if k in data}
updated_user = db.updateById(TYPE_USER, user_id, user_data)
if updated_user is None:
return create_response(
status=404, message="Could not find a user with id {}".format(user_id)
)
return create_response({"user": updated_user})


@app.route(ENDPOINT_USERS_BY_ID, methods=["DELETE"])
def delete_user_by_id(user_id):
if db.getById(TYPE_USER, user_id) is None:
return create_response(
status=404, message="Could not find a user with id {}".format(user_id)
)
db.deleteById(TYPE_USER, user_id)
return create_response(message="Deleted user with id {}".format(user_id))


"""
~~~~~~~~~~~~ END API ~~~~~~~~~~~~
Expand Down
4 changes: 4 additions & 0 deletions mockdb/mockdb_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def getById(type, id):
return next((i for i in get(type) if i["id"] == id), None)


def getByTeam(type, team):
return [i for i in get(type) if i["team"] == team]


def create(type, payload):
last_id = max([i["id"] for i in get(type)])
new_id = last_id + 1
Expand Down
Loading