-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.py
148 lines (125 loc) · 4.38 KB
/
server.py
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
from datetime import datetime, timezone
from uuid import uuid4
from fastapi import FastAPI, Request
from returns.maybe import Maybe
from returns.result import Failure, Result, Success
from stapi_fastapi.backends.product_backend import ProductBackend
from stapi_fastapi.backends.root_backend import RootBackend
from stapi_fastapi.models.conformance import CORE
from stapi_fastapi.models.opportunity import (
Opportunity,
OpportunityProperties,
OpportunityRequest,
)
from stapi_fastapi.models.order import (
Order,
OrderCollection,
OrderParameters,
OrderRequest,
OrderStatus,
OrderStatusCode,
)
from stapi_fastapi.models.product import (
Product,
Provider,
ProviderRole,
)
from stapi_fastapi.routers.product_router import ProductRouter
from stapi_fastapi.routers.root_router import RootRouter
class MockOrderDB(dict[int | str, Order]):
pass
class MockRootBackend(RootBackend):
def __init__(self, orders: MockOrderDB) -> None:
self._orders: MockOrderDB = orders
async def get_orders(self, request: Request) -> Result[OrderCollection, Exception]:
"""
Show all orders.
"""
return Success(OrderCollection(features=list(self._orders.values())))
async def get_order(
self, order_id: str, request: Request
) -> Result[Maybe[Order], Exception]:
"""
Show details for order with `order_id`.
"""
return Success(Maybe.from_optional(self._orders.get(order_id)))
class MockProductBackend(ProductBackend):
def __init__(self, orders: MockOrderDB) -> None:
self._opportunities: list[Opportunity] = []
self._allowed_payloads: list[OrderRequest] = []
self._orders: MockOrderDB = orders
async def search_opportunities(
self,
product_router: ProductRouter,
search: OpportunityRequest,
request: Request,
) -> Result[list[Opportunity], Exception]:
try:
return Success(
[o.model_copy(update=search.model_dump()) for o in self._opportunities]
)
except Exception as e:
return Failure(e)
async def create_order(
self, product_router: ProductRouter, payload: OrderRequest, request: Request
) -> Result[Order, Exception]:
"""
Create a new order.
"""
try:
order = Order(
id=str(uuid4()),
geometry=payload.geometry,
properties={
"product_id": product_router.product.id,
"created": datetime.now(timezone.utc),
"status": OrderStatus(
timestamp=datetime.now(timezone.utc),
status_code=OrderStatusCode.accepted,
),
"search_parameters": {
"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=[],
)
self._orders[order.id] = order
return Success(order)
except Exception as e:
return Failure(e)
class MyOpportunityProperties(OpportunityProperties):
off_nadir: int
class MyOrderParameters(OrderParameters):
s3_path: str | None = None
order_db = MockOrderDB()
product_backend = MockProductBackend(order_db)
root_backend = MockRootBackend(order_db)
provider = Provider(
name="Test Provider",
description="A provider for Test data",
roles=[ProviderRole.producer], # Example role
url="https://test-provider.example.com", # Must be a valid URL
)
product = Product(
id="test-spotlight",
title="Test Spotlight Product",
description="Test product for test spotlight",
license="CC-BY-4.0",
keywords=["test", "satellite"],
providers=[provider],
links=[],
constraints=MyOpportunityProperties,
order_parameters=MyOrderParameters,
backend=product_backend,
)
root_router = RootRouter(root_backend, conformances=[CORE])
root_router.add_product(product)
app: FastAPI = FastAPI()
app.include_router(root_router, prefix="")