From 2dc7e7856d9a7dd12956d4baa11754a0fda594f6 Mon Sep 17 00:00:00 2001 From: Matt Fisher Date: Mon, 26 Aug 2024 16:44:26 -0600 Subject: [PATCH] Extract URLs to own module --- icepyx/core/granules.py | 13 +++++-------- icepyx/core/is2ref.py | 9 ++++----- icepyx/core/urls.py | 10 ++++++++++ 3 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 icepyx/core/urls.py diff --git a/icepyx/core/granules.py b/icepyx/core/granules.py index 2c51e208b..ed7260794 100644 --- a/icepyx/core/granules.py +++ b/icepyx/core/granules.py @@ -13,6 +13,7 @@ import icepyx.core.APIformatting as apifmt from icepyx.core.auth import EarthdataAuthMixin import icepyx.core.exceptions +from icepyx.core.urls import DOWNLOAD_BASE_URL, ORDER_BASE_URL, GRANULE_SEARCH_BASE_URL def info(grans): @@ -201,8 +202,6 @@ def get_avail(self, CMRparams, reqparams, cloud=False): # if not hasattr(self, 'avail'): self.avail = [] - granule_search_url = "https://cmr.earthdata.nasa.gov/search/granules" - headers = {"Accept": "application/json", "Client-Id": "icepyx"} # note we should also check for errors whenever we ping NSIDC-API - # make a function to check for errors @@ -220,7 +219,7 @@ def get_avail(self, CMRparams, reqparams, cloud=False): headers["CMR-Search-After"] = cmr_search_after response = requests.get( - granule_search_url, + GRANULE_SEARCH_BASE_URL, headers=headers, params=apifmt.to_string(params), ) @@ -308,8 +307,6 @@ def place_order( query.Query.order_granules """ - base_url = "https://n5eil02u.ecs.nsidc.org/egi/request" - self.get_avail(CMRparams, reqparams) if subset is False: @@ -345,7 +342,7 @@ def place_order( ) request_params.update({"page_num": page_num}) - request = self.session.get(base_url, params=request_params) + request = self.session.get(ORDER_BASE_URL, params=request_params) # DevGoal: use the request response/number to do some error handling/ # give the user better messaging for failures @@ -377,7 +374,7 @@ def place_order( print("order ID: ", orderID) # Create status URL - statusURL = base_url + "/" + orderID + statusURL = f"{ORDER_BASE_URL}/{orderID}" if verbose is True: print("status URL: ", statusURL) @@ -522,7 +519,7 @@ def download(self, verbose, path, restart=False): i_order = self.orderIDs.index(order_start) + 1 for order in self.orderIDs[i_order:]: - downloadURL = "https://n5eil02u.ecs.nsidc.org/esir/" + order + ".zip" + downloadURL = f"{DOWNLOAD_BASE_URL}/{order}.zip" # DevGoal: get the download_url from the granules if verbose is True: diff --git a/icepyx/core/is2ref.py b/icepyx/core/is2ref.py index be3a3c8da..2d20019c3 100644 --- a/icepyx/core/is2ref.py +++ b/icepyx/core/is2ref.py @@ -7,6 +7,8 @@ import earthaccess +from icepyx.core.urls import COLLECTION_SEARCH_BASE_URL, EGI_BASE_URL + # ICESat-2 specific reference functions @@ -82,8 +84,7 @@ def about_product(prod): query.Query.product_all_info """ - cmr_collections_url = "https://cmr.earthdata.nasa.gov/search/collections.json" - response = requests.get(cmr_collections_url, params={"short_name": prod}) + response = requests.get(COLLECTION_SEARCH_BASE_URL, params={"short_name": prod}) results = json.loads(response.content) return results @@ -101,9 +102,7 @@ def _get_custom_options(session, product, version): "Don't forget to log in to Earthdata using query.earthdata_login()" ) - capability_url = ( - f"https://n5eil02u.ecs.nsidc.org/egi/capabilities/{product}.{version}.xml" - ) + capability_url = f"{EGI_BASE_URL}/capabilities/{product}.{version}.xml" response = session.get(capability_url) root = ET.fromstring(response.content) diff --git a/icepyx/core/urls.py b/icepyx/core/urls.py new file mode 100644 index 000000000..8c5bc325b --- /dev/null +++ b/icepyx/core/urls.py @@ -0,0 +1,10 @@ +from typing import Final + +CMR_BASE_URL: Final = "https://cmr.earthdata.nasa.gov" +GRANULE_SEARCH_BASE_URL: Final = f"{CMR_BASE_URL}/search/granules" +COLLECTION_SEARCH_BASE_URL: Final = f"{CMR_BASE_URL}/search/collections.json" + +EGI_BASE_URL: Final = "https://n5eil02u.ecs.nsidc.org/egi" +ORDER_BASE_URL: Final = f"{EGI_BASE_URL}/request" + +DOWNLOAD_BASE_URL: Final = "https://n5eil02u.ecs.nsidc.org/esir"