Skip to content
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

satsearch.Search error with headers #119

Open
spatialtrail opened this issue May 7, 2021 · 1 comment
Open

satsearch.Search error with headers #119

spatialtrail opened this issue May 7, 2021 · 1 comment

Comments

@spatialtrail
Copy link

Hi,

I am trying to parse an oauth token to satsearch query with custom headers but am getting an error.

Following the sentinel-hub docs to get my token: https://docs.sentinel-hub.com/api/latest/api/overview/authentication/#python

headers = {"Authorization": f"Bearer {token['access_token']}"}
items = satsearch.Search(
    url="https://services.sentinel-hub.com/api/v1/catalog",
    headers=headers,
    intersects=dict(type="Point", coordinates=[-39.30, 174.05]),
    collections=["sentinel-2-l2a"],
    datetime="2021-01-01/2021-02-01"
).items()

Traceback:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<timed exec> in <module>

/opt/conda/lib/python3.8/site-packages/satsearch/search.py in items(self, limit, page_limit, headers)
     88     def items(self, limit=10000, page_limit=500, headers=None):
     89         """ Return all of the Items and Collections for this search """
---> 90         found = self.found(headers=headers)
     91         limit = self.limit or limit
     92         if found > limit:

/opt/conda/lib/python3.8/site-packages/satsearch/search.py in found(self, headers)
     60         url = urljoin(self.url, 'search')
     61 
---> 62         results = self.query(url=url, headers=headers, **kwargs)
     63         # TODO - check for status_code
     64         logger.debug(f"Found: {json.dumps(results)}")

TypeError: query() got multiple values for keyword argument 'headers'

Trying header instead of headers I get:
SatSearchError: {"code":401,"description":"You are not authorized - please provide header [Authorization: Bearer <accessToken>] with your request."}

@spatialtrail
Copy link
Author

Sorry I realise I was putting my headers in the wrong place. This almost works:

items = satsearch.Search(
    url="https://services.sentinel-hub.com/api/v1/catalog",
    intersects=dict(type="Point", coordinates=[-39.30, 174.05]),
    collections=["sentinel-2-l2a"],
    datetime='2021-01-01T00:00:00Z/2021-02-01T00:00:00Z',
    limit=100
).items(headers=headers,page_limit=100)

But gives me an error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-3-367c480f9e36> in <module>
     26 # dates = f"{month_ago.strftime('%Y-%m-%d')}/{now.strftime('%Y-%m-%d')}"
     27 
---> 28 items = satsearch.Search(
     29     url="https://services.sentinel-hub.com/api/v1/catalog",
     30     intersects=dict(type="Point", coordinates=[lon, lat]),

/opt/conda/lib/python3.8/site-packages/satsearch/search.py in items(self, limit, page_limit, headers)
     88     def items(self, limit=10000, page_limit=500, headers=None):
     89         """ Return all of the Items and Collections for this search """
---> 90         found = self.found(headers=headers)
     91         limit = self.limit or limit
     92         if found > limit:

/opt/conda/lib/python3.8/site-packages/satsearch/search.py in found(self, headers)
     65         found = 0
     66         if 'context' in results:
---> 67             found = results['context']['matched']
     68         elif 'numberMatched' in results:
     69             found = results['numberMatched']

KeyError: 'matched'

https://github.com/sat-utils/sat-search/blob/master/satsearch/search.py#L67
Changing 'matched' to 'returned' gets me what I want though. Not sure if its because this STAC endpoint I am using is 0.9?

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

No branches or pull requests

1 participant