From 3f641a2f84e8dfc2b6994252bc5eded900caced1 Mon Sep 17 00:00:00 2001 From: Josh Chou <626661+josh-chou@users.noreply.github.com> Date: Thu, 6 Apr 2023 14:28:38 -0700 Subject: [PATCH 1/3] resolve pylint issues --- .pylintrc | 12 ------------ service/microservice.py | 3 ++- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/.pylintrc b/.pylintrc index 9ccceba..e81c362 100644 --- a/.pylintrc +++ b/.pylintrc @@ -65,11 +65,7 @@ disable=print-statement, unpacking-in-except, old-raise-syntax, backtick, - long-suffix, - old-ne-operator, - old-octal-literal, import-star-module-level, - non-ascii-bytes-literal, raw-checker-failed, bad-inline-option, locally-disabled, @@ -117,7 +113,6 @@ disable=print-statement, range-builtin-not-iterating, filter-builtin-not-iterating, using-cmp-argument, - eq-without-hash, div-method, idiv-method, rdiv-method, @@ -329,13 +324,6 @@ max-line-length=100 # Maximum number of lines in a module. max-module-lines=1000 -# List of optional constructs for which whitespace checking is disabled. `dict- -# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}. -# `trailing-comma` allows a space between comma and closing bracket: (a, ). -# `empty-line` allows space-only lines. -no-space-check=trailing-comma, - dict-separator - # Allow the body of a class to be on the same line as the declaration if body # contains single statement. single-line-class-stmt=no diff --git a/service/microservice.py b/service/microservice.py index 82e4296..5c4fd3d 100755 --- a/service/microservice.py +++ b/service/microservice.py @@ -34,7 +34,8 @@ def cloud_storage_service(_req, resp): params={ 'name':path, 'apikey':os.environ.get('CLOUDSTORAGE_API_KEY') - } + }, + timeout=300 ) resp.status = falcon.get_http_status(response.status_code) resp.content_type = response.headers['Content-Type'] From 165ba6af1744501986a012b4e561570592c98991 Mon Sep 17 00:00:00 2001 From: Josh Chou <626661+josh-chou@users.noreply.github.com> Date: Thu, 6 Apr 2023 14:32:20 -0700 Subject: [PATCH 2/3] pass querystring through --- service/microservice.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/service/microservice.py b/service/microservice.py index 5c4fd3d..c7bb662 100755 --- a/service/microservice.py +++ b/service/microservice.py @@ -18,7 +18,10 @@ def start_service(): def cloud_storage_service(_req, resp): """Send the request through to the cloudstorage microservice""" - path = urlparse(_req.uri).path[1:] + parsed = urlparse(_req.uri) + + # strip off leading / + path = parsed.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 @@ -26,7 +29,7 @@ def cloud_storage_service(_req, resp): 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) + microservice_url = microservice_url.replace('1.0', '2.0', 1) + "?" + parsed.query path = path[3:] response = requests.get( From 2a8b054ee482f7db944acc5a4441b693ddbf28f0 Mon Sep 17 00:00:00 2001 From: Josh Chou <626661+josh-chou@users.noreply.github.com> Date: Tue, 11 Apr 2023 15:44:53 -0700 Subject: [PATCH 3/3] pass querystring --- service/microservice.py | 23 +++++++++++++++-------- tests/test_service.py | 6 +++--- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/service/microservice.py b/service/microservice.py index c7bb662..5c1ee68 100755 --- a/service/microservice.py +++ b/service/microservice.py @@ -1,6 +1,6 @@ """Main application module""" import os -from urllib.parse import urlparse +from urllib import parse import sentry_sdk import falcon import requests @@ -18,7 +18,7 @@ def start_service(): def cloud_storage_service(_req, resp): """Send the request through to the cloudstorage microservice""" - parsed = urlparse(_req.uri) + parsed = parse.urlparse(_req.uri) # strip off leading / path = parsed.path[1:] @@ -28,16 +28,23 @@ def cloud_storage_service(_req, resp): # eg. https://domain.com/az/some_file.pdf microservice_url = os.environ.get('CLOUDSTORAGE_URL') # default to s3 + request_params = { + 'name':path, + 'apikey':os.environ.get('CLOUDSTORAGE_API_KEY') + } + if path[0:3] == 'az/': - microservice_url = microservice_url.replace('1.0', '2.0', 1) + "?" + parsed.query - path = path[3:] + microservice_url = microservice_url.replace('1.0', '2.0', 1) + request_params['name'] = path[3:] # remove az from name + + # pass thru querystring + parsed_query_dict = parse.parse_qs(parsed.query) + for param, val in parsed_query_dict.items(): + request_params[param] = val[0] response = requests.get( microservice_url, - params={ - 'name':path, - 'apikey':os.environ.get('CLOUDSTORAGE_API_KEY') - }, + params=request_params, timeout=300 ) resp.status = falcon.get_http_status(response.status_code) diff --git a/tests/test_service.py b/tests/test_service.py index a6a56ed..4d971fa 100755 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -21,8 +21,8 @@ def client(): @pytest.fixture def mock_env(monkeypatch): """ mock environment var """ - for key in ENV_VARS: - monkeypatch.setenv(key, ENV_VARS[key]) + for key, val in ENV_VARS.items(): + monkeypatch.setenv(key, val) def test_endpoint(client, mock_env): # pylint: disable=unused-argument @@ -42,7 +42,7 @@ def test_endpoint(client, mock_env): assert response.content == content # happy path azure blob storage - response = client.simulate_get('/az/dummy.pdf') + response = client.simulate_get('/az/dummy.pdf?src=abc') assert response.status_code == 200 assert response.headers['Content-Type'] == 'application/pdf' assert response.content == content