mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Refactored code to make it more maintainable and changed an error type
* Rather than an abort 404 returned a 500 and InvalidRequest so that the error is more easily handled on the admin console. If the file is missing but expected to be there is actually an internal error for admin * Refactored the code to remove duplicate code in calls to template preview by creating a new private method which is called with specific parameters
This commit is contained in:
@@ -7,7 +7,6 @@ from flask import (
|
||||
jsonify,
|
||||
request)
|
||||
from requests import post as requests_post
|
||||
from werkzeug.exceptions import abort
|
||||
|
||||
|
||||
from app.dao.notifications_dao import get_notification_by_id
|
||||
@@ -207,7 +206,9 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
|
||||
pdf_file = get_letter_pdf(notification)
|
||||
|
||||
except botocore.exceptions.ClientError:
|
||||
abort(404)
|
||||
current_app.logger.info
|
||||
raise InvalidRequest('Error getting letter file from S3 notification id {}'.format(notification_id),
|
||||
status_code=500)
|
||||
|
||||
content = base64.b64encode(pdf_file).decode('utf-8')
|
||||
|
||||
@@ -218,18 +219,7 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
|
||||
'?page={}'.format(page) if page else ''
|
||||
)
|
||||
|
||||
resp = requests_post(
|
||||
url,
|
||||
data=content,
|
||||
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
||||
)
|
||||
|
||||
if resp.status_code != 200:
|
||||
raise InvalidRequest(
|
||||
'Error generating preview for {}'.format(notification_id), status_code=500
|
||||
)
|
||||
|
||||
content = base64.b64encode(resp.content).decode('utf-8')
|
||||
content = _get_png_preview(url, content, notification.id)
|
||||
|
||||
else:
|
||||
|
||||
@@ -249,21 +239,27 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
|
||||
'dvla_org_id': service.dvla_organisation_id,
|
||||
}
|
||||
|
||||
resp = requests_post(
|
||||
'{}/preview.{}{}'.format(
|
||||
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
||||
file_type,
|
||||
'?page={}'.format(page) if page else ''
|
||||
),
|
||||
json=data,
|
||||
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
||||
url = '{}/preview.{}{}'.format(
|
||||
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
||||
file_type,
|
||||
'?page={}'.format(page) if page else ''
|
||||
)
|
||||
|
||||
if resp.status_code != 200:
|
||||
raise InvalidRequest(
|
||||
'Error generating preview for {}'.format(notification_id), status_code=500
|
||||
)
|
||||
|
||||
content = base64.b64encode(resp.content).decode('utf-8')
|
||||
content = _get_png_preview(url, data, notification.id)
|
||||
|
||||
return jsonify({"content": content})
|
||||
|
||||
|
||||
def _get_png_preview(url, data, notification_id):
|
||||
resp = requests_post(
|
||||
url,
|
||||
data=data,
|
||||
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
||||
)
|
||||
|
||||
if resp.status_code != 200:
|
||||
raise InvalidRequest(
|
||||
'Error generating preview for {}'.format(notification_id), status_code=500
|
||||
)
|
||||
|
||||
return base64.b64encode(resp.content).decode('utf-8')
|
||||
|
||||
Reference in New Issue
Block a user