refactor method results for consistent types

This commit is contained in:
2023-12-23 15:38:33 -07:00
parent f15ea55ad7
commit 9e85f15951
2 changed files with 15 additions and 12 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
*~
.aider*

26
app.py
View File

@@ -102,7 +102,8 @@ class WebPushService():
db.session.commit()
except Exception as e:
print(f"Error generating VAPID keys: {e}")
print(f"Error generating VAPID keys: {str(e)}")
raise e
def _initialize(self) -> None:
@@ -135,7 +136,8 @@ class WebPushService():
- vapid_key (VAPIDKey): The VAPID key model instance containing the private key used for sending the notification.
Returns:
- bool: True if the push notification was sent successfully, False otherwise.
- request.Response https://requests.readthedocs.io/en/latest/api.html#requests.Response if the push notification was sent successfully
or False if there was an exception
Notes:
- The `webpush` function is used to send the notification.
@@ -144,14 +146,14 @@ class WebPushService():
# Sending the push notification using the webpush function
try:
webpush(
result = webpush(
subscription_info=subscription_info,
data=json.dumps(message),
vapid_private_key=vapid_key.private_key,
vapid_claims={"sub": CONTACT_EMAIL}
)
time.sleep(1)
return True
return {"success": True, "result": result}
except WebPushException as ex:
now = datetime.datetime.now().isoformat()
@@ -172,7 +174,7 @@ class WebPushService():
else:
print("Error other than unsubscribed/expired.", ex.args[0], flush=True)
return False
return {"success": False, "message": ex.args[0]}
def _send_daily_notifications(self) -> None:
@@ -239,7 +241,7 @@ class WebPushService():
Header: Authentication: Basic ...
Returns:
- Tuple[str, int]: A JSON response indicating the success or failure of the operation, along with the appropriate HTTP status code.
- tuple with "success" as True or False, and "message" message string
Notes:
- If the operation is successful, a JSON response with a success message is returned with a 200 status code.
@@ -273,7 +275,7 @@ class WebPushService():
return jsonify(success=True, message="VAPID keys regenerated successfully"), 200
except Exception as e:
return jsonify(error=f'Error regenerating VAPID keys: {str(e)}'), 500
return jsonify(success=False, message=f'Error regenerating VAPID keys: {str(e)}'), 500
@staticmethod
@@ -317,7 +319,7 @@ class WebPushService():
Method: POST
Returns:
- Tuple[str, int]: A JSON response indicating the success or failure of the operation, along with the appropriate HTTP status code.
- A JSON response with "success" as True or False, "message" as a response message string, and potentially a "result" as a request.Response object from sending a test notification
Notes:
- If the operation is successful, a confirmation push notification is sent to the subscriber with a success message.
@@ -333,7 +335,7 @@ class WebPushService():
# Checking if the VAPID key is available
if not vapid_key:
return jsonify(success=False, error="No VAPID keys available"), 500
return jsonify(success=False, message="No VAPID keys available"), 500
# Creating a new Subscription instance with the provided data
subscription = Subscription(endpoint=content['endpoint'],
@@ -361,10 +363,10 @@ class WebPushService():
message = {"title": "Subscription Successful", "message": "Thank you for subscribing!"}
# Sending the confirmation push notification
success = WebPushService._send_push_notification(subscription_info, message, vapid_key)
result = WebPushService._send_push_notification(subscription_info, message, vapid_key)
# Returning the operation status
return jsonify(success=success)
return jsonify(success=result.ok, message=result.text, result=result)
@staticmethod
@@ -404,7 +406,7 @@ class WebPushService():
# If the subscription is not found, return an error message
else:
return jsonify(success=False, error="Subscription not found"), 404
return jsonify(success=False, message="Subscription not found"), 404
web_push_service = WebPushService(app, "app")