Skip to content

Commit

Permalink
Merge pull request #83 from CSCI-GA-2820-FA23-001/refactor_restx
Browse files Browse the repository at this point in the history
Refactor using Flask-RESTX
  • Loading branch information
ZzSteven-Wang authored Dec 11, 2023
2 parents 263a86a + 775704e commit 5264bc8
Show file tree
Hide file tree
Showing 9 changed files with 542 additions and 264 deletions.
2 changes: 1 addition & 1 deletion features/products.feature
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Scenario: Delete a Product
And I press the "Delete" button
Then I should see the message "Product has been Deleted!"
When I press the "Retrieve" button
Then I should see the message "404 Not Found: Product with id"
Then I should see the message "not found"

Scenario: Update a Product
When I visit the "Home Page"
Expand Down
2 changes: 1 addition & 1 deletion features/steps/product_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def step_impl(context):
"""Delete all Products and load new ones"""

# List all of the products and delete them one by one
rest_endpoint = f"{context.base_url}/products"
rest_endpoint = f"{context.base_url}/api/products"
context.resp = requests.get(rest_endpoint)
assert context.resp.status_code == HTTP_200_OK
for products in context.resp.json():
Expand Down
3 changes: 3 additions & 0 deletions k8s/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ SQLAlchemy==2.0.0

# Runtime dependencies
Flask==2.3.2
flask-restx==1.1.0
cloudant==2.15.0
retry2==0.9.5
Flask-SQLAlchemy==3.0.2
# psycopg2==2.9.5
psycopg2-binary==2.9.5
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ SQLAlchemy==2.0.0

# Runtime dependencies
Flask==2.3.2
flask-restx==1.1.0
cloudant==2.15.0
retry2==0.9.5
Flask-SQLAlchemy==3.0.2
psycopg2==2.9.5
# psycopg2-binary==2.9.5
Expand Down
14 changes: 14 additions & 0 deletions service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@
"""
import sys
from flask import Flask
from flask_restx import Api
from service import config
from service.common import log_handlers

# Create Flask application
app = Flask(__name__)
app.config.from_object(config)
app.config["ERROR_404_HELP"] = False

api = Api(
app,
version="1.0.0",
title="Product Demo REST API Service",
description="This is a sample Product server.",
default="products",
default_label="Product operations",
doc="/apidocs", # default also could use doc='/apidocs/'
prefix="/api",
)


# Dependencies require we import the routes AFTER the Flask app is created
# pylint: disable=wrong-import-position, wrong-import-order, cyclic-import
Expand Down
122 changes: 61 additions & 61 deletions service/common/error_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,72 +45,72 @@ def bad_request(error):
)


@app.errorhandler(status.HTTP_404_NOT_FOUND)
def not_found(error):
"""Handles resources not found with 404_NOT_FOUND"""
message = str(error)
app.logger.warning(message)
return (
jsonify(status=status.HTTP_404_NOT_FOUND, error="Not Found", message=message),
status.HTTP_404_NOT_FOUND,
)
# @app.errorhandler(status.HTTP_404_NOT_FOUND)
# def not_found(error):
# """Handles resources not found with 404_NOT_FOUND"""
# message = str(error)
# app.logger.warning(message)
# return (
# jsonify(status=status.HTTP_404_NOT_FOUND, error="Not Found", message=message),
# status.HTTP_404_NOT_FOUND,
# )


@app.errorhandler(status.HTTP_405_METHOD_NOT_ALLOWED)
def method_not_supported(error):
"""Handles unsupported HTTP methods with 405_METHOD_NOT_SUPPORTED"""
message = str(error)
app.logger.warning(message)
return (
jsonify(
status=status.HTTP_405_METHOD_NOT_ALLOWED,
error="Method not Allowed",
message=message,
),
status.HTTP_405_METHOD_NOT_ALLOWED,
)
# @app.errorhandler(status.HTTP_405_METHOD_NOT_ALLOWED)
# def method_not_supported(error):
# """Handles unsupported HTTP methods with 405_METHOD_NOT_SUPPORTED"""
# message = str(error)
# app.logger.warning(message)
# return (
# jsonify(
# status=status.HTTP_405_METHOD_NOT_ALLOWED,
# error="Method not Allowed",
# message=message,
# ),
# status.HTTP_405_METHOD_NOT_ALLOWED,
# )


@app.errorhandler(status.HTTP_409_CONFLICT)
def resource_conflict(error):
"""Handles resource conflicts with HTTP_409_CONFLICT"""
message = str(error)
app.logger.warning(message)
return (
jsonify(
status=status.HTTP_409_CONFLICT,
error="Conflict",
message=message,
),
status.HTTP_409_CONFLICT,
)
# @app.errorhandler(status.HTTP_409_CONFLICT)
# def resource_conflict(error):
# """Handles resource conflicts with HTTP_409_CONFLICT"""
# message = str(error)
# app.logger.warning(message)
# return (
# jsonify(
# status=status.HTTP_409_CONFLICT,
# error="Conflict",
# message=message,
# ),
# status.HTTP_409_CONFLICT,
# )


@app.errorhandler(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
def mediatype_not_supported(error):
"""Handles unsupported media requests with 415_UNSUPPORTED_MEDIA_TYPE"""
message = str(error)
app.logger.warning(message)
return (
jsonify(
status=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
error="Unsupported media type",
message=message,
),
status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
)
# @app.errorhandler(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
# def mediatype_not_supported(error):
# """Handles unsupported media requests with 415_UNSUPPORTED_MEDIA_TYPE"""
# message = str(error)
# app.logger.warning(message)
# return (
# jsonify(
# status=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
# error="Unsupported media type",
# message=message,
# ),
# status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
# )


@app.errorhandler(status.HTTP_500_INTERNAL_SERVER_ERROR)
def internal_server_error(error):
"""Handles unexpected server error with 500_SERVER_ERROR"""
message = str(error)
app.logger.error(message)
return (
jsonify(
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
error="Internal Server Error",
message=message,
),
status.HTTP_500_INTERNAL_SERVER_ERROR,
)
# @app.errorhandler(status.HTTP_500_INTERNAL_SERVER_ERROR)
# def internal_server_error(error):
# """Handles unexpected server error with 500_SERVER_ERROR"""
# message = str(error)
# app.logger.error(message)
# return (
# jsonify(
# status=status.HTTP_500_INTERNAL_SERVER_ERROR,
# error="Internal Server Error",
# message=message,
# ),
# status.HTTP_500_INTERNAL_SERVER_ERROR,
# )
Loading

0 comments on commit 5264bc8

Please sign in to comment.