From 4a3b5a9fda09c417d4638cbb785bd9bbc46850b0 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Wed, 18 Jan 2023 16:34:07 +0100 Subject: [PATCH] python-flask: Add compatibility with Flask 2 --- python-flask/app.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/python-flask/app.py b/python-flask/app.py index fdfe39b..c51ef04 100644 --- a/python-flask/app.py +++ b/python-flask/app.py @@ -20,21 +20,19 @@ # with Crate these terms will supersede the license and you may use the # software solely pursuant to the terms of the relevant commercial agreement. -import json import uuid import base64 import hashlib +import typing as t from datetime import datetime from tempfile import TemporaryFile -from urllib.request import urlopen, Request -from urllib.error import HTTPError, URLError - from crate.client import connect from flask import ( Flask, + Request, g as app_globals, make_response, jsonify @@ -367,6 +365,29 @@ def not_found(error): return jsonify({'error': 'Not found'}), 404 +def on_json_loading_failed(self, e: t.Optional[ValueError]) -> t.Any: + """ + Respond with custom error message on erroneous JSON requests. + + Otherwise, Flask would catch the error and respond with:: + + { + "message": "Did not attempt to load JSON data because the " + "request Content-Type was not 'application/json'." + } + + There may be empty POST/PUT requests, or requests without Content-Type + header, all of them effectively not having a valid JSON payload. + """ + + # Return an empty data container, containing no parsed arguments. + return self.namespace_class() + + +# Monkeypatch the original Flask method. +Request.on_json_loading_failed = on_json_loading_failed + + def run(): api = Api(app) api.add_resource(PostList, '/posts', endpoint='posts')