1
- from datetime import datetime , timezone
2
- from uuid import uuid4
3
1
import logging
4
2
5
- logger = logging .getLogger (__name__ )
6
-
7
-
8
3
from fastapi import Request
9
4
from returns .maybe import Maybe , Nothing , Some
10
5
from returns .result import Failure , ResultE , Success
11
6
7
+ from stapi_fastapi import Link
8
+ from stapi_fastapi .constants import TYPE_JSON
12
9
from stapi_fastapi .models .opportunity import (
13
10
Opportunity ,
14
- OpportunityCollection ,
15
11
OpportunityPayload ,
16
- OpportunitySearchRecord ,
17
- OpportunitySearchStatus ,
18
- OpportunitySearchStatusCode ,
19
12
)
20
13
from stapi_fastapi .models .order import (
21
14
Order ,
22
15
OrderPayload ,
23
- OrderProperties ,
24
- OrderSearchParameters ,
25
16
OrderStatus ,
26
- OrderStatusCode ,
27
17
)
18
+ from stapi_fastapi .models .product import ProductsCollection
28
19
from stapi_fastapi .routers .product_router import ProductRouter
20
+ from stapi_fastapi .routers .route_names import CREATE_ORDER , LIST_PRODUCTS
21
+
22
+ from . import conversions
23
+ from .client import Client
29
24
30
- from client import Client
31
- import conversions
25
+ logger = logging .getLogger (__name__ )
32
26
33
27
34
28
async def mock_get_orders (
@@ -64,7 +58,11 @@ async def get_order(order_id: str, request: Request) -> ResultE[Maybe[Order]]:
64
58
"""
65
59
try :
66
60
return Success (
67
- Maybe .from_optional (conversions .planet_order_to_stapi_order (Client (request ).get_order (order_id )))
61
+ Maybe .from_optional (
62
+ conversions .planet_order_to_stapi_order (
63
+ Client (request ).get_order (order_id )
64
+ )
65
+ )
68
66
)
69
67
except Exception as e :
70
68
return Failure (e )
@@ -92,46 +90,39 @@ async def mock_get_order_statuses(
92
90
return Failure (e )
93
91
94
92
95
- async def mock_create_order (
93
+ async def get_products (self , request : Request , ** router_args ) -> ProductsCollection :
94
+ links = [
95
+ Link (
96
+ href = str (request .url_for (f"{ self .name } :{ LIST_PRODUCTS } " )),
97
+ rel = "self" ,
98
+ type = TYPE_JSON ,
99
+ ),
100
+ ]
101
+ return ProductsCollection (
102
+ products = [
103
+ conversions .planet_product_to_stapi_product (planet_product , ** router_args )
104
+ for planet_product in Client (request ).get_products ()
105
+ ],
106
+ links = links ,
107
+ )
108
+
109
+
110
+ async def create_order (
96
111
product_router : ProductRouter , payload : OrderPayload , request : Request
97
112
) -> ResultE [Order ]:
98
- """
99
- Create a new order.
100
- """
101
113
try :
102
- status = OrderStatus (
103
- timestamp = datetime .now (timezone .utc ),
104
- status_code = OrderStatusCode .received ,
114
+ planet_payload = conversions .stapi_order_payload_to_planet_create_order_payload (
115
+ payload , product_router .product
105
116
)
106
- order = Order (
107
- id = str (uuid4 ()),
108
- geometry = payload .geometry ,
109
- properties = OrderProperties (
110
- product_id = product_router .product .id ,
111
- created = datetime .now (timezone .utc ),
112
- status = status ,
113
- search_parameters = OrderSearchParameters (
114
- geometry = payload .geometry ,
115
- datetime = payload .datetime ,
116
- filter = payload .filter ,
117
- ),
118
- order_parameters = payload .order_parameters .model_dump (),
119
- opportunity_properties = {
120
- "datetime" : "2024-01-29T12:00:00Z/2024-01-30T12:00:00Z" ,
121
- "off_nadir" : 10 ,
122
- },
123
- ),
124
- links = [],
125
- )
126
-
127
- request .state ._orders_db .put_order (order )
128
- request .state ._orders_db .put_order_status (order .id , status )
129
- return Success (order )
117
+ planet_order_response = Client (request ).create_order (planet_payload )
118
+ stapi_order = conversions .planet_order_to_stapi_order (planet_order_response )
119
+ return Success (stapi_order )
130
120
except Exception as e :
131
121
return Failure (e )
132
122
133
123
134
124
# TODO why does this return a list of Opportunities and not an OpportunityCollection?
125
+ # and what does the related "get_search_opportunities" do in comparison?
135
126
async def search_opportunities (
136
127
product_router : ProductRouter ,
137
128
search : OpportunityPayload ,
@@ -140,88 +131,20 @@ async def search_opportunities(
140
131
request : Request ,
141
132
) -> ResultE [tuple [list [Opportunity ], Maybe [str ]]]:
142
133
try :
143
- iw_request = conversions .stapi_opportunity_payload_to_planet_iw_search (product_router .product , search )
134
+ iw_request = conversions .stapi_opportunity_payload_to_planet_iw_search (
135
+ product_router .product , search
136
+ )
144
137
imaging_windows = Client (request ).get_imaging_windows (iw_request )
138
+ create_order_name = f"{ product_router .root_router .name } :{ product_router .product .id } :{ CREATE_ORDER } "
139
+ create_href = str (request .url_for (create_order_name ))
145
140
146
141
opportunities = [
147
- conversions .planet_iw_to_stapi_opportunity (iw , product_router .product , search )
148
- for iw
149
- in imaging_windows
142
+ conversions .planet_iw_to_stapi_opportunity (
143
+ iw , product_router .product , search , create_href
144
+ )
145
+ for iw in imaging_windows
150
146
]
151
- #return OpportunityCollection(features=opportunities)
147
+ # return OpportunityCollection(features=opportunities)
152
148
return Success ((opportunities , Nothing ))
153
149
except Exception as e :
154
150
return Failure (e )
155
-
156
-
157
- async def mock_search_opportunities_async (
158
- product_router : ProductRouter ,
159
- search : OpportunityPayload ,
160
- request : Request ,
161
- ) -> ResultE [OpportunitySearchRecord ]:
162
- try :
163
- received_status = OpportunitySearchStatus (
164
- timestamp = datetime .now (timezone .utc ),
165
- status_code = OpportunitySearchStatusCode .received ,
166
- )
167
- search_record = OpportunitySearchRecord (
168
- id = str (uuid4 ()),
169
- product_id = product_router .product .id ,
170
- opportunity_request = search ,
171
- status = received_status ,
172
- links = [],
173
- )
174
- request .state ._opportunities_db .put_search_record (search_record )
175
- return Success (search_record )
176
- except Exception as e :
177
- return Failure (e )
178
-
179
-
180
- async def mock_get_opportunity_collection (
181
- product_router : ProductRouter , opportunity_collection_id : str , request : Request
182
- ) -> ResultE [Maybe [OpportunityCollection ]]:
183
- try :
184
- return Success (
185
- Maybe .from_optional (
186
- request .state ._opportunities_db .get_opportunity_collection (
187
- opportunity_collection_id
188
- )
189
- )
190
- )
191
- except Exception as e :
192
- return Failure (e )
193
-
194
-
195
- async def mock_get_opportunity_search_records (
196
- next : str | None ,
197
- limit : int ,
198
- request : Request ,
199
- ) -> ResultE [tuple [list [OpportunitySearchRecord ], Maybe [str ]]]:
200
- try :
201
- start = 0
202
- limit = min (limit , 100 )
203
- search_records = request .state ._opportunities_db .get_search_records ()
204
-
205
- if next :
206
- start = int (next )
207
- end = start + limit
208
- page = search_records [start :end ]
209
-
210
- if end > 0 and end < len (search_records ):
211
- return Success ((page , Some (str (end ))))
212
- return Success ((page , Nothing ))
213
- except Exception as e :
214
- return Failure (e )
215
-
216
-
217
- async def mock_get_opportunity_search_record (
218
- search_record_id : str , request : Request
219
- ) -> ResultE [Maybe [OpportunitySearchRecord ]]:
220
- try :
221
- return Success (
222
- Maybe .from_optional (
223
- request .state ._opportunities_db .get_search_record (search_record_id )
224
- )
225
- )
226
- except Exception as e :
227
- return Failure (e )
0 commit comments