Skip to content

Commit

Permalink
In DataGranules, override hits() method so that session authenticatio…
Browse files Browse the repository at this point in the history
…n is used
  • Loading branch information
danielfromearth committed Nov 21, 2023
1 parent f48f630 commit c059ad5
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions earthaccess/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,25 @@ def __init__(self, auth: Any = None, *args: Any, **kwargs: Any) -> None:

self._debug = False

def hits(self):
"""
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.
Expand Down

0 comments on commit c059ad5

Please sign in to comment.