Skip to content

Commit

Permalink
Make /new_email support Idempotent-Key
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Oct 10, 2023
1 parent 532235e commit fcad5b8
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/mailadm/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
from mailadm.mailcow import MailcowError


def idempotent(handler):
"""Decorator making route support Idempotency-Key header."""
cache = {}

def idempotent_handler():
key = request.headers.get("Idempotency-Key")
if key and key in cache:
return cache[key]
result = handler()
if key:
cache[key] = result
return result


def create_app_from_db_path(db_path=None):
if db_path is None:
db_path = mailadm.db.get_db_path()
Expand All @@ -19,6 +33,7 @@ def create_app_from_db(db):
app.db = db

@app.route("/", methods=["POST"])
@idempotent()
def new_email():
token = request.args.get("t")
if token is None:
Expand Down

0 comments on commit fcad5b8

Please sign in to comment.