Skip to content

Commit 5264bc8

Browse files
Merge pull request #83 from CSCI-GA-2820-FA23-001/refactor_restx
Refactor using Flask-RESTX
2 parents 263a86a + 775704e commit 5264bc8

File tree

9 files changed

+542
-264
lines changed

9 files changed

+542
-264
lines changed

features/products.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Scenario: Delete a Product
7171
And I press the "Delete" button
7272
Then I should see the message "Product has been Deleted!"
7373
When I press the "Retrieve" button
74-
Then I should see the message "404 Not Found: Product with id"
74+
Then I should see the message "not found"
7575

7676
Scenario: Update a Product
7777
When I visit the "Home Page"

features/steps/product_steps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def step_impl(context):
1212
"""Delete all Products and load new ones"""
1313

1414
# List all of the products and delete them one by one
15-
rest_endpoint = f"{context.base_url}/products"
15+
rest_endpoint = f"{context.base_url}/api/products"
1616
context.resp = requests.get(rest_endpoint)
1717
assert context.resp.status_code == HTTP_200_OK
1818
for products in context.resp.json():

k8s/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ SQLAlchemy==2.0.0
44

55
# Runtime dependencies
66
Flask==2.3.2
7+
flask-restx==1.1.0
8+
cloudant==2.15.0
9+
retry2==0.9.5
710
Flask-SQLAlchemy==3.0.2
811
# psycopg2==2.9.5
912
psycopg2-binary==2.9.5

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ SQLAlchemy==2.0.0
44

55
# Runtime dependencies
66
Flask==2.3.2
7+
flask-restx==1.1.0
8+
cloudant==2.15.0
9+
retry2==0.9.5
710
Flask-SQLAlchemy==3.0.2
811
psycopg2==2.9.5
912
# psycopg2-binary==2.9.5

service/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,26 @@
66
"""
77
import sys
88
from flask import Flask
9+
from flask_restx import Api
910
from service import config
1011
from service.common import log_handlers
1112

1213
# Create Flask application
1314
app = Flask(__name__)
1415
app.config.from_object(config)
16+
app.config["ERROR_404_HELP"] = False
17+
18+
api = Api(
19+
app,
20+
version="1.0.0",
21+
title="Product Demo REST API Service",
22+
description="This is a sample Product server.",
23+
default="products",
24+
default_label="Product operations",
25+
doc="/apidocs", # default also could use doc='/apidocs/'
26+
prefix="/api",
27+
)
28+
1529

1630
# Dependencies require we import the routes AFTER the Flask app is created
1731
# pylint: disable=wrong-import-position, wrong-import-order, cyclic-import

service/common/error_handlers.py

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -45,72 +45,72 @@ def bad_request(error):
4545
)
4646

4747

48-
@app.errorhandler(status.HTTP_404_NOT_FOUND)
49-
def not_found(error):
50-
"""Handles resources not found with 404_NOT_FOUND"""
51-
message = str(error)
52-
app.logger.warning(message)
53-
return (
54-
jsonify(status=status.HTTP_404_NOT_FOUND, error="Not Found", message=message),
55-
status.HTTP_404_NOT_FOUND,
56-
)
48+
# @app.errorhandler(status.HTTP_404_NOT_FOUND)
49+
# def not_found(error):
50+
# """Handles resources not found with 404_NOT_FOUND"""
51+
# message = str(error)
52+
# app.logger.warning(message)
53+
# return (
54+
# jsonify(status=status.HTTP_404_NOT_FOUND, error="Not Found", message=message),
55+
# status.HTTP_404_NOT_FOUND,
56+
# )
5757

5858

59-
@app.errorhandler(status.HTTP_405_METHOD_NOT_ALLOWED)
60-
def method_not_supported(error):
61-
"""Handles unsupported HTTP methods with 405_METHOD_NOT_SUPPORTED"""
62-
message = str(error)
63-
app.logger.warning(message)
64-
return (
65-
jsonify(
66-
status=status.HTTP_405_METHOD_NOT_ALLOWED,
67-
error="Method not Allowed",
68-
message=message,
69-
),
70-
status.HTTP_405_METHOD_NOT_ALLOWED,
71-
)
59+
# @app.errorhandler(status.HTTP_405_METHOD_NOT_ALLOWED)
60+
# def method_not_supported(error):
61+
# """Handles unsupported HTTP methods with 405_METHOD_NOT_SUPPORTED"""
62+
# message = str(error)
63+
# app.logger.warning(message)
64+
# return (
65+
# jsonify(
66+
# status=status.HTTP_405_METHOD_NOT_ALLOWED,
67+
# error="Method not Allowed",
68+
# message=message,
69+
# ),
70+
# status.HTTP_405_METHOD_NOT_ALLOWED,
71+
# )
7272

7373

74-
@app.errorhandler(status.HTTP_409_CONFLICT)
75-
def resource_conflict(error):
76-
"""Handles resource conflicts with HTTP_409_CONFLICT"""
77-
message = str(error)
78-
app.logger.warning(message)
79-
return (
80-
jsonify(
81-
status=status.HTTP_409_CONFLICT,
82-
error="Conflict",
83-
message=message,
84-
),
85-
status.HTTP_409_CONFLICT,
86-
)
74+
# @app.errorhandler(status.HTTP_409_CONFLICT)
75+
# def resource_conflict(error):
76+
# """Handles resource conflicts with HTTP_409_CONFLICT"""
77+
# message = str(error)
78+
# app.logger.warning(message)
79+
# return (
80+
# jsonify(
81+
# status=status.HTTP_409_CONFLICT,
82+
# error="Conflict",
83+
# message=message,
84+
# ),
85+
# status.HTTP_409_CONFLICT,
86+
# )
8787

8888

89-
@app.errorhandler(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
90-
def mediatype_not_supported(error):
91-
"""Handles unsupported media requests with 415_UNSUPPORTED_MEDIA_TYPE"""
92-
message = str(error)
93-
app.logger.warning(message)
94-
return (
95-
jsonify(
96-
status=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
97-
error="Unsupported media type",
98-
message=message,
99-
),
100-
status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
101-
)
89+
# @app.errorhandler(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
90+
# def mediatype_not_supported(error):
91+
# """Handles unsupported media requests with 415_UNSUPPORTED_MEDIA_TYPE"""
92+
# message = str(error)
93+
# app.logger.warning(message)
94+
# return (
95+
# jsonify(
96+
# status=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
97+
# error="Unsupported media type",
98+
# message=message,
99+
# ),
100+
# status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
101+
# )
102102

103103

104-
@app.errorhandler(status.HTTP_500_INTERNAL_SERVER_ERROR)
105-
def internal_server_error(error):
106-
"""Handles unexpected server error with 500_SERVER_ERROR"""
107-
message = str(error)
108-
app.logger.error(message)
109-
return (
110-
jsonify(
111-
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
112-
error="Internal Server Error",
113-
message=message,
114-
),
115-
status.HTTP_500_INTERNAL_SERVER_ERROR,
116-
)
104+
# @app.errorhandler(status.HTTP_500_INTERNAL_SERVER_ERROR)
105+
# def internal_server_error(error):
106+
# """Handles unexpected server error with 500_SERVER_ERROR"""
107+
# message = str(error)
108+
# app.logger.error(message)
109+
# return (
110+
# jsonify(
111+
# status=status.HTTP_500_INTERNAL_SERVER_ERROR,
112+
# error="Internal Server Error",
113+
# message=message,
114+
# ),
115+
# status.HTTP_500_INTERNAL_SERVER_ERROR,
116+
# )

0 commit comments

Comments
 (0)