Skip to content

Commit

Permalink
Merge pull request #99 from CSCI-GA-2820-SP24-003/refactor-rest-x
Browse files Browse the repository at this point in the history
Refactor Rest X
  • Loading branch information
carolinetfls authored Apr 29, 2024
2 parents f9d9668 + 3f53fb9 commit 3d7c35a
Show file tree
Hide file tree
Showing 15 changed files with 448 additions and 312 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NYU DevOps Project Template

[![CI Build](https://github.com/CSCI-GA-2820-SP24-003/shopcarts/actions/workflows/ci.yml/badge.svg)](https://github.com/CSCI-GA-2820-SP24-003/shopcarts/actions/workflows/ci.yml)
[![CI Build](https://github.com/CSCI-GA-2820-SP24-003/shopcarts/actions/workflows/ci.yml/badge.svg)](https://github.com/CSCI-GA-2820-SP24-003/shopcarts/actions/workflows/tdd.yml)
[![codecov](https://codecov.io/gh/CSCI-GA-2820-SP24-003/shopcarts/graph/badge.svg?token=MM379ZQE9D)](https://codecov.io/gh/CSCI-GA-2820-SP24-003/shopcarts)

[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
Expand Down
3 changes: 2 additions & 1 deletion features/steps/shopcarts_steps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# spell: ignore shopcarts shopcart
"""
Shopcart Steps
Expand All @@ -19,7 +20,7 @@ def step_impl(context):
""" Delete all shopcarts and load new ones """

# List all of the shopcarts and delete them one by one
rest_endpoint = f"{context.base_url}/shopcarts"
rest_endpoint = f"{context.base_url}/api/shopcarts"
context.resp = requests.get(rest_endpoint)
assert(context.resp.status_code == HTTP_200_OK)
for shopcart in context.resp.json():
Expand Down
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 25 additions & 14 deletions service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@
"""
import sys
from flask import Flask
from flask_restx import Api
from service import config
from service.common import log_handlers
from flask_restx import Api

# Will be initialize when app is created
api = None # pylint: disable=invalid-name

# Document the type of authorization required
authorizations = {
"apikey": {
"type": "apiKey",
"in": "header",
"name": "X-Api-Key",
}
}


############################################################
# Initialize the Flask instance
############################################################
Expand All @@ -36,31 +46,32 @@ def create_app():
app = Flask(__name__)
app.config.from_object(config)

# Initialize Plugins
# pylint: disable=import-outside-toplevel
from service.models.persistent_base import db

######################################################################
# Configure Swagger before initializing it
######################################################################
app.url_map.strict_slashes = False

# Initialize the RESTX API
global api
api = Api(
app,
version="1.0.0",
title="Shopcart Demo REST API Service",
description="This is a sample server Shopcart server.",
default="Shopcarts",
title="Shopcart REST API Service",
description="This is the REST API for the Shopcart Service",
default="shopcarts",
default_label="Shopcart operations",
doc="/apidocs", # default also could use doc='/apidocs/',
doc="/apidocs",
authorizations=authorizations,
prefix="/api",
)

# Initialize Plugins
# pylint: disable=import-outside-toplevel
from service.models.persistent_base import db

db.init_app(app)

with app.app_context():
# Dependencies require we import the routes AFTER the Flask app is created
# pylint: disable=wrong-import-position, wrong-import-order, unused-import
from service import routes # noqa: F401 E402
# pylint: disable=wrong-import-position, wrong-import-order, unused-import, cyclic-import
from service import routes, models # noqa: F401 E402
from service.common import error_handlers, cli_commands # noqa: F401, E402

try:
Expand Down
14 changes: 9 additions & 5 deletions service/common/error_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@
"""
Module: error_handlers
"""
from flask import jsonify
from flask import current_app as app # Import Flask application
from service.models import DataValidationError
from service import api
from . import status


######################################################################
# Error Handlers
######################################################################
@app.errorhandler(DataValidationError)
@api.errorhandler(DataValidationError)
def request_validation_error(error):
"""Handles Value Errors from bad data"""
return bad_request(error)


message = str(error)
app.logger.error(message)
return {
"status_code": status.HTTP_400_BAD_REQUEST,
"error": "Bad Request",
"message": message,
}, status.HTTP_400_BAD_REQUEST
1 change: 0 additions & 1 deletion service/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@

# , ShopCartStatus
from .shop_cart_item import ShopCartItem

4 changes: 2 additions & 2 deletions service/models/shop_cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def serialize(self) -> dict:
"id": self.id,
"user_id": self.user_id,
"name": self.name,
"total_price": str(self.total_price),
"total_price": float(self.total_price),
"status": self.status.name,
"items": [],
}
Expand Down Expand Up @@ -149,5 +149,5 @@ def find_by_status(cls, status):
Args:
status (ShopCartStatus): the status of the ShopCarts you want to match
"""
logger.info("Processing status query for %s ...", status.name)
logger.info("Processing status query for %s ...", status)
return cls.query.filter(cls.status == status).all()
2 changes: 1 addition & 1 deletion service/models/shop_cart_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def serialize(self) -> dict:
"name": self.name,
"product_id": self.product_id,
"quantity": self.quantity,
"price": self.price,
"price": float(self.price),
}

def deserialize(self, data):
Expand Down
Loading

0 comments on commit 3d7c35a

Please sign in to comment.