diff --git a/app/__init__.py b/app/__init__.py index 81eeec6e0..df76d9b81 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -16,6 +16,7 @@ from werkzeug.local import LocalProxy from app.celery.celery import NotifyCelery from app.clients import Clients +from app.clients.document_download import DocumentDownloadClient from app.clients.email.aws_ses import AwsSesClient from app.clients.sms.firetext import FiretextClient from app.clients.sms.loadtesting import LoadtestingClient @@ -39,6 +40,7 @@ deskpro_client = DeskproClient() statsd_client = StatsdClient() redis_store = RedisClient() performance_platform_client = PerformancePlatformClient() +document_download_client = DocumentDownloadClient() clients = Clients() @@ -71,6 +73,7 @@ def create_app(application): encryption.init_app(application) redis_store.init_app(application) performance_platform_client.init_app(application) + document_download_client.init_app(application) clients.init_app(sms_clients=[firetext_client, mmg_client, loadtest_client], email_clients=[aws_ses_client]) register_blueprint(application) diff --git a/app/clients/document_download.py b/app/clients/document_download.py new file mode 100644 index 000000000..cb99ac88e --- /dev/null +++ b/app/clients/document_download.py @@ -0,0 +1,27 @@ +import base64 + +import requests + + +class DocumentDownloadClient: + + def init_app(self, app): + self.api_host = app.config['DOCUMENT_DOWNLOAD_API_HOST'] + self.auth_token = app.config['DOCUMENT_DOWNLOAD_API_KEY'] + + def get_upload_url(self, service_id): + return "{}/services/{}/documents".format(self.api_host, service_id) + + def upload_document(self, service_id, file_contents): + response = requests.post( + self.get_upload_url(service_id), + headers={ + 'Authorization': "Bearer {}".format(self.auth_token), + }, + files={ + 'document': base64.b64decode(file_contents) + } + ) + response.raise_for_status() + + return response.json()['document']['url'] diff --git a/app/config.py b/app/config.py index b9c6b9d9b..035416c70 100644 --- a/app/config.py +++ b/app/config.py @@ -318,6 +318,9 @@ class Config(object): TEMPLATE_PREVIEW_API_HOST = os.environ.get('TEMPLATE_PREVIEW_API_HOST', 'http://localhost:6013') TEMPLATE_PREVIEW_API_KEY = os.environ.get('TEMPLATE_PREVIEW_API_KEY', 'my-secret-key') + DOCUMENT_DOWNLOAD_API_HOST = os.environ.get('DOCUMENT_DOWNLOAD_API_HOST', 'http://localhost:7000') + DOCUMENT_DOWNLOAD_API_KEY = os.environ.get('DOCUMENT_DOWNLOAD_API_KEY', 'auth-token') + LETTER_PROCESSING_DEADLINE = time(17, 30) MMG_URL = "https://api.mmg.co.uk/json/api.php" diff --git a/tests/app/clients/test_document_download.py b/tests/app/clients/test_document_download.py new file mode 100644 index 000000000..e01094192 --- /dev/null +++ b/tests/app/clients/test_document_download.py @@ -0,0 +1,37 @@ +import requests +import requests_mock +import pytest + +from app.clients.document_download import DocumentDownloadClient + + +@pytest.fixture(scope='function') +def document_download(client, mocker): + client = DocumentDownloadClient() + current_app = mocker.Mock(config={ + 'DOCUMENT_DOWNLOAD_API_HOST': 'https://document-download', + 'DOCUMENT_DOWNLOAD_API_KEY': 'test-key' + }) + client.init_app(current_app) + return client + + +def test_get_upload_url(document_download): + assert document_download.get_upload_url('service-id') == 'https://document-download/services/service-id/documents' + + +def test_upload_document(document_download): + with requests_mock.Mocker() as request_mock: + request_mock.post('https://document-download/services/service-id/documents', json={ + 'document': {'url': 'https://document-download/services/service-id/documents/uploaded-url'} + }, status_code=201) + + resp = document_download.upload_document('service-id', 'abababab') + + assert resp == 'https://document-download/services/service-id/documents/uploaded-url' + + +def test_should_raise_for_status(document_download): + with pytest.raises(requests.HTTPError), requests_mock.Mocker() as request_mock: + request_mock.post('https://document-download/services/service-id/documents', json={}, status_code=403) + document_download.upload_document('service-id', 'abababab')