Browse Source

refactor method results for consistent types

test-message
Trent Larson 9 months ago
parent
commit
9e85f15951
  1. 1
      .gitignore
  2. 26
      app.py

1
.gitignore

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

26
app.py

@ -102,7 +102,8 @@ class WebPushService():
db.session.commit() db.session.commit()
except Exception as e: 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: 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. - vapid_key (VAPIDKey): The VAPID key model instance containing the private key used for sending the notification.
Returns: 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: Notes:
- The `webpush` function is used to send the notification. - The `webpush` function is used to send the notification.
@ -144,14 +146,14 @@ class WebPushService():
# Sending the push notification using the webpush function # Sending the push notification using the webpush function
try: try:
webpush( result = webpush(
subscription_info=subscription_info, subscription_info=subscription_info,
data=json.dumps(message), data=json.dumps(message),
vapid_private_key=vapid_key.private_key, vapid_private_key=vapid_key.private_key,
vapid_claims={"sub": CONTACT_EMAIL} vapid_claims={"sub": CONTACT_EMAIL}
) )
time.sleep(1) time.sleep(1)
return True return {"success": True, "result": result}
except WebPushException as ex: except WebPushException as ex:
now = datetime.datetime.now().isoformat() now = datetime.datetime.now().isoformat()
@ -172,7 +174,7 @@ class WebPushService():
else: else:
print("Error other than unsubscribed/expired.", ex.args[0], flush=True) 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: def _send_daily_notifications(self) -> None:
@ -239,7 +241,7 @@ class WebPushService():
Header: Authentication: Basic ... Header: Authentication: Basic ...
Returns: 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: Notes:
- If the operation is successful, a JSON response with a success message is returned with a 200 status code. - 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 return jsonify(success=True, message="VAPID keys regenerated successfully"), 200
except Exception as e: 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 @staticmethod
@ -317,7 +319,7 @@ class WebPushService():
Method: POST Method: POST
Returns: 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: Notes:
- If the operation is successful, a confirmation push notification is sent to the subscriber with a success message. - 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 # Checking if the VAPID key is available
if not vapid_key: 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 # Creating a new Subscription instance with the provided data
subscription = Subscription(endpoint=content['endpoint'], subscription = Subscription(endpoint=content['endpoint'],
@ -361,10 +363,10 @@ class WebPushService():
message = {"title": "Subscription Successful", "message": "Thank you for subscribing!"} message = {"title": "Subscription Successful", "message": "Thank you for subscribing!"}
# Sending the confirmation push notification # 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 # Returning the operation status
return jsonify(success=success) return jsonify(success=result.ok, message=result.text, result=result)
@staticmethod @staticmethod
@ -404,7 +406,7 @@ class WebPushService():
# If the subscription is not found, return an error message # If the subscription is not found, return an error message
else: 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") web_push_service = WebPushService(app, "app")

Loading…
Cancel
Save