2022-03-25 15:03:52 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
from requests import RequestException, request
|
|
|
|
|
|
2022-03-24 16:41:08 +00:00
|
|
|
from app.clients.sms import SmsClient, SmsClientResponseException
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_reach_responses(status, detailed_status_code=None):
|
|
|
|
|
if status == 'TODO-d':
|
|
|
|
|
return ("delivered", "TODO: Delivered")
|
|
|
|
|
elif status == 'TODO-tf':
|
|
|
|
|
return ("temporary-failure", "TODO: Temporary failure")
|
|
|
|
|
elif status == 'TODO-pf':
|
|
|
|
|
return ("permanent-failure", "TODO: Permanent failure")
|
|
|
|
|
else:
|
|
|
|
|
raise KeyError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ReachClient(SmsClient):
|
2022-03-25 15:03:52 +00:00
|
|
|
def init_app(self, *args, **kwargs):
|
|
|
|
|
super().init_app(*args, **kwargs)
|
|
|
|
|
self.url = self.current_app.config.get('REACH_URL')
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def name(self):
|
|
|
|
|
return 'reach'
|
|
|
|
|
|
|
|
|
|
def try_send_sms(self, to, content, reference, international, sender):
|
|
|
|
|
data = {
|
|
|
|
|
# TODO
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
response = request(
|
|
|
|
|
"POST",
|
|
|
|
|
self.url,
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers={
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
timeout=60
|
|
|
|
|
)
|
2022-03-24 16:41:08 +00:00
|
|
|
|
2022-03-25 15:03:52 +00:00
|
|
|
response.raise_for_status()
|
|
|
|
|
try:
|
|
|
|
|
json.loads(response.text)
|
2022-03-29 14:18:00 +01:00
|
|
|
except (ValueError, AttributeError):
|
|
|
|
|
raise SmsClientResponseException("Invalid response JSON")
|
|
|
|
|
except RequestException:
|
|
|
|
|
raise SmsClientResponseException("Request failed")
|
2022-03-24 16:41:08 +00:00
|
|
|
|
2022-03-25 15:03:52 +00:00
|
|
|
return response
|