From fadcb2901e2d6197b1dc4bad720adc2fcc52868b 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 363164131..b9f740eab 100644 --- a/icepyx/core/granules.py +++ b/icepyx/core/granules.py @@ -10,7 +10,6 @@ import os import pprint import zipfile -from typing import Final from requests.compat import unquote from xml.etree import ElementTree as ET @@ -18,6 +17,7 @@ import icepyx.core.exceptions from icepyx.core.auth import EarthdataAuthMixin from icepyx.core.types import CMRParams, EGISpecificParams +from icepyx.core.urls import DOWNLOAD_BASE_URL, ORDER_BASE_URL, GRANULE_SEARCH_BASE_URL def info(grans): @@ -211,8 +211,6 @@ def get_avail( # if not hasattr(self, 'avail'): self.avail = [] - granule_search_url: Final = "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 @@ -230,7 +228,7 @@ def get_avail( headers["CMR-Search-After"] = cmr_search_after response = requests.get( - granule_search_url, + GRANULE_SEARCH_BASE_URL, headers=headers, params=apifmt.to_string(params), ) @@ -317,7 +315,6 @@ def place_order( -------- query.Query.order_granules """ - base_url: Final = "https://n5eil02u.ecs.nsidc.org/egi/request" self.get_avail(CMRparams, reqparams) @@ -354,7 +351,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 @@ -386,7 +383,7 @@ def place_order( print("order ID: ", orderID) # Create status URL - statusURL = base_url + "/" + orderID + statusURL = ORDER_BASE_URL + "/" + orderID if verbose is True: print("status URL: ", statusURL) @@ -539,7 +536,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"