Skip to content

Commit

Permalink
HARMONY-1993: Allow users to set custom labels for harmony requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-durbin committed Jan 29, 2025
1 parent c530649 commit d770dfd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
9 changes: 7 additions & 2 deletions harmony/harmony.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ class Request(BaseRequest):
grid: The name of the output grid to use for regridding requests. The name must
match the UMM grid name in the CMR.
labels: The list of labels to include for the request. By default a 'harmony-py'
label is added to all requests unless the environment variable EXCLUDE_DEFAULT_LABEL
is set to 'true'.
Returns:
A Harmony Transformation Request instance
"""
Expand Down Expand Up @@ -681,6 +685,7 @@ def _cloud_access_url(self) -> str:
def _params(self, request: BaseRequest) -> dict:
"""Creates a dictionary of request query parameters from the given request."""
params = {}
skipped_params = ['shapefile']
if not isinstance(request, CapabilitiesRequest):
if request.is_edr_request():
params['forceAsync'] = True
Expand All @@ -701,11 +706,11 @@ def _params(self, request: BaseRequest) -> dict:
if len(subset) > 0:
params['subset'] = subset
if (os.getenv('EXCLUDE_DEFAULT_LABEL') != 'true'):
labels = getattr(params, 'labels', [])
labels = request.labels or []
labels.append(DEFAULT_JOB_LABEL)
params['label'] = labels
skipped_params.append('label')

skipped_params = ['shapefile']
query_params = [pv for pv in request.parameter_values() if pv[0] not in skipped_params]
for p, val in query_params:
if isinstance(val, str):
Expand Down
14 changes: 7 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ classifiers = [
]
dynamic = ["version"]
dependencies = [
"python-dateutil ~= 2.8.2",
"python-dateutil ~= 2.9",
"python-dotenv ~= 0.20.0",
"progressbar2 ~= 4.2.0",
"requests ~= 2.32.3",
Expand All @@ -45,15 +45,15 @@ Repository = "https://github.com/nasa/harmony-py.git"

[project.optional-dependencies]
dev = [
"coverage ~= 5.4",
"coverage ~= 7.4",
"flake8 ~= 7.1.1",
"hypothesis ~= 6.2",
"hypothesis ~= 6.103",
"PyYAML ~= 6.0.1",
"pytest ~= 6.2",
"pytest-cov ~= 2.11",
"pytest-mock ~= 3.5",
"pytest ~= 8.2",
"pytest-cov ~= 5.0",
"pytest-mock ~= 3.14",
"pytest-watch ~= 4.2",
"responses ~= 0.25.3"
"responses ~= 0.25.6"
]
docs = [
"curlify ~= 2.2.1",
Expand Down
27 changes: 25 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,34 @@ def test_post_request_has_default_label(examples_dir):
assert label == DEFAULT_JOB_LABEL

@responses.activate
def test_post_request_can_skip_default_label(examples_dir):
def test_user_labels_and_default_label(examples_dir):
collection = Collection('foobar')
request = Request(
collection=collection,
shape=os.path.join(examples_dir, 'asf_example.json'),
spatial=BBox(-107, 40, -105, 42),
labels=['one', 'two'],
)
responses.add(
responses.POST,
expected_submit_url(collection.id),
status=200,
json=expected_job(collection.id, 'abcd-1234'),
)

Client(should_validate_auth=False).submit(request)
form_data_params = parse_multipart_data(responses.calls[0].request)
label = form_data_params['label']
assert label == ['one', 'two', DEFAULT_JOB_LABEL]

@responses.activate
def test_user_labels_and_no_default_label(examples_dir):
collection = Collection('foobar')
request = Request(
collection=collection,
shape=os.path.join(examples_dir, 'asf_example.json'),
spatial=BBox(-107, 40, -105, 42),
labels=['one', 'two'],
)
responses.add(
responses.POST,
Expand All @@ -564,7 +586,8 @@ def test_post_request_can_skip_default_label(examples_dir):

Client(should_validate_auth=False).submit(request)
form_data_params = parse_multipart_data(responses.calls[0].request)
assert 'label' not in form_data_params
label = form_data_params['label']
assert label == ['one', 'two']

os.environ['EXCLUDE_DEFAULT_LABEL'] = origninal_exclude_label or ''

Expand Down

0 comments on commit d770dfd

Please sign in to comment.