From 395221288e54900088fe4c81d7a685e721d4316c Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 19 Jun 2020 19:08:04 -0500 Subject: [PATCH] remove changed route from API in favor of If-Modified-Since header-based implementation in refresh route --- trivia/api.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/trivia/api.py b/trivia/api.py index 949cfc4..3c02115 100644 --- a/trivia/api.py +++ b/trivia/api.py @@ -4,26 +4,11 @@ api.py Handles backend routes assisting """ import json +import time -from flask import request, jsonify, make_response +from flask import request, make_response from trivia import app -from trivia.utils import lastModified - - -@app.route("/api/changed") -@app.route("/api/changed/") -def changed(): - """ - A simple substitute for the 304 Not Modified HTTP return. - - :return True if data has changed since last - - TODO: Remove this function once a proper 304 Not Modified implementation is found for client side. - """ - from trivia.utils import lastChange - time = int(request.args.get('last') or lastModified()) - return jsonify(lastChange < time) @app.route("/api/refresh") @@ -32,8 +17,23 @@ def refresh(): """ Used for refreshing client-side table data. Returns a JSON response with all data necessary to build the table. """ - from trivia.utils import teams + from trivia.utils import teams, lastChange + + # Create response using namedtuples r = make_response(json.dumps([team._asdict() for team in teams])) - print(r) r.mimetype = 'application/json' - return r + + status_code = 200 + # Try to handle If-Modified-Since header properly (304 Not Modified + try: + if request.headers['If-Modified-Since']: + # Acquire epoch time from header + epoch = time.mktime(time.strptime(request.headers['If-Modified-Since'], "%a, %d %b %Y %I:%M:%S %Z")) + if epoch < lastChange: + status_code = 304 + except KeyError: + pass # Header was not supplied. Ignore. + except ValueError: + print('If-Modified-Since Header could not be parsed.') # Header could not be parsed. + + return r, status_code