mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-17 03:40:04 -04:00
Parse the JOB API response converting new style response to old style object
- allows migration of API
This commit is contained in:
@@ -14,15 +14,41 @@ class JobApiClient(BaseAPIClient):
|
||||
self.client_id = app.config['ADMIN_CLIENT_USER_NAME']
|
||||
self.secret = app.config['ADMIN_CLIENT_SECRET']
|
||||
|
||||
@staticmethod
|
||||
def __convert_statistics(job):
|
||||
results = {
|
||||
'sending': 0,
|
||||
'delivered': 0,
|
||||
'failed': 0
|
||||
}
|
||||
if 'statistics' in job:
|
||||
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']
|
||||
return results
|
||||
|
||||
def get_job(self, service_id, job_id=None, limit_days=None, status=None):
|
||||
if job_id:
|
||||
params = {}
|
||||
if status is not None:
|
||||
params['status'] = status
|
||||
return self.get(url='/service/{}/job/{}'.format(service_id, job_id), params=params)
|
||||
job = self.get(url='/service/{}/job/{}'.format(service_id, job_id), params=params)
|
||||
|
||||
if 'notifications_sent' not in job['data']:
|
||||
stats = self.__convert_statistics(job)
|
||||
job['data']['notifications_sent'] = stats['sending']
|
||||
job['data']['notifications_delivered'] = stats['delivered']
|
||||
job['data']['notifications_failed'] = stats['failed']
|
||||
return job
|
||||
|
||||
params = {}
|
||||
if limit_days is not None:
|
||||
params['limit_days'] = limit_days
|
||||
|
||||
return self.get(url='/service/{}/job'.format(service_id), params=params)
|
||||
|
||||
def create_job(self, job_id, service_id, template_id, original_file_name, notification_count):
|
||||
@@ -34,4 +60,11 @@ class JobApiClient(BaseAPIClient):
|
||||
}
|
||||
data = _attach_current_user(data)
|
||||
resp = self.post(url='/service/{}/job'.format(service_id), data=data)
|
||||
return resp['data']
|
||||
|
||||
if 'notifications_sent' not in resp['data']:
|
||||
stats = self.__convert_statistics(resp)
|
||||
resp['data']['notifications_sent'] = stats['sending']
|
||||
resp['data']['notifications_delivered'] = stats['delivered']
|
||||
resp['data']['notifications_failed'] = stats['failed']
|
||||
|
||||
return resp
|
||||
|
||||
@@ -184,7 +184,6 @@ def test_should_show_updates_for_one_job_as_json(
|
||||
assert 'Recipient' in content['notifications']
|
||||
assert '07123456789' in content['notifications']
|
||||
assert 'Status' in content['notifications']
|
||||
print(content['notifications'])
|
||||
assert 'Delivered' in content['notifications']
|
||||
assert '11:10' in content['notifications']
|
||||
assert 'Uploaded by Test User on 1 January at 11:09' in content['status']
|
||||
|
||||
Reference in New Issue
Block a user