diff --git a/resources/ApiKey.py b/resources/ApiKey.py new file mode 100644 index 00000000..ccec6b6d --- /dev/null +++ b/resources/ApiKey.py @@ -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 diff --git a/resources/DataSources.py b/resources/DataSources.py index f0ef5ae7..7d9e2f78 100644 --- a/resources/DataSources.py +++ b/resources/DataSources.py @@ -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): @@ -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): @@ -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): @@ -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 diff --git a/resources/QuickSearch.py b/resources/QuickSearch.py index 080236a8..a782b3b6 100644 --- a/resources/QuickSearch.py +++ b/resources/QuickSearch.py @@ -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: diff --git a/resources/SearchTokens.py b/resources/SearchTokens.py index 0bbfa13f..5ee44390 100644 --- a/resources/SearchTokens.py +++ b/resources/SearchTokens.py @@ -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: @@ -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 diff --git a/resources/User.py b/resources/User.py index c26725df..f0a17a0a 100644 --- a/resources/User.py +++ b/resources/User.py @@ -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): @@ -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