diff --git a/earthaccess/api.py b/earthaccess/api.py index fd6d857b..cc9c5a17 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -68,11 +68,14 @@ def search_datasets( ) return [] if earthaccess.__auth__.authenticated: - query = DataCollections(auth=earthaccess.__auth__, - earthdata_environment=earthaccess.__auth__.earthdata_environment).parameters(**kwargs) + query = DataCollections( + auth=earthaccess.__auth__, + earthdata_environment=earthaccess.__auth__.earthdata_environment, + ).parameters(**kwargs) else: query = DataCollections( - earthdata_environment=earthaccess.__auth__.earthdata_environment).parameters(**kwargs) + earthdata_environment=earthaccess.__auth__.earthdata_environment + ).parameters(**kwargs) datasets_found = query.hits() print(f"Datasets found: {datasets_found}") if count > 0: @@ -116,11 +119,14 @@ def search_data( ``` """ if earthaccess.__auth__.authenticated: - query = DataGranules(earthaccess.__auth__, - earthdata_environment=earthaccess.__auth__.earthdata_environment).parameters(**kwargs) + query = DataGranules( + earthaccess.__auth__, + earthdata_environment=earthaccess.__auth__.earthdata_environment, + ).parameters(**kwargs) else: query = DataGranules( - earthdata_environment=earthaccess.__auth__.earthdata_environment).parameters(**kwargs) + earthdata_environment=earthaccess.__auth__.earthdata_environment + ).parameters(**kwargs) granules_found = query.hits() print(f"Granules found: {granules_found}") if count > 0: @@ -128,7 +134,9 @@ def search_data( return query.get_all() -def login(strategy: str = "all", persist: bool = False, earthdata_environment=Env.PROD) -> Auth: +def login( + strategy: str = "all", persist: bool = False, earthdata_environment=Env.PROD +) -> Auth: """Authenticate with Earthdata login (https://urs.earthdata.nasa.gov/). Parameters: @@ -148,7 +156,11 @@ def login(strategy: str = "all", persist: bool = False, earthdata_environment=En if strategy == "all": for strategy in ["environment", "netrc", "interactive"]: try: - earthaccess.__auth__.login(strategy=strategy, persist=persist, earthdata_environment=earthdata_environment) + earthaccess.__auth__.login( + strategy=strategy, + persist=persist, + earthdata_environment=earthdata_environment, + ) except Exception: pass @@ -156,7 +168,11 @@ def login(strategy: str = "all", persist: bool = False, earthdata_environment=En earthaccess.__store__ = Store(earthaccess.__auth__) break else: - earthaccess.__auth__.login(strategy=strategy, persist=persist, earthdata_environment=earthdata_environment) + earthaccess.__auth__.login( + strategy=strategy, + persist=persist, + earthdata_environment=earthdata_environment, + ) if earthaccess.__auth__.authenticated: earthaccess.__store__ = Store(earthaccess.__auth__) @@ -251,11 +267,14 @@ def collection_query() -> Type[CollectionQuery]: a query builder instance for data collections. """ if earthaccess.__auth__.authenticated: - query_builder = DataCollections(earthaccess.__auth__, - earthdata_environment=earthaccess.__auth__.earthdata_environment) + query_builder = DataCollections( + earthaccess.__auth__, + earthdata_environment=earthaccess.__auth__.earthdata_environment, + ) else: query_builder = DataCollections( - earthdata_environment=earthaccess.__auth__.earthdata_environment) + earthdata_environment=earthaccess.__auth__.earthdata_environment + ) return query_builder @@ -266,11 +285,14 @@ def granule_query() -> Type[GranuleQuery]: a query builder instance for data granules. """ if earthaccess.__auth__.authenticated: - query_builder = DataGranules(earthaccess.__auth__, - earthdata_environment=earthaccess.__auth__.earthdata_environment) + query_builder = DataGranules( + earthaccess.__auth__, + earthdata_environment=earthaccess.__auth__.earthdata_environment, + ) else: query_builder = DataGranules( - earthdata_environment=earthaccess.__auth__.earthdata_environment) + earthdata_environment=earthaccess.__auth__.earthdata_environment + ) return query_builder diff --git a/earthaccess/auth.py b/earthaccess/auth.py index 3f61f2f2..7e387bf6 100644 --- a/earthaccess/auth.py +++ b/earthaccess/auth.py @@ -26,10 +26,12 @@ class Env(Enum): """ Host URL options, for different Earthdata domains. """ + PROD = "urs.earthdata.nasa.gov" UAT = "uat.urs.earthdata.nasa.gov" SIT = "sit.urs.earthdata.nasa.gov" + class SessionWithHeaderRedirection(requests.Session): """ Requests removes auth headers if the redirect happens outside the @@ -82,8 +84,12 @@ def __init__(self) -> None: self.tokens: List = [] self._set_earthdata_environment(Env.PROD) - def login(self, strategy: str = "netrc", persist: bool = False, - earthdata_environment: Optional[Env] = None) -> Any: + def login( + self, + strategy: str = "netrc", + persist: bool = False, + earthdata_environment: Optional[Env] = None, + ) -> Any: """Authenticate with Earthdata login. Parameters: @@ -119,13 +125,21 @@ def _set_earthdata_environment(self, earthdata_environment: Env) -> None: self.earthdata_environment = earthdata_environment # Maybe all these predefined URLs should be in a constants.py file - self.EDL_GET_TOKENS_URL = f"https://{self.earthdata_environment.value}/api/users/tokens" + self.EDL_GET_TOKENS_URL = ( + f"https://{self.earthdata_environment.value}/api/users/tokens" + ) self.EDL_GET_PROFILE = f"https://{self.earthdata_environment.value}/api/users/?client_id=ntD0YGC_SM3Bjs-Tnxd7bg" - self.EDL_GENERATE_TOKENS_URL = f"https://{self.earthdata_environment.value}/api/users/token" - self.EDL_REVOKE_TOKEN = f"https://{self.earthdata_environment.value}/api/users/revoke_token" + self.EDL_GENERATE_TOKENS_URL = ( + f"https://{self.earthdata_environment.value}/api/users/token" + ) + self.EDL_REVOKE_TOKEN = ( + f"https://{self.earthdata_environment.value}/api/users/revoke_token" + ) self._eula_url = f"https://{self.earthdata_environment.value}/users/earthaccess/unaccepted_eulas" - self._apps_url = f"https://{self.earthdata_environment.value}/application_search" + self._apps_url = ( + f"https://{self.earthdata_environment.value}/application_search" + ) def refresh_tokens(self) -> bool: """Refresh CMR tokens. @@ -196,7 +210,9 @@ def get_s3_credentials( A Python dictionary with the temporary AWS S3 credentials. """ if self.authenticated: - session = SessionWithHeaderRedirection(self.username, self.password, self.earthdata_environment) + session = SessionWithHeaderRedirection( + self.username, self.password, self.earthdata_environment + ) if endpoint is None: auth_url = self._get_cloud_auth_url( daac_shortname=daac, provider=provider @@ -346,7 +362,9 @@ def _get_credentials( return self.authenticated def _get_user_tokens(self, username: str, password: str) -> Any: - session = SessionWithHeaderRedirection(username, password, self.earthdata_environment) + session = SessionWithHeaderRedirection( + username, password, self.earthdata_environment + ) auth_resp = session.get( self.EDL_GET_TOKENS_URL, headers={ @@ -357,7 +375,9 @@ def _get_user_tokens(self, username: str, password: str) -> Any: return auth_resp def _generate_user_token(self, username: str, password: str) -> Any: - session = SessionWithHeaderRedirection(username, password, self.earthdata_environment) + session = SessionWithHeaderRedirection( + username, password, self.earthdata_environment + ) auth_resp = session.post( self.EDL_GENERATE_TOKENS_URL, headers={ @@ -369,7 +389,9 @@ def _generate_user_token(self, username: str, password: str) -> Any: def _revoke_user_token(self, token: str) -> bool: if self.authenticated: - session = SessionWithHeaderRedirection(self.username, self.password, self.earthdata_environment) + session = SessionWithHeaderRedirection( + self.username, self.password, self.earthdata_environment + ) auth_resp = session.post( self.EDL_REVOKE_TOKEN, params={"token": token}, @@ -392,7 +414,10 @@ def _persist_user_credentials(self, username: str, password: str) -> bool: print(e) return False my_netrc = Netrc(str(netrc_path)) - my_netrc[self.earthdata_environment.value] = {"login": username, "password": password} + my_netrc[self.earthdata_environment.value] = { + "login": username, + "password": password, + } my_netrc.save() return True diff --git a/earthaccess/search.py b/earthaccess/search.py index 7ea34d78..ea574239 100644 --- a/earthaccess/search.py +++ b/earthaccess/search.py @@ -33,7 +33,13 @@ class DataCollections(CollectionQuery): "umm_json", ] - def __init__(self, auth: Optional[Auth] = None, earthdata_environment: Optional[Env] = None, *args: Any, **kwargs: Any) -> None: + def __init__( + self, + auth: Optional[Auth] = None, + earthdata_environment: Optional[Env] = None, + *args: Any, + **kwargs: Any, + ) -> None: """Builds an instance of DataCollections to query CMR Parameters: @@ -53,7 +59,9 @@ def __init__(self, auth: Optional[Auth] = None, earthdata_environment: Optional[ self.mode(CMR_SIT) print(f"[in DataCollections] Earthdata environment: {earthdata_environment}") - print(f"[in DataCollections] earthdata_environment == Env.PROD -----> {earthdata_environment == Env.PROD}") + print( + f"[in DataCollections] earthdata_environment == Env.PROD -----> {earthdata_environment == Env.PROD}" + ) if auth is not None and auth.authenticated: # To search, we need the new bearer tokens from NASA Earthdata @@ -378,7 +386,13 @@ class DataGranules(GranuleQuery): "umm_json", ] - def __init__(self, auth: Any = None, earthdata_environment: Optional[Env] = None, *args: Any, **kwargs: Any) -> None: + def __init__( + self, + auth: Any = None, + earthdata_environment: Optional[Env] = None, + *args: Any, + **kwargs: Any, + ) -> None: """Base class for Granule and Collection CMR queries.""" super().__init__(*args, **kwargs) self.session = session() @@ -393,7 +407,9 @@ def __init__(self, auth: Any = None, earthdata_environment: Optional[Env] = None self.mode(CMR_SIT) print(f"[in DataGranules] Earthdata environment: {earthdata_environment}") - print(f"[in DataGranules] earthdata_environment == Env.PROD -----> {earthdata_environment == Env.PROD}") + print( + f"[in DataGranules] earthdata_environment == Env.PROD -----> {earthdata_environment == Env.PROD}" + ) if auth is not None and auth.authenticated: # To search, we need the new bearer tokens from NASA Earthdata