Skip to content

Commit

Permalink
Restore use of session for CMR paged queries
Browse files Browse the repository at this point in the history
Use of the session during CMR paged queries was accidentally removed
with the introduction of Search-After functionality.
  • Loading branch information
chuckwondo committed Apr 15, 2024
1 parent 3a88746 commit 4a6586f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 6 additions & 4 deletions earthaccess/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@


def get_results(
query: Union[CollectionQuery, GranuleQuery], limit: int = 2000
session: requests.Session,
query: Union[CollectionQuery, GranuleQuery],
limit: int = 2000,
) -> List[Any]:
"""
Get all results up to some limit, even if spanning multiple pages.
Expand Down Expand Up @@ -55,7 +57,7 @@ def get_results(
headers = dict(query.headers or {})

while more_results:
response = requests.get(url, headers=headers, params={"page_size": page_size})
response = session.get(url, headers=headers, params={"page_size": page_size})

if cmr_search_after := response.headers.get("cmr-search-after"):
headers["cmr-search-after"] = cmr_search_after
Expand Down Expand Up @@ -152,7 +154,7 @@ def get(self, limit: int = 2000) -> List[DataCollection]:

return [
DataCollection(collection, self._fields)
for collection in get_results(self, limit)
for collection in get_results(self.session, self, limit)
]

@override
Expand Down Expand Up @@ -516,7 +518,7 @@ def get(self, limit: int = 2000) -> List[DataGranule]:
Raises:
RuntimeError: The CMR query failed.
"""
response = get_results(self, limit)
response = get_results(self.session, self, limit)

cloud = self._is_cloud_hosted(response[0])

Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import earthaccess
from earthaccess.search import DataCollections

from vcr.unittest import VCRTestCase # type: ignore[import-untyped]

logging.basicConfig()
Expand Down Expand Up @@ -37,6 +38,7 @@ def _get_vcr(self, **kwargs):
myvcr.filter_headers = [
"Accept-Encoding",
"Authorization",
"Cookie",
"Set-Cookie",
"User-Agent",
]
Expand Down

0 comments on commit 4a6586f

Please sign in to comment.