diff --git a/service/microservice.py b/service/microservice.py index e9c8341..82e4296 100755 --- a/service/microservice.py +++ b/service/microservice.py @@ -5,9 +5,6 @@ import falcon import requests -CLOUDSTORAGE_URL = os.environ.get('CLOUDSTORAGE_URL') -CLOUDSTORAGE_API_KEY = os.environ.get('CLOUDSTORAGE_API_KEY') - def start_service(): """Start this service set SENTRY_DSN environmental variable to enable logging with Sentry @@ -22,11 +19,21 @@ def start_service(): def cloud_storage_service(_req, resp): """Send the request through to the cloudstorage microservice""" path = urlparse(_req.uri).path[1:] + + # determine whether to use amazon s3 or azure blob storage + # Use 'az' at beginning of the path to signal that file is from azure + # eg. https://domain.com/az/some_file.pdf + microservice_url = os.environ.get('CLOUDSTORAGE_URL') # default to s3 + + if path[0:3] == 'az/': + microservice_url = microservice_url.replace('1.0', '2.0', 1) + path = path[3:] + response = requests.get( - CLOUDSTORAGE_URL, + microservice_url, params={ 'name':path, - 'apikey':CLOUDSTORAGE_API_KEY + 'apikey':os.environ.get('CLOUDSTORAGE_API_KEY') } ) resp.status = falcon.get_http_status(response.status_code) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_service.py b/tests/test_service.py index d654ffe..a6a56ed 100755 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -28,7 +28,6 @@ def test_endpoint(client, mock_env): # pylint: disable=unused-argument """Test the endpoint""" - # pdf happy path with patch('service.microservice.requests.get') as mock_get: mock_get.return_value.status_code = 200 mock_get.return_value.headers = {'Content-Type':'application/pdf'} @@ -36,14 +35,22 @@ def test_endpoint(client, mock_env): content = f.read() mock_get.return_value.content = content + # pdf happy path amazon s3 response = client.simulate_get('/dummy.pdf') assert response.status_code == 200 assert response.headers['Content-Type'] == 'application/pdf' assert response.content == content + # happy path azure blob storage + response = client.simulate_get('/az/dummy.pdf') + assert response.status_code == 200 + assert response.headers['Content-Type'] == 'application/pdf' + assert response.content == content + # 404 file not found with patch('service.microservice.requests.get') as mock_get: mock_get.return_value.status_code = 404 + mock_get.return_value.content = "File not found" response = client.simulate_get('/file_that_no_exists') assert response.status_code == 404