diff --git a/CHANGELOG.md b/CHANGELOG.md index d7fd3a1e..6aefdb21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## [unreleased] +* Fix zero granules being reported for restricted datasets + ## [v0.7.1] 2023-11-08 * Bug Fixes: * Treat granules without `RelatedUrls` as not cloud-hosted. diff --git a/earthaccess/search.py b/earthaccess/search.py index 3fd70684..0ed3b61d 100644 --- a/earthaccess/search.py +++ b/earthaccess/search.py @@ -58,7 +58,7 @@ def hits(self) -> int: Returns: - number of results reproted by CMR + number of results reported by CMR """ return super().hits() @@ -318,6 +318,25 @@ def __init__(self, auth: Any = None, *args: Any, **kwargs: Any) -> None: self._debug = False + def hits(self) -> int: + """ + Returns the number of hits the current query will return. This is done by + making a lightweight query to CMR and inspecting the returned headers. + + :returns: number of results reported by CMR + """ + + url = self._build_url() + + response = self.session.get(url, headers=self.headers, params={"page_size": 0}) + + try: + response.raise_for_status() + except exceptions.HTTPError as ex: + raise RuntimeError(ex.response.text) + + return int(response.headers["CMR-Hits"]) + def parameters(self, **kwargs: Any) -> Type[CollectionQuery]: """Provide query parameters as keyword arguments. The keyword needs to match the name of the method, and the value should either be the value or a tuple of values.