mirror of
https://github.com/Xevion/trivia.git
synced 2025-12-10 00:08:50 -06:00
remove changed route from API in favor of If-Modified-Since header-based implementation in refresh route
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user