Skip to content

Commit a0375f5

Browse files
authored
Merge pull request #252 from raphaelrpl/bdc-catalog-v1
🐛 Temp fix for cube builder support for latest setuptools breaking changes
2 parents 2fcbe9d + 1e9d5a6 commit a0375f5

File tree

5 files changed

+42
-16
lines changed

5 files changed

+42
-16
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ ADD . ${CUBE_BUILDER_INSTALL_PATH}
3434

3535
WORKDIR ${CUBE_BUILDER_INSTALL_PATH}
3636

37-
RUN python3 -m pip install pip --upgrade setuptools wheel && \
37+
RUN python3 -m pip install pip --upgrade "setuptools<67" wheel && \
3838
python3 -m pip install -e .[rabbitmq] && \
3939
python3 -m pip install gunicorn
4040

INSTALL.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Compatibility
4646
+--------------+-------------+
4747
| Cube-Builder | BDC-Catalog |
4848
+==============+=============+
49-
| 1.0.x | 1.0.0 |
49+
| 1.0.x | 1.0.x |
5050
+--------------+-------------+
5151
| 0.8.x | 0.8.2 |
5252
+--------------+-------------+
@@ -79,7 +79,7 @@ Install in development mode:
7979

8080
.. code-block:: shell
8181
82-
$ pip3 install -U pip setuptools wheel
82+
$ pip3 install -U pip "setuptools<67" wheel
8383
$ pip3 install -e .[all]
8484
8585
@@ -88,6 +88,13 @@ Install in development mode:
8888
If you have problems with the ``librabbitmq`` installation, please, see [#f1]_.
8989

9090

91+
.. note::
92+
93+
The `setuptools v67+ <https://setuptools.pypa.io/en/latest/history.html>`_ has breaking changes related
94+
Pip versions requirements. For now, you should install ``setuptools<67`` for compatibility.
95+
The packages in ``Cube-Builder`` will be upgraded to support latest version.
96+
97+
9198
Running in Development Mode
9299
---------------------------
93100

cube_builder/_adapter.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
from abc import ABC, abstractmethod
2222
from copy import deepcopy
2323
from typing import List
24+
from urllib.parse import urljoin
2425

2526
import requests
2627
import shapely.geometry
2728
from pystac_client import Client
28-
from stac import STAC as _STAC
29+
from werkzeug.exceptions import abort
2930

3031

3132
class BaseSTAC(ABC):
@@ -119,27 +120,33 @@ def items(self, collection_id: str, **kwargs) -> dict:
119120
class STACLegacy(BaseSTAC):
120121
"""Define structure to add support for legacy versions of STAC server..
121122
122-
This implementation uses `stac.py <https://github.com/brazil-data-cube/stac.py>`_
123-
to communicate with STAC legacy versions 0.8x, 0.9x.
123+
This implementation uses `requests.Session <https://requests.readthedocs.io/en/latest/user/advanced/#session-objects>`_
124+
to communicate with STAC legacy versions 0.8x, 0.9x directly.
125+
126+
By default, the ssl entries are ignored. You may override this setting using ``verify=False``.
124127
"""
125128

126-
def __init__(self, uri: str, params=None, headers=None, **kwargs):
129+
def __init__(self, uri: str, params=None, headers=None, verify=False, **kwargs):
127130
"""Build STAC instance."""
128131
super(STACLegacy, self).__init__(uri, params, headers, **kwargs)
129132

130133
params = params or {}
131134
headers = headers or {}
132-
token = params.get('access_token') or (headers.get('x-api-key') or headers.get('X-Api-Key'))
133135

134-
self._instance = _STAC(uri, access_token=token)
136+
self._params = params
137+
self._headers = headers
138+
self._session = requests.session()
139+
self._session.verify = verify
135140

136141
def search(self, **parameters) -> dict:
137142
"""Search for collection items on STAC server."""
138143
options = deepcopy(parameters)
139144
# Remove unsupported values
140145
options.pop('query', None)
146+
url = self._url_resource('search')
147+
141148
try:
142-
return self._instance.search(filter=options)
149+
response = self._request(url, method='POST', data=options, headers=self._headers, params=self._params)
143150
except:
144151
# Use bbox instead
145152
geom = options.pop('intersects', None)
@@ -148,21 +155,35 @@ def search(self, **parameters) -> dict:
148155

149156
options['bbox'] = shapely.geometry.shape(geom).bounds
150157

151-
return self._instance.search(filter=options)
158+
response = self._request(url, method='POST', data=options, headers=self._headers, params=self._params)
159+
160+
return response
161+
162+
def _request(self, uri: str, method: str = 'GET', data=None, headers=None, params=None):
163+
response = self._session.request(method, uri, headers=headers, params=params, json=data)
164+
if response.status_code != 200:
165+
abort(response.status_code, response.content)
166+
return response.json()
152167

153168
def collections(self) -> List[dict]:
154169
"""Retrieve the collections from STAC."""
155-
collections = self._instance.collections
156-
return list(collections.values())
170+
uri = self._url_resource('collections')
171+
collections = self._request(uri, params=self._params, headers=self._headers)
172+
return collections
157173

158174
def collection(self, collection_id: str) -> dict:
159175
"""Access STAC Collection."""
160-
return self._instance.collection(collection_id)
176+
uri = self._url_resource(f'collections/{collection_id}')
177+
collection = self._request(uri, params=self._params, headers=self._headers)
178+
return collection
161179

162180
def items(self, collection_id: str, **kwargs) -> dict:
163181
"""Access STAC Collection Items."""
164182
return self.search(collections=[collection_id], limit=1)
165183

184+
def _url_resource(self, resource: str) -> str:
185+
return urljoin(self.uri + '/', resource)
186+
166187

167188
def build_stac(uri, headers=None, **parameters) -> BaseSTAC:
168189
"""Build a STAC instance according versions."""

cube_builder/maestro.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
from celery import chain, group
3939
from geoalchemy2 import func
4040
from geoalchemy2.shape import to_shape
41-
from stac import STAC
4241

4342
# Cube Builder
4443
from werkzeug.exceptions import abort

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
'rio_cogeo==3.0.2',
7878
'shapely>=1.7,<2',
7979
'SQLAlchemy-Utils>=0.34.2,<1',
80-
'stac.py==0.9.0.post12',
8180
'MarkupSafe==2.0.1',
8281
]
8382

0 commit comments

Comments
 (0)