2016-08-30 14:54:33 +01:00
|
|
|
from collections import defaultdict
|
2016-01-29 10:27:23 +00:00
|
|
|
|
2016-02-11 15:27:08 +00:00
|
|
|
from notifications_python_client.base import BaseAPIClient
|
2016-04-15 11:08:19 +01:00
|
|
|
from app.notify_client import _attach_current_user
|
2016-01-29 10:27:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class JobApiClient(BaseAPIClient):
|
2016-10-11 10:35:33 +01:00
|
|
|
|
|
|
|
|
JOB_STATUSES = {
|
|
|
|
|
'scheduled',
|
|
|
|
|
'pending',
|
|
|
|
|
'in progress',
|
|
|
|
|
'finished',
|
|
|
|
|
'cancelled',
|
|
|
|
|
'sending limits exceeded',
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-12 12:14:57 +01:00
|
|
|
def __init__(self):
|
2016-09-12 14:59:53 +01:00
|
|
|
super().__init__("a", "b", "c")
|
2016-01-29 10:27:23 +00:00
|
|
|
|
|
|
|
|
def init_app(self, app):
|
|
|
|
|
self.base_url = app.config['API_HOST_NAME']
|
2016-09-08 15:55:07 +01:00
|
|
|
self.service_id = app.config['ADMIN_CLIENT_USER_NAME']
|
|
|
|
|
self.api_key = app.config['ADMIN_CLIENT_SECRET']
|
2016-01-29 10:27:23 +00:00
|
|
|
|
2016-08-23 16:58:50 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def __convert_statistics(job):
|
2016-08-30 14:54:33 +01:00
|
|
|
results = defaultdict(int)
|
2016-08-31 12:23:17 +01:00
|
|
|
for outcome in job['statistics']:
|
|
|
|
|
if outcome['status'] in ['failed', 'technical-failure', 'temporary-failure', 'permanent-failure']:
|
|
|
|
|
results['failed'] += outcome['count']
|
|
|
|
|
if outcome['status'] in ['sending', 'pending', 'created']:
|
|
|
|
|
results['sending'] += outcome['count']
|
|
|
|
|
if outcome['status'] in ['delivered']:
|
|
|
|
|
results['delivered'] += outcome['count']
|
|
|
|
|
results['requested'] += outcome['count']
|
2016-08-23 16:58:50 +01:00
|
|
|
return results
|
|
|
|
|
|
2016-09-22 09:53:33 +01:00
|
|
|
def get_job(self, service_id, job_id):
|
|
|
|
|
params = {}
|
|
|
|
|
job = self.get(url='/service/{}/job/{}'.format(service_id, job_id), params=params)
|
2016-09-05 14:29:58 +01:00
|
|
|
|
2016-09-22 09:53:33 +01:00
|
|
|
stats = self.__convert_statistics(job['data'])
|
|
|
|
|
job['data']['notifications_sent'] = stats['delivered'] + stats['failed']
|
|
|
|
|
job['data']['notifications_delivered'] = stats['delivered']
|
|
|
|
|
job['data']['notifications_failed'] = stats['failed']
|
|
|
|
|
job['data']['notifications_requested'] = stats['requested']
|
2016-09-05 14:29:58 +01:00
|
|
|
|
2016-09-22 09:53:33 +01:00
|
|
|
return job
|
2016-08-23 16:58:50 +01:00
|
|
|
|
2016-10-10 14:50:59 +01:00
|
|
|
def get_jobs(self, service_id, limit_days=None, statuses=None, page=1):
|
|
|
|
|
params = {'page': page}
|
2016-05-25 12:04:51 +01:00
|
|
|
if limit_days is not None:
|
|
|
|
|
params['limit_days'] = limit_days
|
2016-10-05 09:52:31 +01:00
|
|
|
if statuses is not None:
|
|
|
|
|
params['statuses'] = ','.join(statuses)
|
2016-08-23 16:58:50 +01:00
|
|
|
|
2016-08-26 15:21:10 +01:00
|
|
|
jobs = self.get(url='/service/{}/job'.format(service_id), params=params)
|
|
|
|
|
for job in jobs['data']:
|
2016-09-05 14:29:58 +01:00
|
|
|
stats = self.__convert_statistics(job)
|
|
|
|
|
job['notifications_sent'] = stats['delivered'] + stats['failed']
|
|
|
|
|
job['notifications_delivered'] = stats['delivered']
|
|
|
|
|
job['notifications_failed'] = stats['failed']
|
|
|
|
|
job['notifications_requested'] = stats['requested']
|
2016-08-26 15:21:10 +01:00
|
|
|
|
|
|
|
|
return jobs
|
2016-01-29 15:35:35 +00:00
|
|
|
|
2016-09-05 13:39:03 +01:00
|
|
|
def create_job(self, job_id, service_id, template_id, original_file_name, notification_count, scheduled_for=None):
|
2016-01-29 10:27:23 +00:00
|
|
|
data = {
|
|
|
|
|
"id": job_id,
|
|
|
|
|
"template": template_id,
|
2016-02-01 11:28:36 +00:00
|
|
|
"original_file_name": original_file_name,
|
2016-02-22 14:52:16 +00:00
|
|
|
"notification_count": notification_count
|
2016-01-29 10:27:23 +00:00
|
|
|
}
|
2016-09-05 13:39:03 +01:00
|
|
|
|
|
|
|
|
if scheduled_for:
|
|
|
|
|
data.update({'scheduled_for': scheduled_for})
|
|
|
|
|
|
2016-08-11 14:20:43 +01:00
|
|
|
data = _attach_current_user(data)
|
2016-08-24 10:35:04 +01:00
|
|
|
job = self.post(url='/service/{}/job'.format(service_id), data=data)
|
2016-08-23 16:58:50 +01:00
|
|
|
|
2016-09-05 14:29:58 +01:00
|
|
|
stats = self.__convert_statistics(job['data'])
|
|
|
|
|
job['data']['notifications_sent'] = stats['delivered'] + stats['failed']
|
|
|
|
|
job['data']['notifications_delivered'] = stats['delivered']
|
|
|
|
|
job['data']['notifications_failed'] = stats['failed']
|
|
|
|
|
job['data']['notifications_requested'] = stats['requested']
|
2016-08-23 16:58:50 +01:00
|
|
|
|
2016-08-24 10:35:04 +01:00
|
|
|
return job
|
2016-09-01 15:40:49 +01:00
|
|
|
|
|
|
|
|
def cancel_job(self, service_id, job_id):
|
|
|
|
|
|
|
|
|
|
job = self.post(
|
|
|
|
|
url='/service/{}/job/{}/cancel'.format(service_id, job_id),
|
|
|
|
|
data={}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
stats = self.__convert_statistics(job['data'])
|
|
|
|
|
job['data']['notifications_sent'] = stats['delivered'] + stats['failed']
|
|
|
|
|
job['data']['notifications_delivered'] = stats['delivered']
|
|
|
|
|
job['data']['notifications_failed'] = stats['failed']
|
|
|
|
|
job['data']['notifications_requested'] = stats['requested']
|
|
|
|
|
|
|
|
|
|
return job
|