Skip to content

Fix recursive search in Client.get_items #799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

mishaschwartz
Copy link

@mishaschwartz mishaschwartz commented May 14, 2025

Related Issue(s):

Description:

  • runs a non-recursive search when using the recursive argument is False
  • no longer fails when using /search endpoint of an API that requires that at least one collection be passed as an argument (e.g. https://planetarycomputer.microsoft.com/api/stac/v1/)
  • updates tests

PR Checklist:

  • Code is formatted
  • Tests pass
  • Changes are added to CHANGELOG.md

@jsignell jsignell self-requested a review May 15, 2025 13:07
@jsignell jsignell self-assigned this May 15, 2025
Copy link
Member

@jsignell jsignell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few suggestions, but thank you so much for opening this PR! I think it'll be a real improvement.

Comment on lines 465 to 467
except APIError:
child_catalogs = [catalog for catalog, _, _ in self.walk()]
search = self.search(ids=ids, collections=[self, *child_catalogs])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it would be pretty easy to do accidentally. I think I'd prefer to just let the error raise and make it a little harder to get every single item in planetary computer for instance.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern is that without something like this, many functions that call get_items simply don't work for planetary computer or similar APIs that enforce this required argument. This includes:

  • Client.get_all_items,
  • Client.walk,
  • Client.validate_all,
  • Client.describe,
  • Client.make_all_asset_hrefs_relative,
  • Client.make_all_asset_hrefs_absolute

Note that the spec doesn't say one way or another that these arguments must be optional so I'm guessing that planetary computer's API is still spec compliant technically. However, the examples show that a search without collections should be supported so I don't really know one way or the other how to interpret that:

https://github.com/radiantearth/stac-api-spec/blob/604ade6158de15b8ab068320ca41e25e2bf0e116/item-search/examples.md?plain=1#L27

Otherwise the only way to make this work for APIs like planetary computer is to override the Client class like:

import pystac_client

class Client(pystac_client.Client):
    def search(self, *args, **kwargs):
        if kwargs["collections"] is None:
            kwargs["collections"] = [self.id *[catalog.id for catalog, _, _ in self.walk()]]
        return super().search(*args, **kwargs)

pystac_client.client.Client = Client  # so that sub-catalogs also use the updated search method 

If that's the approach we want to go with that's fine, but maybe we should document this workaround in case users want to interact with planetary computer.

What do you think?

@jsignell jsignell removed their assignment May 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Client.get_items has surprising recursive behaviour when using the /search endpoint
2 participants