sat-utils / sat-search

A python client for sat-api
MIT License
188 stars 43 forks source link

satsearch.Search error with headers #119

Open spatialtrail opened 3 years ago

spatialtrail commented 3 years ago

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 commented 3 years ago

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?