-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbackends.py
More file actions
214 lines (186 loc) · 6.6 KB
/
Copy pathbackends.py
File metadata and controls
214 lines (186 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from datetime import UTC, datetime
from uuid import uuid4
from fastapi import Request
from returns.maybe import Maybe, Nothing, Some
from returns.result import Failure, ResultE, Success
from stapi_fastapi.routers.product_router import ProductRouter
from stapi_pydantic import (
Opportunity,
OpportunityCollection,
OpportunityPayload,
OpportunitySearchRecord,
OpportunitySearchStatus,
OpportunitySearchStatusCode,
Order,
OrderPayload,
OrderProperties,
OrderSearchParameters,
OrderStatus,
OrderStatusCode,
)
async def mock_get_orders(
next: str | None,
limit: int,
request: Request,
) -> ResultE[tuple[list[Order], Maybe[str], Maybe[int]]]:
"""
Return orders from backend. Handle pagination/limit if applicable
"""
count = 314
try:
start = 0
limit = min(limit, 100)
order_ids = [*request.state._orders_db._orders.keys()]
if next:
start = order_ids.index(next)
end = start + limit
ids = order_ids[start:end]
orders = [request.state._orders_db.get_order(order_id) for order_id in ids]
if end > 0 and end < len(order_ids):
return Success((orders, Some(request.state._orders_db._orders[order_ids[end]].id), Some(count)))
return Success((orders, Nothing, Some(count)))
except Exception as e:
return Failure(e)
async def mock_get_order(order_id: str, request: Request) -> ResultE[Maybe[Order]]:
"""
Show details for order with `order_id`.
"""
try:
return Success(Maybe.from_optional(request.state._orders_db.get_order(order_id)))
except Exception as e:
return Failure(e)
async def mock_get_order_statuses(
order_id: str, next: str | None, limit: int, request: Request
) -> ResultE[Maybe[tuple[list[OrderStatus], Maybe[str]]]]:
try:
start = 0
limit = min(limit, 100)
statuses = request.state._orders_db.get_order_statuses(order_id)
if statuses is None:
return Success(Nothing)
if next:
start = int(next)
end = start + limit
stati = statuses[start:end]
if end > 0 and end < len(statuses):
return Success(Some((stati, Some(str(end)))))
return Success(Some((stati, Nothing)))
except Exception as e:
return Failure(e)
async def mock_create_order(product_router: ProductRouter, payload: OrderPayload, request: Request) -> ResultE[Order]:
"""
Create a new order.
"""
try:
status = OrderStatus(
timestamp=datetime.now(UTC),
status_code=OrderStatusCode.received,
)
order = Order(
id=str(uuid4()),
geometry=payload.geometry,
properties=OrderProperties(
product_id=product_router.product.id,
created=datetime.now(UTC),
status=status,
search_parameters=OrderSearchParameters(
geometry=payload.geometry,
datetime=payload.datetime,
filter=payload.filter,
),
order_parameters=payload.order_parameters.model_dump(),
opportunity_properties={
"datetime": "2024-01-29T12:00:00Z/2024-01-30T12:00:00Z",
"off_nadir": 10,
},
),
links=[],
)
request.state._orders_db.put_order(order)
request.state._orders_db.put_order_status(order.id, status)
return Success(order)
except Exception as e:
return Failure(e)
async def mock_search_opportunities(
product_router: ProductRouter,
search: OpportunityPayload,
next: str | None,
limit: int,
request: Request,
) -> ResultE[tuple[list[Opportunity], Maybe[str]]]:
try:
start = 0
limit = min(limit, 100)
if next:
start = int(next)
end = start + limit
opportunities = [o.model_copy(update=search.model_dump()) for o in request.state._opportunities[start:end]]
if end > 0 and end < len(request.state._opportunities):
return Success((opportunities, Some(str(end))))
return Success((opportunities, Nothing))
except Exception as e:
return Failure(e)
async def mock_search_opportunities_async(
product_router: ProductRouter,
search: OpportunityPayload,
request: Request,
) -> ResultE[OpportunitySearchRecord]:
try:
received_status = OpportunitySearchStatus(
timestamp=datetime.now(UTC),
status_code=OpportunitySearchStatusCode.received,
)
search_record = OpportunitySearchRecord(
id=str(uuid4()),
product_id=product_router.product.id,
opportunity_request=search,
status=received_status,
links=[],
)
request.state._opportunities_db.put_search_record(search_record)
return Success(search_record)
except Exception as e:
return Failure(e)
async def mock_get_opportunity_collection(
product_router: ProductRouter, opportunity_collection_id: str, request: Request
) -> ResultE[Maybe[OpportunityCollection]]:
try:
return Success(
Maybe.from_optional(request.state._opportunities_db.get_opportunity_collection(opportunity_collection_id))
)
except Exception as e:
return Failure(e)
async def mock_get_opportunity_search_records(
next: str | None,
limit: int,
request: Request,
) -> ResultE[tuple[list[OpportunitySearchRecord], Maybe[str]]]:
try:
start = 0
limit = min(limit, 100)
search_records = request.state._opportunities_db.get_search_records()
if next:
start = int(next)
end = start + limit
page = search_records[start:end]
if end > 0 and end < len(search_records):
return Success((page, Some(str(end))))
return Success((page, Nothing))
except Exception as e:
return Failure(e)
async def mock_get_opportunity_search_record(
search_record_id: str, request: Request
) -> ResultE[Maybe[OpportunitySearchRecord]]:
try:
return Success(Maybe.from_optional(request.state._opportunities_db.get_search_record(search_record_id)))
except Exception as e:
return Failure(e)
async def mock_get_opportunity_search_record_statuses(
search_record_id: str, request: Request
) -> ResultE[Maybe[list[OpportunitySearchStatus]]]:
try:
return Success(
Maybe.from_optional(request.state._opportunities_db.get_search_record_statuses(search_record_id))
)
except Exception as e:
return Failure(e)