Skip to content

Commit

Permalink
HARMONY-1721: Send all granuleId parameters as a single comma-separat…
Browse files Browse the repository at this point in the history
…ed string to avoid creating too many file boundaries in the multipart form post.
  • Loading branch information
chris-durbin committed Mar 14, 2024
1 parent 40b6e30 commit 0338963
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
19 changes: 9 additions & 10 deletions harmony/harmony.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,25 +685,24 @@ def _files(self, request) -> ContextManager[Mapping[str, Any]]:

def _params_dict_to_files(self, params: Mapping[str, Any]) -> List[Tuple[None, str, None]]:
"""Returns the given parameter mapping as a list of tuples suitable for multipart POST
This method is a temporary need until HARMONY-290 is implemented, since we cannot
currently pass query parameters to a shapefile POST request. Because we need to pass
them as a multipart POST body, we need to inflate them into a list of tuples, one for
each parameter value to allow us to call requests.post(..., files=params)
Args:
params: A dictionary of parameter mappings as returned by self._params(request)
Returns:
A list of tuples suitable for passing to a requests multipart upload corresponding
to the provided parameters
"""
# TODO: remove / refactor after HARMONY-290 is complete
result = []
for key, values in params.items():
if not isinstance(values, list):
values = [values]
result += [(key, (None, str(value), None)) for value in values]
if key == 'granuleId' and isinstance(values, list):
# Include all granuleId values in a single file boundary as a comma separated
# list to avoid creating too many file boundaries
concatenated_values = ','.join(map(str, values))
result.append((key, (None, concatenated_values, None)))
else:
if not isinstance(values, list):
values = [values]
result += [(key, (None, str(value), None)) for value in values]
return result

def _get_prepared_request(self, request: BaseRequest) -> requests.models.PreparedRequest:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ def test_post_request_has_user_agent_headers(examples_dir):
({'crs': 'epsg:3141'}, 'outputcrs=epsg:3141'),
({'destination_url': 's3://bucket'}, 'destinationUrl=s3://bucket'),
({'format': 'r2d2/hologram'}, 'format=r2d2/hologram'),
({'granule_id': ['G1', 'G2', 'G3']}, 'granuleId=G1&granuleId=G2&granuleId=G3'),
({'granule_id': ['G1', 'G2', 'G3']}, 'granuleId=G1,G2,G3'),
({'granule_name': ['abc*123', 'ab?d123', 'abcd123']},
'granuleName=abc*123&granuleName=ab?d123&granuleName=abcd123'),
({'height': 200}, 'height=200'),
Expand Down

0 comments on commit 0338963

Please sign in to comment.