From af59a6d3939e4f421e238b2abd1ad7f7b4accd57 Mon Sep 17 00:00:00 2001 From: Luca Date: Fri, 10 May 2024 09:44:28 +0200 Subject: [PATCH] chore(messaging.backends): better error messages --- shiftregister/messaging/backends/clicksend.py | 21 ++++++++++++------- shiftregister/messaging/backends/sipgate.py | 16 +++++++++++--- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/shiftregister/messaging/backends/clicksend.py b/shiftregister/messaging/backends/clicksend.py index 9885a1e..7a33e64 100644 --- a/shiftregister/messaging/backends/clicksend.py +++ b/shiftregister/messaging/backends/clicksend.py @@ -22,16 +22,21 @@ class Configuration(BaseConfiguration): def __init__(self): super().__init__() + for setting in ( + "clicksend_password", + "clicksend_sender_id", + "clicksend_username", + ): + try: + settings.SMS_SETTINGS[setting] + except KeyError: + raise ImproperlyConfigured( + f"'{setting}' must be set in SMS_SETTINGS for ClickSend backend" + ) + self.username = settings.SMS_SETTINGS["clicksend_username"] self.password = settings.SMS_SETTINGS["clicksend_password"] - try: - settings.SMS_SETTINGS["clicksend_sender_id"] - except KeyError: - raise ImproperlyConfigured( - "'clicksend_sender_id' must be set in SMS_SETTINGS for ClickSend backend" - ) - client = ApiClient(Configuration()) @@ -45,6 +50,8 @@ class Receiver(BaseReceiver): except ValueError: raise ValueError("invalid timestamp") + if not kwargs.get("from", ""): + raise ValueError("message has no sender") if not message_id: raise ValueError("empty message id") diff --git a/shiftregister/messaging/backends/sipgate.py b/shiftregister/messaging/backends/sipgate.py index 320fa29..57b66fa 100644 --- a/shiftregister/messaging/backends/sipgate.py +++ b/shiftregister/messaging/backends/sipgate.py @@ -12,7 +12,15 @@ __all__ = ("Receiver", "Sender") BASE_URL = "https://api.sipgate.com/v2" -auth = HTTPBasicAuth( +inbound_auth = HTTPBasicAuth( + settings.SMS_SETTINGS.get( + "sipgate_incoming_token_id", settings.SIPGATE_INCOMING_TOKEN_ID + ), + settings.SMS_SETTINGS.get( + "sipgate_incoming_token", settings.SIPGATE_INCOMING_TOKEN + ), +) +outbound_auth = HTTPBasicAuth( settings.SMS_SETTINGS.get("sipgate_token_id", settings.SIPGATE_TOKEN_ID), settings.SMS_SETTINGS.get("sipgate_token", settings.SIPGATE_TOKEN), ) @@ -41,7 +49,9 @@ class Receiver(BaseReceiver): total = 10 while offset < total: r = requests.get( - f"{BASE_URL}/history", auth=auth, params=params | {"offset": offset} + f"{BASE_URL}/history", + auth=inbound_auth, + params=params | {"offset": offset}, ) r.raise_for_status() @@ -69,7 +79,7 @@ class Sender(BaseSender): for message in messages: r = requests.post( f"{BASE_URL}/sessions/sms", - auth=auth, + auth=outbound_auth, json={ "smsId": settings.SMS_SETTINGS.get( "sipgate_sms_extension", settings.SIPGATE_SMS_EXTENSION