Skip to content
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

standardized error codes #201

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions resources/ApiKey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from werkzeug.security import check_password_hash
from flask_restful import Resource
from flask import request
from middleware.user_queries import user_get_results
import uuid


class ApiKey(Resource):
def __init__(self, **kwargs):
self.psycopg2_connection = kwargs["psycopg2_connection"]

def get(self):
"""
Generate an API key for a user that successfully logs in
"""
try:
data = request.get_json()
email = data.get("email")
password = data.get("password")
cursor = self.psycopg2_connection.cursor()
user_data = user_get_results(cursor, email)

if check_password_hash(user_data["password_digest"], password):
api_key = uuid.uuid4().hex
user_id = str(user_data["id"])
cursor.execute(
"UPDATE users SET api_key = %s WHERE id = %s", (api_key, user_id)
)
payload = {
"message": "API key successfully created",
"api_key": api_key,
}
self.psycopg2_connection.commit()
return payload

except Exception as e:
self.psycopg2_connection.rollback()
print(str(e))
return {"message": str(e)}, 500
14 changes: 7 additions & 7 deletions resources/DataSources.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def get(self, data_source_id):
return data_source_details

else:
return "Data source not found.", 404
return {"message": "Data source not found."}, 404

except Exception as e:
print(str(e))
return "There has been an error pulling data!"
return {"message": "There has been an error pulling data!"}, 500

@api_required
def put(self, data_source_id):
Expand Down Expand Up @@ -64,11 +64,11 @@ def put(self, data_source_id):

cursor.execute(sql_query)
self.psycopg2_connection.commit()
return {"status": "success"}
return {"message": "Data source updated successfully."}

except Exception as e:
print(str(e))
return "There has been an error updating the data source", 400
return {"message": "There has been an error updating the data source"}, 500


class DataSources(Resource):
Expand All @@ -90,7 +90,7 @@ def get(self):
except Exception as e:
self.psycopg2_connection.rollback()
print(str(e))
return "There has been an error pulling data!"
return {"message": "There has been an error pulling data!"}, 500

@api_required
def post(self):
Expand Down Expand Up @@ -129,9 +129,9 @@ def post(self):
cursor.execute(sql_query)
self.psycopg2_connection.commit()

return True
return {"message": "Data source added successfully."}

except Exception as e:
self.psycopg2_connection.rollback()
print(str(e))
return False
return {"message": "There has been an error adding the data source"}, 500
6 changes: 6 additions & 0 deletions resources/QuickSearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def get(self, search, location):
self.psycopg2_connection, search, location
)

if data_sources["count"] == 0:
return {
"count": 0,
message: "No results found. Please considering requesting a new data source.",
}, 404

return data_sources

except Exception as e:
Expand Down
8 changes: 4 additions & 4 deletions resources/SearchTokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get(self):
except Exception as e:
self.psycopg2_connection.rollback()
print(str(e))
return "There has been an error pulling data!"
return {"message": "There has been an error pulling data!"}, 500

elif endpoint == "data-sources-by-id":
try:
Expand All @@ -89,16 +89,16 @@ def get(self):
return data_source_details

else:
return "Data source not found.", 404
return {"message": "Data source not found."}, 404

except Exception as e:
print(str(e))
return "There has been an error pulling data!"

else:
return {"error": "Unknown endpoint"}, 500
return {"message": "Unknown endpoint"}, 500

except Exception as e:
self.psycopg2_connection.rollback()
print(str(e))
return {"error": e}, 500
return {"message": e}, 500
16 changes: 11 additions & 5 deletions resources/User.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@ def get(self):
if len(results) > 0:
user_data = {"id": results[0][0], "password_digest": results[0][1]}
else:
return {"error": "no match"}
return {
"message": "There username or password is incorrect. Please try again."
}, 400

if check_password_hash(user_data["password_digest"], password):
api_key = uuid.uuid4().hex
user_id = str(user_data["id"])
cursor.execute(
"UPDATE users SET api_key = %s WHERE id = %s", (api_key, user_id)
)
payload = {"api_key": api_key}
payload = {
"message": "API key successfully created",
"api_key": api_key,
}
self.psycopg2_connection.commit()
return payload

except Exception as e:
self.psycopg2_connection.rollback()
print(str(e))
return {"error": str(e)}
return {"message": str(e)}, 500

# Sign up function: allows a user to sign up by submitting an email and password. The email and a hashed password are stored in the users table and this data is returned to the user upon completion
def post(self):
Expand All @@ -57,9 +63,9 @@ def post(self):
)
self.psycopg2_connection.commit()

return {"data": "Successfully added user"}
return {"message": "Successfully added user"}

except Exception as e:
self.psycopg2_connection.rollback()
print(str(e))
return {"error": e}
return {"message": e}, 500