karthik vajja
karthik vajja

Reputation: 1

Running Flask app with Connexion 3.1.0 with synchronous api calls

When migrating from connexion2 to connexion3 setting CORS with middleware / using flask-cors, Options preflight is not dealt right, it either throws Unauthorized / Method not allowed / random error like ERROR:root:error(0,'').

I am using Cors connections with credentials set as True, as multiple UI applications hosted in different domains connect with auth0 authenticaiton to the api flask server.

Below is the code i am running:

app = connexion.FlaskApp(__name__, specification_dir='config/swagger/')
CORS(app.app, supports_credentials=True)
app.add_middleware(
      CORSMiddleware,
      position=MiddlewarePosition.BEFORE_EXCEPTION,
      allow_origins=["*"],
      allow_credentials=True,
      allow_methods=["*"],
      allow_headers=["*"],
)

@app.app.after_request
def cache_busting_headers(response):
    response.headers.extend({
        'Last-Modified': datetime.now(),
        'Cache-Control': cache_control_header,
        'Pragma': 'no-cache',
        'Expires': '0'
    })
    return response

def run():
    uvicorn.run(app, port=int(os.getenv('PORT', 8080)), log_level='trace')


if __name__ == '__main__':
    run()

API errors occurs randomly: ERROR:root:error (0, '') ERROR:root:error Packet sequence number wrong - got 102 expected 3

If middleware addition is removed then 405 Method not allowed in Options call.

requirements Txt: connexion[flask,uvicorn,swagger-ui]==3.1.0 Flask-Cors==4.0.1

Can anyone guide me on how to resolve ASGI server issue of blocking the requests when multpile requests are triggered together.

Note: Same domain calls works fine including swagger exposed for the server.

First call is successfull and then second call throws error like below:ERROR:root:error (0, '')
ERROR:root:error (0, '')
INFO:     127.0.0.1:59311 - "GET /api/v1/me HTTP/1.1" 401 Unauthorized
INFO:     127.0.0.1:59312 - "GET /api/v1/me/offers HTTP/1.1" 401 Unauthorized

ASGI server unable to handle the multiple api requests that are triggered from UI. Flask app functionalities are rendered useless with connexion3 upgrade.

Upvotes: 0

Views: 197

Answers (0)

Related Questions