77import pystac
88from api .api_types import (Opportunity , OpportunityCollection ,
99 OpportunityProperties , Product , ProductConstraints ,
10- Search , Provider )
11- from pystac import Collection
10+ Search , Order , Provider )
11+ from pystac import Collection , ItemCollection
12+
1213from pystac_client .client import Client
1314
1415DEFAULT_MAX_ITEMS = 10
@@ -89,11 +90,7 @@ class HistoricalBackend:
8990 def __init__ (self ) -> None :
9091 self .catalog = Client .open ('https://earth-search.aws.element84.com/v1' ) # type: ignore
9192
92- async def find_opportunities (
93- self ,
94- search : Search ,
95- token : str ,
96- ) -> OpportunityCollection :
93+ def _search (self , search ) -> ItemCollection :
9794 max_items = min (search .limit , MAX_MAX_ITEMS )
9895
9996 args : dict [str , Any ] = {
@@ -114,19 +111,26 @@ async def find_opportunities(
114111 raise Exception ('A datetime range must be specified' )
115112
116113 search_obj = self .catalog .search (** args )
117- item_coll = search_obj .item_collection ()
114+ return search_obj .item_collection ()
118115
116+ async def find_opportunities (
117+ self ,
118+ search : Search ,
119+ token : str ,
120+ ) -> OpportunityCollection :
119121 # Convert the STAC items from earth search into opportunities
122+ item_collection = self ._search (search )
120123 opportunities : list [Opportunity ] = [
121124 stac_item_to_opportunity (item , product_id = search .product_id )
122- for item in item_coll .items
125+ for item in item_collection .items
123126 ]
124127 opportunity_collection = OpportunityCollection (
125128 features = opportunities
126129 )
127130
128131 return opportunity_collection
129132
133+
130134 async def find_products (self , token : str ) -> list [Product ]:
131135 def safe_get_coll (product_id : str ) -> Collection :
132136 coll = self .catalog .get_collection (product_id )
@@ -138,3 +142,17 @@ def safe_get_coll(product_id: str) -> Collection:
138142 stac_collection_to_product (safe_get_coll (product_id ))
139143 for product_id in PRODUCT_IDS
140144 ]
145+
146+ async def place_order (
147+ self ,
148+ search : Search ,
149+ token : str ,
150+ ) -> Order :
151+ """Get the first item off the search output and return that ID"""
152+ item_collection = self ._search (search )
153+
154+ if len (item_collection .items ) == 0 :
155+ raise ValueError (f"Unable to place an order for this product: '{ search .product_id } '" )
156+
157+ best_guess = item_collection .items [0 ]
158+ return Order (id = best_guess .id )
0 commit comments