How best can I apply flask_http_middleware with flask application factory that actually works flawlessly?

80 views Asked by At

This is my factory function, where I've tried setting up flask_http_middleware as per the documentation. I'm using flask smorest and trying to implement addition of JWT based authentication middleware fastapi way in flask. I've been getting a strange error shown below. Any leads to the solution or the best alternative?

def create_app(config_name):
    """Application factory function to start the application"""
    myapp = Flask(__name__)
    myapp.config.from_object(config[config_name])
    config[config_name].init_app(myapp)
    orders_api = Api(myapp)

    # Registering url routes
    orders_api.register_blueprint(notifications)
    orders_api.register_blueprint(reviews)
    orders_api.register_blueprint(items)
    orders_api.register_blueprint(alerts)

    # Updating api specifications
    spec = APISpec(title=myapp.config["API_TITLE"], version=myapp.config["API_VERSION"], openapi_version=myapp.config["OPENAPI_VERSION"])

    orders_api.spec = spec

    myapp.wsgi_app = MiddlewareManager(myapp)
    myapp.wsgi_app.add_middleware(AuthorizeRequestMiddleware)
    CORS(myapp)

    return myapp

This is the error I'm getting when I try to call a request.

Press CTRL+C to quit
[2023-05-13 21:44:46,655] ERROR in app: Exception on /notification/1 [GET]
Traceback (most recent call last):
  File "/home/ado/.local/share/virtualenvs/ordering-vu1A4Yrb/lib/python3.11/site-packages/flask_http_middleware/manager.py", line 96, in __call__
    self.app.try_trigger_before_first_request_functions()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Flask' object has no attribute 'try_trigger_before_first_request_functions'
127.0.0.1 - - [13/May/2023 21:44:46] "GET /notification/1 HTTP/1.1" 500 -
Error on request:
Traceback (most recent call last):
  File "/home/ado/.local/share/virtualenvs/ordering-vu1A4Yrb/lib/python3.11/site-packages/werkzeug/serving.py", line 364, in run_wsgi
    execute(self.server.app)
  File "/home/ado/.local/share/virtualenvs/ordering-vu1A4Yrb/lib/python3.11/site-packages/werkzeug/serving.py", line 325, in execute
    application_iter = app(environ, start_response)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ado/.local/share/virtualenvs/ordering-vu1A4Yrb/lib/python3.11/site-packages/flask/app.py", line 2213, in __call__
    return self.wsgi_app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ado/.local/share/virtualenvs/ordering-vu1A4Yrb/lib/python3.11/site-packages/flask_http_middleware/manager.py", line 121, in __call__
    ctx.auto_pop(error)
AttributeError: 'RequestContext' object has no attribute 'auto_pop'
0

There are 0 answers