2016-02-17 12:57:51 +00:00
|
|
|
import logging
|
2016-03-02 09:33:20 +00:00
|
|
|
from monotonic import monotonic
|
2016-02-17 12:57:51 +00:00
|
|
|
from app.clients.sms import (
|
|
|
|
|
SmsClient,
|
|
|
|
|
SmsClientException
|
|
|
|
|
)
|
2016-03-02 09:33:20 +00:00
|
|
|
from flask import current_app
|
2016-02-17 12:57:51 +00:00
|
|
|
from requests import request, RequestException, HTTPError
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2016-03-10 15:40:41 +00:00
|
|
|
firetext_response_status = {
|
|
|
|
|
'0': {
|
|
|
|
|
"firetext_message": 'delivered',
|
|
|
|
|
"success": True,
|
|
|
|
|
"notify_status": 'delivered'
|
|
|
|
|
},
|
|
|
|
|
'1': {
|
|
|
|
|
"firetext_message": 'declined',
|
|
|
|
|
"success": False,
|
|
|
|
|
"notify_status": 'failed'
|
|
|
|
|
},
|
|
|
|
|
'2': {
|
|
|
|
|
"firetext_message": 'Undelivered (Pending with Network)',
|
|
|
|
|
"success": False,
|
|
|
|
|
"notify_status": 'sent'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 12:57:51 +00:00
|
|
|
|
|
|
|
|
class FiretextClientException(SmsClientException):
|
2016-03-10 13:22:45 +00:00
|
|
|
def __init__(self, response):
|
|
|
|
|
self.code = response['code']
|
|
|
|
|
self.description = response['description']
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return "Code {} description {}".format(self.code, self.description)
|
2016-02-17 12:57:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class FiretextClient(SmsClient):
|
|
|
|
|
'''
|
|
|
|
|
FireText sms client.
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
def init_app(self, config, *args, **kwargs):
|
|
|
|
|
super(SmsClient, self).__init__(*args, **kwargs)
|
|
|
|
|
self.api_key = config.config.get('FIRETEXT_API_KEY')
|
|
|
|
|
self.from_number = config.config.get('FIRETEXT_NUMBER')
|
2016-02-25 11:23:04 +00:00
|
|
|
self.name = 'firetext'
|
|
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
|
return self.name
|
2016-02-17 12:57:51 +00:00
|
|
|
|
2016-03-10 13:22:45 +00:00
|
|
|
def send_sms(self, to, content, notification_id=None):
|
2016-02-17 12:57:51 +00:00
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
"apiKey": self.api_key,
|
|
|
|
|
"from": self.from_number,
|
|
|
|
|
"to": to.replace('+', ''),
|
|
|
|
|
"message": content
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 13:22:45 +00:00
|
|
|
if notification_id:
|
|
|
|
|
data.update({
|
|
|
|
|
"reference": notification_id
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
start_time = monotonic()
|
2016-02-17 12:57:51 +00:00
|
|
|
try:
|
|
|
|
|
response = request(
|
|
|
|
|
"POST",
|
2016-03-10 13:22:45 +00:00
|
|
|
"https://www.firetext.co.uk/api/sendsms/json",
|
2016-02-17 12:57:51 +00:00
|
|
|
data=data
|
|
|
|
|
)
|
2016-03-10 13:22:45 +00:00
|
|
|
firetext_response = response.json()
|
|
|
|
|
if firetext_response['code'] != 0:
|
|
|
|
|
raise FiretextClientException(firetext_response)
|
2016-02-17 12:57:51 +00:00
|
|
|
response.raise_for_status()
|
|
|
|
|
except RequestException as e:
|
|
|
|
|
api_error = HTTPError.create(e)
|
2016-02-17 17:48:23 +00:00
|
|
|
logger.error(
|
2016-02-17 12:57:51 +00:00
|
|
|
"API {} request on {} failed with {} '{}'".format(
|
|
|
|
|
"POST",
|
|
|
|
|
"https://www.firetext.co.uk/api/sendsms",
|
|
|
|
|
api_error.status_code,
|
|
|
|
|
api_error.message
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
raise api_error
|
2016-03-02 09:33:20 +00:00
|
|
|
finally:
|
|
|
|
|
elapsed_time = monotonic() - start_time
|
|
|
|
|
current_app.logger.info("Firetext request finished in {}".format(elapsed_time))
|
2016-02-17 12:57:51 +00:00
|
|
|
return response
|