fix(messaging.backends): do not require inbound settings for outbound and vice versa

pull/1/head
Luca 8 months ago
parent 8f6c965cd7
commit 4c47dffc67

@ -2,9 +2,13 @@ import json
from ast import literal_eval from ast import literal_eval
from datetime import datetime, timezone from datetime import datetime, timezone
from clicksend_client import ApiClient from clicksend_client import (
from clicksend_client import Configuration as BaseConfiguration ApiClient,
from clicksend_client import SMSApi, SmsMessage, SmsMessageCollection Configuration,
SMSApi,
SmsMessage,
SmsMessageCollection,
)
from clicksend_client.rest import ApiException from clicksend_client.rest import ApiException
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
@ -19,29 +23,6 @@ __all__ = ("Receiver", "Sender")
MAX_BATCH_SIZE = 1000 # see https://developers.clicksend.com/docs/rest/v3/#how-many-messages-can-i-send MAX_BATCH_SIZE = 1000 # see https://developers.clicksend.com/docs/rest/v3/#how-many-messages-can-i-send
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"]
client = ApiClient(Configuration())
class Receiver(BaseReceiver): class Receiver(BaseReceiver):
fetch = None fetch = None
@ -66,11 +47,30 @@ class Receiver(BaseReceiver):
class Sender(BaseSender): class Sender(BaseSender):
def __init__(self):
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"
)
config = Configuration()
config.username = settings.SMS_SETTINGS["clicksend_username"]
config.password = settings.SMS_SETTINGS["clicksend_password"]
self.client = ApiClient(config)
def send(self, messages): def send(self, messages):
messages = messages[:MAX_BATCH_SIZE] messages = messages[:MAX_BATCH_SIZE]
try: try:
response = SMSApi(client).sms_send_post( response = SMSApi(self.client).sms_send_post(
SmsMessageCollection( SmsMessageCollection(
messages=[ messages=[
SmsMessage( SmsMessage(

@ -12,7 +12,10 @@ __all__ = ("Receiver", "Sender")
BASE_URL = "https://api.sipgate.com/v2" BASE_URL = "https://api.sipgate.com/v2"
inbound_auth = HTTPBasicAuth(
class Receiver(BaseReceiver):
def __init__(self):
self.auth = HTTPBasicAuth(
settings.SMS_SETTINGS.get( settings.SMS_SETTINGS.get(
"sipgate_incoming_token_id", settings.SIPGATE_INCOMING_TOKEN_ID "sipgate_incoming_token_id", settings.SIPGATE_INCOMING_TOKEN_ID
), ),
@ -20,14 +23,7 @@ inbound_auth = HTTPBasicAuth(
"sipgate_incoming_token", settings.SIPGATE_INCOMING_TOKEN "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),
)
class Receiver(BaseReceiver):
def __init__(self):
self.from_dt = None self.from_dt = None
def fetch(self): def fetch(self):
@ -50,7 +46,7 @@ class Receiver(BaseReceiver):
while offset < total: while offset < total:
r = requests.get( r = requests.get(
f"{BASE_URL}/history", f"{BASE_URL}/history",
auth=inbound_auth, auth=self.auth,
params=params | {"offset": offset}, params=params | {"offset": offset},
) )
r.raise_for_status() r.raise_for_status()
@ -75,11 +71,17 @@ class Receiver(BaseReceiver):
class Sender(BaseSender): class Sender(BaseSender):
def __init__(self):
self.auth = HTTPBasicAuth(
settings.SMS_SETTINGS.get("sipgate_token_id", settings.SIPGATE_TOKEN_ID),
settings.SMS_SETTINGS.get("sipgate_token", settings.SIPGATE_TOKEN),
)
def send(self, messages): def send(self, messages):
for message in messages: for message in messages:
r = requests.post( r = requests.post(
f"{BASE_URL}/sessions/sms", f"{BASE_URL}/sessions/sms",
auth=outbound_auth, auth=self.auth,
json={ json={
"smsId": settings.SMS_SETTINGS.get( "smsId": settings.SMS_SETTINGS.get(
"sipgate_sms_extension", settings.SIPGATE_SMS_EXTENSION "sipgate_sms_extension", settings.SIPGATE_SMS_EXTENSION

Loading…
Cancel
Save