Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: return response caching #199

Merged
merged 1 commit into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/app/api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

def get_dataset_artifact_s3_uri(url_dataroot: str = None, dataset_id: str = None):
dataset_artifact_s3_uri = get_dataset_metadata(url_dataroot, dataset_id, current_app.app_config)["s3_uri"]
dataset_artifact_s3_uri = dataset_artifact_s3_uri.rstrip("/") # remove trailing slash for cloudfront caching.
if dataset_artifact_s3_uri:
dataset_artifact_s3_uri = dataset_artifact_s3_uri.rstrip("/") # remove trailing slash for cloudfront caching.
return dataset_artifact_s3_uri


Expand Down
6 changes: 4 additions & 2 deletions server/data_common/dataset_metadata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import logging
from typing import Union

import requests
from flask import current_app

Expand Down Expand Up @@ -27,7 +29,7 @@ def request_dataset_metadata_from_data_portal(data_portal_api_base: str, explore
return None


def infer_dataset_s3_uri(server_config: ServerConfig, dataset_root: str, dataset_id: str):
def infer_dataset_s3_uri(server_config: ServerConfig, dataset_root: str, dataset_id: str) -> Union[str, None]:
"""
Use the dataset_root, dataset_id, and the server config to infer the physical S3 URI for an Explorer dataset
artifact
Expand Down Expand Up @@ -56,7 +58,7 @@ def infer_dataset_s3_uri(server_config: ServerConfig, dataset_root: str, dataset
return s3_uri


def get_dataset_metadata(dataset_root: str, dataset_id: str, app_config: AppConfig):
def get_dataset_metadata(dataset_root: str, dataset_id: str, app_config: AppConfig) -> dict:
"""
Given the dataset root and dataset_id and the explorer web base url (from the app_config), returns the metadata for
the dataset, including the s3 URI of the dataset artifact.
Expand Down
53 changes: 20 additions & 33 deletions server/tests/unit/common/apis/test_api_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,39 +731,26 @@ def setUpClass(cls):

@patch("server.data_common.dataset_metadata.requests.get")
def test_get_S3_URI_in_data_portal(self, mock_get):
s3_uri = f"{FIXTURES_ROOT}/pbmc3k.cxg"
response_body = json.dumps(
{
"collection_id": "4f098ff4-4a12-446b-a841-91ba3d8e3fa6",
"collection_visibility": "PUBLIC",
"dataset_id": "2fa37b10-ab4d-49c9-97a8-b4b3d80bf939",
"s3_uri": s3_uri,
"tombstoned": False,
}
)
mock_get.return_value = MockResponse(body=response_body, status_code=200)

result = self.client.get(self.url)
self.assertEqual(result.status_code, HTTPStatus.OK)
self.assertEqual(json.loads(result.data), s3_uri)

@patch("server.data_common.dataset_metadata.requests.get")
def test_get_S3_URI_in_data_portal_format(self, mock_get):
s3_uri = f"{FIXTURES_ROOT}/pbmc3k.cxg/"
response_body = json.dumps(
{
"collection_id": "4f098ff4-4a12-446b-a841-91ba3d8e3fa6",
"collection_visibility": "PUBLIC",
"dataset_id": "2fa37b10-ab4d-49c9-97a8-b4b3d80bf939",
"s3_uri": s3_uri,
"tombstoned": False,
}
)
mock_get.return_value = MockResponse(body=response_body, status_code=200)

result = self.client.get(self.url)
self.assertEqual(result.status_code, HTTPStatus.OK)
self.assertEqual(json.loads(result.data), s3_uri[:-1])
test_s3_uris = [
(f"{FIXTURES_ROOT}/pbmc3k.cxg", f"{FIXTURES_ROOT}/pbmc3k.cxg"),
(f"{FIXTURES_ROOT}/pbmc3k.cxg/", f"{FIXTURES_ROOT}/pbmc3k.cxg"),
(None, None),
]
test_response_body = {
"collection_id": "4f098ff4-4a12-446b-a841-91ba3d8e3fa6",
"collection_visibility": "PUBLIC",
"dataset_id": "2fa37b10-ab4d-49c9-97a8-b4b3d80bf939",
"tombstoned": False,
}
for actual, expected in test_s3_uris:
with self.subTest(actual):
test_response_body["s3_uri"] = actual
response_body = json.dumps(test_response_body)
mock_get.return_value = MockResponse(body=response_body, status_code=200)

result = self.client.get(self.url)
self.assertEqual(result.status_code, HTTPStatus.OK)
self.assertEqual(json.loads(result.data), expected)

@patch("server.data_common.dataset_metadata.requests.get")
def test_get_S3_URI_not_in_data_portal(self, mock_get):
Expand Down