Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make /new_email support Idempotent-Key #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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