2121from abc import ABC , abstractmethod
2222from copy import deepcopy
2323from typing import List
24+ from urllib .parse import urljoin
2425
2526import requests
2627import shapely .geometry
2728from pystac_client import Client
28- from stac import STAC as _STAC
29+ from werkzeug . exceptions import abort
2930
3031
3132class BaseSTAC (ABC ):
@@ -119,27 +120,33 @@ def items(self, collection_id: str, **kwargs) -> dict:
119120class 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
167188def build_stac (uri , headers = None , ** parameters ) -> BaseSTAC :
168189 """Build a STAC instance according versions."""
0 commit comments