-
Notifications
You must be signed in to change notification settings - Fork 0
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
Move from flask_restful to flask_restx. Replace error dictionaries with abort #15
Conversation
This commit replaces all instances of returning error dictionaries with flask_restx.abort in multiple resources files. This refactoring enhances code readability and simplifies error handling. It also ensures that HTTP errors are handled in a more standardized way across the application.
The flask_restful import for Resource has been streamlined to flask_restx, and the initialization process in the PgsqlResource class has been extended with additional *args and **kwargs. This adjustment refines how our resources are initialized, and makes handling database connections in the kwargs argument more straightforward.
Flask_restful library is replaced with flask_restx in app.py file to leverage extended functionalities provided by flask_restx. This change aims to refine the resource initialization and streamline the handling of database connections.
The required Python package flask-restx has been added to the requirements file. Migrating from the flask_restful library to flask_restx, offers added features and a smoother experience with handling database connections and initializing resources.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just one question on how this changes the error response objects.
middleware/security.py
Outdated
@@ -80,25 +82,27 @@ def decorator(*args, **kwargs): | |||
if len(authorization_header) >= 2 and authorization_header[0] == "Bearer": | |||
api_key = request.headers["Authorization"].split(" ")[1] | |||
if api_key == "undefined": | |||
return {"message": "Please provide an API key"}, 400 | |||
abort(code=400, message="Please provide an API key") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maxachis Is this going to change the shape of the error objects we receive on the client?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joshuagraber Based on my tests, yes, but only somewhat, and seemingly not in a way that should interfere. The response.json
will now include a "http_status_code" key-value pair in addition to the "message" key-value pair.
So {'message': 'An unexpected error occurred'}
will become {'http_status_code': 500, 'message': 'An unexpected error occurred'}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem @maxachis thank you.
# Conflicts: # middleware/security.py # resources/PsycopgResource.py # resources/QuickSearch.py # resources/RefreshSession.py
…sage key directly.
…e_with_abort # Conflicts: # resources/QuickSearch.py
Fixes
Description
Api
usage inapp.py
has transitioned fromflask_restful
toflask_restx
, which supports automated swagger documentation and has more active support.Testing
Performance
Docs