Skip to content

Commit 740c37c

Browse files
authored
Merge pull request #83 from nasa/harmony-1752
Harmony 1752
2 parents d6b20c1 + 0c70317 commit 740c37c

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

examples/tutorial.ipynb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"import sys; sys.path.append('..')\n",
2525
"!{sys.executable} -m pip install -q -r ../requirements/examples.txt\n",
2626
"\n",
27-
"# Install harmony-py requirements. Not necessary if you ran `pip install harmony-py` in your kernel \n",
27+
"# Install harmony-py requirements. Not necessary if you ran `pip install harmony-py` in your kernel\n",
2828
"!{sys.executable} -m pip install -q -r ../requirements/core.txt\n",
2929
"\n",
3030
"import datetime as dt\n",
@@ -174,12 +174,31 @@
174174
" rasterio.plot.show(rasterio.open(r.result()))"
175175
]
176176
},
177+
{
178+
"cell_type": "markdown",
179+
"id": "f97fa92f",
180+
"metadata": {},
181+
"source": [
182+
"We can also get a URL corresponding to our request that we can use in a browser. **Note:** This will not work if the request includes a shapefile."
183+
]
184+
},
185+
{
186+
"cell_type": "code",
187+
"execution_count": null,
188+
"id": "f03e22a8",
189+
"metadata": {},
190+
"outputs": [],
191+
"source": [
192+
"url = harmony_client.request_as_url(request)\n",
193+
"print(url)"
194+
]
195+
},
177196
{
178197
"cell_type": "markdown",
179198
"id": "45a14473",
180199
"metadata": {},
181200
"source": [
182-
"Let's build another request. This time, we'll request a specific granule and grid using the UMM grid name from the CMR. You can find grids in the CMR at https://cmr.uat.earthdata.nasa.gov/search/grids.umm_json. Include a query parameter `?name=LambertExample` to list detailed information about a specific grid."
201+
"Let's build another request. This time, we'll request a specific granule and grid using the UMM grid name from the CMR. You can find grids in the CMR at https://cmr.uat.earthdata.nasa.gov/search/grids.umm_json. Include a query parameter `?grid=LambertExample` to list detailed information about a specific grid."
183202
]
184203
},
185204
{
@@ -317,7 +336,7 @@
317336
"name": "python",
318337
"nbconvert_exporter": "python",
319338
"pygments_lexer": "ipython3",
320-
"version": "3.11.2"
339+
"version": "3.11.9"
321340
},
322341
"vscode": {
323342
"interpreter": {

harmony/harmony.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,11 +706,13 @@ def _params_dict_to_files(self, params: Mapping[str, Any]) -> List[Tuple[None, s
706706
result += [(key, (None, str(value), None)) for value in values]
707707
return result
708708

709-
def _get_prepared_request(self, request: BaseRequest) -> requests.models.PreparedRequest:
709+
def _get_prepared_request(
710+
self, request: BaseRequest, for_browser=False) -> requests.models.PreparedRequest:
710711
"""Returns a :requests.models.PreparedRequest: object for the given harmony Request
711712
712713
Args:
713714
request: The Harmony Request to prepare
715+
for_browser: if True only the url with query params will be returned
714716
715717
Returns:
716718
A PreparedRequest
@@ -722,7 +724,7 @@ def _get_prepared_request(self, request: BaseRequest) -> requests.models.Prepare
722724

723725
method = self._http_method(request)
724726
with self._files(request) as files:
725-
if files or method == 'POST':
727+
if (files or method == 'POST') and not for_browser:
726728
# Ideally this should just be files=files, params=params but Harmony
727729
# cannot accept both files and query params now. (HARMONY-290)
728730
# Inflate params to a list of tuples that can be passed as multipart
@@ -742,17 +744,23 @@ def _get_prepared_request(self, request: BaseRequest) -> requests.models.Prepare
742744
files=all_files,
743745
headers=headers)
744746
else:
747+
if files:
748+
raise Exception("Cannot include shapefile as URL query parameter")
749+
745750
r = requests.models.Request('GET',
746751
self._submit_url(request),
747752
params=params,
748753
headers=headers)
754+
749755
prepped_request = session.prepare_request(r)
756+
if for_browser:
757+
prepped_request.headers = None
750758

751759
return prepped_request
752760

753761
def _handle_error_response(self, response: Response):
754762
"""Raises the appropriate exception based on the response
755-
received from Harmony. Trys to pull out an error message
763+
received from Harmony. Tries to pull out an error message
756764
from a Harmony JSON response when possible.
757765
758766
Args:
@@ -798,6 +806,21 @@ def request_as_curl(self, request: BaseRequest) -> str:
798806
prepped_request.prepare_cookies(cooks)
799807
return curlify.to_curl(prepped_request)
800808

809+
def request_as_url(self, request: BaseRequest) -> str:
810+
"""Returns a URL string representation of the given request.
811+
**Note** Headers and cookies are not included, just the URL.
812+
Shapefiles are not supported.
813+
814+
Args:
815+
request: The Request to build the URL string for
816+
817+
Returns:
818+
A URL string that can be pasted into a browser.
819+
:raises
820+
Exception: if a shapefile is included in the request.
821+
"""
822+
return self._get_prepared_request(request, for_browser=True).url
823+
801824
def submit(self, request: BaseRequest) -> any:
802825
"""Submits a request to Harmony and returns the Harmony Job ID.
803826

tests/test_client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,28 @@ def test_request_as_curl_post(examples_dir):
15021502
f'/ogc-api-coverages/1.0.0/collections/all/coverage/rangeset' in curl_command
15031503
assert '-X POST' in curl_command
15041504

1505+
def test_request_as_url():
1506+
collection = Collection(id='C1940468263-POCLOUD')
1507+
request = Request(
1508+
collection=collection,
1509+
spatial=BBox(-107, 40, -105, 42)
1510+
)
1511+
1512+
url = Client(should_validate_auth=False).request_as_url(request)
1513+
assert url == 'https://harmony.earthdata.nasa.gov/C1940468263-POCLOUD/ogc-api-coverages/1.0.0/collections/all/coverage/rangeset?forceAsync=true&subset=lat%2840%3A42%29&subset=lon%28-107%3A-105%29'
1514+
1515+
def test_request_with_shapefile_as_url(examples_dir):
1516+
collection = Collection(id='C1940468263-POCLOUD')
1517+
request = Request(
1518+
collection=collection,
1519+
shape=os.path.join(examples_dir, 'asf_example.json'),
1520+
spatial=BBox(-107, 40, -105, 42)
1521+
)
1522+
1523+
with pytest.raises(Exception) as e:
1524+
Client(should_validate_auth=False).request_as_url(request)
1525+
assert str(e.value) == "Cannot include shapefile as URL query parameter"
1526+
15051527
@responses.activate
15061528
def test_collection_capabilities():
15071529
collection_id='C1940468263-POCLOUD'

0 commit comments

Comments
 (0)