Skip to content

Commit

Permalink
Removed code snippets from API responses
Browse files Browse the repository at this point in the history
  • Loading branch information
brystmar committed Feb 2, 2020
1 parent 0a4ca61 commit 8d666bd
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 164 deletions.
2 changes: 2 additions & 0 deletions .flaskenv
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# When python-dotenv is installed, this automatically registers the entrypoint to our Flask app
FLASK_APP=main.py
FLASK_DEBUG=0
2 changes: 1 addition & 1 deletion backend/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Initialization file that defines items within the app."""
"""Initialization file that creates the app and applies our config object."""
from config import Config
from flask import Flask

Expand Down
1 change: 1 addition & 0 deletions backend/functions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def test_generate_new_id():


def test_set_when():
# TODO: Write tests!
pass


Expand Down
11 changes: 8 additions & 3 deletions backend/meta_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ def get(self) -> json:
output = markdown.markdown(content)
return {'message': 'Success', 'data': output}, 200

except BaseException as e:
error_msg = f"Error attempting to retrieve or compile the README file: {e}.)"
logger.debug(error_msg)
except FileNotFoundError as e:
error_msg = f"ERRORMSG: File not found."
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Error', 'data': error_msg}, 404

except ValueError as e:
error_msg = f"ERRORMSG: Error attempting to compile the README file.)"
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Error', 'data': error_msg}, 500
1 change: 0 additions & 1 deletion backend/meta_routes_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from backend.meta_routes import ReadmeApi
import pytest


class TestReadmeApi:
Expand Down
1 change: 0 additions & 1 deletion backend/models_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
from config import Config
from backend.models import Recipe, Step, Replacement
from backend.recipe_routes import generate_new_id
Expand Down
29 changes: 15 additions & 14 deletions backend/recipe_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def get(self) -> json:
logger.debug(f"End of RecipeCollectionApi.get()")
return {'message': 'Success', 'data': output}, 200
except BaseException as e:
error_msg = f"Error trying to retrieve or compile recipe list: {e}.)"
logger.debug(error_msg)
error_msg = f"Error trying to retrieve or compile recipe list.)"
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Error', 'data': error_msg}, 500

def post(self) -> json:
Expand Down Expand Up @@ -57,8 +57,8 @@ def post(self) -> json:
steps=args['steps']
)
except PynamoDBException as e:
error_msg = f"Error trying to save new recipe. \nData: {args}. \nError: {e}.)"
logger.debug(error_msg)
error_msg = f"Error trying to save new recipe.)"
logger.debug(f"{error_msg}\nData: {args}.\nError: {e}.")
return {'message': 'Error', 'data': error_msg}, 500

try:
Expand All @@ -68,8 +68,8 @@ def post(self) -> json:
logger.debug("End of RecipeCollectionApi.post()")
return {'message': 'Created', 'data': new_recipe.to_dict()}, 201
except PynamoDBException as e:
error_msg = f"Error trying to save new recipe {new_recipe.__repr__()}: {e}.)"
logger.debug(error_msg)
error_msg = f"Error trying to save new recipe.)"
logger.debug(f"{error_msg}\n{new_recipe.__repr__()}: {e}.")
return {'message': 'Error', 'data': error_msg}, 500


Expand All @@ -88,8 +88,8 @@ def get(self, recipe_id) -> json:
logger.debug(f"Recipe {recipe_id} not found.)")
return {'message': 'Not Found', 'data': f'Recipe {recipe_id} not found.'}, 404
except PynamoDBException as e:
error_msg = f"Error trying to retrieve recipe {recipe_id}: {e}.)"
logger.debug(error_msg)
error_msg = f"Error trying to retrieve recipe {recipe_id}.)"
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Error', 'data': error_msg}, 500

def put(self, recipe_id) -> json:
Expand All @@ -101,8 +101,9 @@ def put(self, recipe_id) -> json:
recipe = Recipe.get(recipe_id)
logger.debug(f"Recipe retrieved: {recipe.__repr__()})")
except Recipe.DoesNotExist as e:
logger.debug(f"Recipe {recipe_id} not found.)")
return {'message': 'Not Found', 'data': f'Recipe {recipe_id} not found.'}, 404
error_msg = f"Recipe {recipe_id} not found.)"
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Not Found', 'data': error_msg}, 404

# Initialize the parser
parser = reqparse.RequestParser(bundle_errors=True)
Expand Down Expand Up @@ -132,8 +133,8 @@ def put(self, recipe_id) -> json:
logger.debug(f"Recipe {recipe_id} not found.)")
return {'message': 'Not Found', 'data': f'Recipe {recipe_id} not found.'}, 404
except PynamoDBException as e:
error_msg = f"Error trying to save recipe {recipe_id}: {e}.)"
logger.debug(error_msg)
error_msg = f"Error trying to save recipe {recipe_id}.)"
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Error', 'data': error_msg}, 500

def delete(self, recipe_id) -> json:
Expand All @@ -152,8 +153,8 @@ def delete(self, recipe_id) -> json:
logger.debug(f"Recipe {footprint} deleted successfully.)")
return {'message': 'Success', 'data': f'Recipe {footprint} deleted successfully.'}, 200
except PynamoDBException as e:
error_msg = f"Error trying to delete recipe {recipe_id}: {e}.)"
logger.debug(error_msg)
error_msg = f"Error trying to delete recipe {recipe_id}.)"
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Error', 'data': error_msg}, 500


Expand Down
79 changes: 26 additions & 53 deletions backend/replacement_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,38 @@ def get(self, scope='all') -> json:
elif scope in ('ingredients', 'directions'):
items = Replacement.query(scope)
else:
return {
'message': 'Not Found',
'data': f'Invalid scope: {scope}'
}, 404
return {'message': 'Error', 'data': f'Invalid scope: {scope}'}, 404

except ScanError as e:
error_msg = f"Error trying to scan the Replacement table for scope: {scope}.\n{e}."
logger.debug(error_msg)
return {
'message': 'Error',
'data': error_msg
}, 500
error_msg = f"Scope {scope} not found."
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Error', 'data': error_msg}, 404

except QueryError as e:
error_msg = f"Error trying to query the Replacement table for scope: {scope}.\n{e}."
logger.debug(error_msg)
return {
'message': 'Error',
'data': error_msg
}, 500
error_msg = f"Scope {scope} not found."
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Error', 'data': error_msg}, 404

except BaseException as e:
error_msg = f"Unknown error reading the Replacement table for scope: {scope}.\n{e}."
logger.debug(error_msg)
return {
'message': 'Error',
'data': error_msg
}, 500
error_msg = f"Error searching for scope {scope}."
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Error', 'data': error_msg}, 404

output = []
for i in items:
output.append(i.to_dict())

logger.debug(f"End of ReplacementCollectionApi.get({scope})")
return {
'message': 'Success',
'data': output
}, 200
return {'message': 'Success', 'data': output}, 200

def put(self, scope, old_value) -> json:
"""Add or update a replacement record."""
logger.debug(f"Request: {request}")

if scope not in ('all', 'ingredients', 'directions'):
logger.debug(f"Invalid scope: {scope}.")
return {
'message': 'Validation Error',
'data': f"Invalid scope: {scope}."
}, 422
error_msg = f"Invalid scope: {scope}."
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Validation Error', 'data': error_msg}, 422

# Retrieve the record
try:
Expand All @@ -89,13 +75,9 @@ def put(self, scope, old_value) -> json:
try:
rep_to_update = Replacement(scope=scope, old=old_value, new=args['new'])
except PynamoDBException as e:
error_msg = f"Error creating a new Replacement entity w/scope: {scope}, " \
f"old: {old_value}, new: {args['new']}.\n{e}."
logger.debug(error_msg)
return {
'message': 'Error',
'data': error_msg
}, 500
error_msg = f"Error adding scope: {scope}, old: {old_value}, new: {args['new']}."
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Error', 'data': error_msg}, 500
else:
rep_to_update.scope = scope
rep_to_update.old = old_value
Expand All @@ -105,20 +87,11 @@ def put(self, scope, old_value) -> json:
rep_to_update.save()
logger.debug(f"End of ReplacementCollectionApi.put({scope}, {old_value})")
if exists:
return {
'message': 'Success',
'data': 'Replacement record updated successfully.'
}, 200
return {'message': 'Success', 'data': 'Replacement record updated.'}, 200
else:
return {
'message': 'Created',
'data': 'Replacement record created successfully.'
}, 201
return {'message': 'Created', 'data': 'Replacement record created.'}, 201

except PutError as e:
error_msg = f"Error updating a Replacement entity w/scope: {scope}, " \
f"old: {old_value}, new: {args['new']}.\n{e}."
logger.debug(error_msg)
return {
'message': 'Error',
'data': error_msg
}, 500
error_msg = f"Error updating scope: {scope}, old: {old_value}, new: {args['new']}."
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Error', 'data': error_msg}, 500
Loading

0 comments on commit 8d666bd

Please sign in to comment.