From 1f28ebcbd0e4aaeb30b121cb19f28442e5e8db5d Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Dec 07 2020 12:31:38 +0000 Subject: Make get_request_data take into account the content-type header If the user has specified a content-type, we can assume that they really wanted to pass us a JSON blob and thus we can force the flask.request.get_json() method. This reduces the code duplication Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/api/__init__.py b/pagure/api/__init__.py index 6fcf915..bb2c67a 100644 --- a/pagure/api/__init__.py +++ b/pagure/api/__init__.py @@ -161,7 +161,13 @@ def get_authorized_api_project(session, repo, user=None, namespace=None): def get_request_data(): - return flask.request.form or flask.request.get_json() or {} + # Check if it's JSON or form data + if flask.request.headers.get("Content-Type") == "application/json": + # Set force to True to ignore the mimetype. Set silent so that None is + # returned if it's invalid JSON. + return flask.request.get_json(force=True, silent=True) or {} + else: + return flask.request.form or flask.request.get_json() or {} def api_login_required(acls=None, optional=False): diff --git a/pagure/api/project.py b/pagure/api/project.py index ed50f42..4eef249 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -1627,15 +1627,8 @@ def api_modify_project(repo, namespace=None): ) valid_keys = ["main_admin", "retain_access"] - # Check if it's JSON or form data - if flask.request.headers.get("Content-Type") == "application/json": - # Set force to True to ignore the mimetype. Set silent so that None is - # returned if it's invalid JSON. - args = flask.request.get_json(force=True, silent=True) or {} - retain_access = args.get("retain_access", False) - else: - args = get_request_data() - retain_access = args.get("retain_access", "").lower() in ["true", "1"] + args = get_request_data() + retain_access = args.get("retain_access", "").lower() in ["true", "1"] if not args: raise pagure.exceptions.APIError(400, error_code=APIERROR.EINVALIDREQ) @@ -1847,14 +1840,7 @@ def api_generate_acls(repo, username=None, namespace=None): project = _get_repo(repo, username, namespace) _check_token(project, project_token=False) - # Check if it's JSON or form data - if flask.request.headers.get("Content-Type") == "application/json": - # Set force to True to ignore the mimetype. Set silent so that None is - # returned if it's invalid JSON. - json = flask.request.get_json(force=True, silent=True) or {} - wait = json.get("wait", False) - else: - wait = pagure.utils.is_true(get_request_data().get("wait")) + wait = pagure.utils.is_true(get_request_data().get("wait")) try: task = pagure.lib.git.generate_gitolite_acls(project=project) @@ -1926,13 +1912,7 @@ def api_new_branch(repo, username=None, namespace=None): project = _get_repo(repo, username, namespace) _check_token(project, project_token=False) - # Check if it's JSON or form data - if flask.request.headers.get("Content-Type") == "application/json": - # Set force to True to ignore the mimetype. Set silent so that None is - # returned if it's invalid JSON. - args = flask.request.get_json(force=True, silent=True) or {} - else: - args = get_request_data() + args = get_request_data() branch = args.get("branch") from_branch = args.get("from_branch")