From 0f35b91e8e1d702e6b37f6819519e0cdd2500c29 Mon Sep 17 00:00:00 2001 From: Corder Guy Date: Tue, 17 Apr 2018 14:24:04 -0500 Subject: [PATCH] Add endpoint for get_top_N --- README.md | 9 +++++---- mocha_server.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 07db75b..1335d4b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # API endpoints: -| Endpoint | Return | -| ------------------------ | ----------------------- | -| `/mocha/users/{user_id}` | row with the given user | -| `/mocha/users/*` | all rows | +| Endpoint | Return | +| ------------------------ | ------------------------------ | +| `/mocha/users/{user_id}` | row with the given user | +| `/mocha/users/*` | all rows | +| `/mocha/top/{n}` | user with the n highest scores | diff --git a/mocha_server.py b/mocha_server.py index bc7729d..fbcd6ec 100755 --- a/mocha_server.py +++ b/mocha_server.py @@ -104,6 +104,15 @@ def fetch_user(user_id): return output +def fetch_top_n(n): + output = get_top_N('score', True, n) + + if output == '[]': + output = None + + return output + + def process_request(uri): """ Handles the API endpoint. @@ -118,6 +127,8 @@ def process_request(uri): if parts[1] == 'users': output = fetch_user(parts[2]) + elif parts[1] == 'top': + output = fetch_top_n(parts[2]) return output