Skip to content

Commit

Permalink
⚡ perf: Added 'If-Modified-Since' header to optimize breaches route c…
Browse files Browse the repository at this point in the history
…alls
  • Loading branch information
DevaOnBreaches committed Feb 2, 2024
1 parent 3551a51 commit 820554d
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ def not_found(error):
return make_response(jsonify({"Error": "Not found"}), 404)


# @XON.errorhandler(304)
# def nothing_changed(error):
# """Returns response for 304"""
# return make_response(jsonify({"Error": "Nothing has changed"}), 304)


@XON.errorhandler(429)
def ratelimit_handler(error):
"""Returns response for 429"""
Expand Down Expand Up @@ -3432,6 +3438,19 @@ def get_xposed_breaches():
query = client.query(kind="xon_breaches")
query.add_filter("domain", "=", domain)

query.order = ["-timestamp"]
latest_entity = list(query.fetch(limit=1))

if latest_entity:
latest_timestamp = latest_entity[0]["timestamp"]
if_modified_since_str = request.headers.get("If-Modified-Since")
if if_modified_since_str:
if_modified_since = datetime.datetime.strptime(
if_modified_since_str, "%a, %d %b %Y %H:%M:%S GMT"
)
if latest_timestamp.replace(tzinfo=None) <= if_modified_since:
return make_response("", 304)

entities = query.fetch()

fields = [
Expand Down

0 comments on commit 820554d

Please sign in to comment.