Skip to content

Commit

Permalink
Merge pull request #3 from SFDigitalServices/support-azure
Browse files Browse the repository at this point in the history
support azure blob files
  • Loading branch information
josh-chou authored Jun 2, 2021
2 parents a4d2591 + b9ae53e commit a6f9a21
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
17 changes: 12 additions & 5 deletions service/microservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Empty file added tests/__init__.py
Empty file.
9 changes: 8 additions & 1 deletion tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,29 @@ 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'}
with open(os.path.join(FILES_PATH, 'dummy.pdf'), 'rb') as f: # pylint: disable=invalid-name
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

0 comments on commit a6f9a21

Please sign in to comment.