-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
212 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,17 @@ | ||
from enum import Enum | ||
from typing import Literal | ||
|
||
from geojson_pydantic import Feature | ||
from geojson_pydantic.geometries import Geometry | ||
from pydantic import AwareDatetime, ConfigDict | ||
|
||
from stat_fastapi.models.constraints import Constraints | ||
from stat_fastapi.models.shared import Link | ||
from stat_fastapi.types.datetime_interval import DatetimeInterval | ||
|
||
|
||
class OrderPayload(Feature[Geometry, Constraints]): | ||
product_id: str | ||
|
||
|
||
class OrderStatus(str, Enum): | ||
pending = "pending" | ||
processing = "processing" | ||
finished = "finished" | ||
failed = "failed" | ||
expired = "expired" | ||
|
||
|
||
class OrderProperties(Constraints): | ||
datetime: DatetimeInterval | ||
|
||
status: OrderStatus = OrderStatus.pending | ||
created_at: AwareDatetime | ||
updated_at: AwareDatetime | ||
|
||
model_config = ConfigDict(extra="allow") | ||
|
||
|
||
class Order(Feature[Geometry, OrderProperties]): | ||
class Order(Feature[Geometry, Constraints]): | ||
type: Literal["Feature"] = "Feature" | ||
product_id: str | ||
links: list[Link] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from stat_fastapi_test_backend.backend import TestBackend | ||
|
||
__all__ = ["TestBackend"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from typing import Mapping | ||
from uuid import uuid4 | ||
|
||
from fastapi import Request | ||
|
||
from stat_fastapi.exceptions import ConstraintsException, NotFoundException | ||
from stat_fastapi.models.opportunity import Opportunity, OpportunitySearch | ||
from stat_fastapi.models.order import Order, OrderPayload | ||
from stat_fastapi.models.product import Product | ||
|
||
|
||
class TestBackend: | ||
_products: list[Product] = [] | ||
_opportunities: list[Opportunity] = [] | ||
_allowed_order_payloads: list[OrderPayload] = [] | ||
_orders: Mapping[str, Order] = {} | ||
|
||
def products(self, request: Request) -> list[Product]: | ||
""" | ||
Return a list of supported products. | ||
""" | ||
return self._products | ||
|
||
def product(self, product_id: str, request: Request) -> Product | None: | ||
""" | ||
Return the product identified by `product_id` or `None` if it isn't | ||
supported. | ||
""" | ||
try: | ||
return next( | ||
(product for product in self._products if product.id == product_id) | ||
) | ||
except StopIteration as exc: | ||
raise NotFoundException() from exc | ||
|
||
async def search_opportunities( | ||
self, search: OpportunitySearch, request: Request | ||
) -> list[Opportunity]: | ||
return [ | ||
o.model_copy(update={"constraints": search.properties}) | ||
for o in self._opportunities | ||
] | ||
|
||
async def create_order(self, payload: OrderPayload, request: Request) -> Order: | ||
""" | ||
Create a new order. | ||
""" | ||
allowed = any(allowed == payload for allowed in self._allowed_order_payloads) | ||
if allowed: | ||
order = Order( | ||
id=str(uuid4()), | ||
geometry=payload.geometry, | ||
properties=payload.properties, | ||
product_id=payload.product_id, | ||
links=[], | ||
) | ||
self._orders[order.id] = order | ||
return order | ||
raise ConstraintsException("not allowed") | ||
|
||
async def get_order(self, order_id: str, request: Request): | ||
""" | ||
Show details for order with `order_id`. | ||
""" | ||
try: | ||
return self._orders[order_id] | ||
except KeyError: | ||
raise NotFoundException() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from stat_fastapi_tle_backend.backend import StatMockBackend | ||
|
||
__all__ = ["StatMockBackend"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from datetime import UTC, datetime | ||
|
||
from geojson_pydantic import Point | ||
from pydantic import BaseModel | ||
from pytest import fixture | ||
|
||
from stat_fastapi.models.constraints import Constraints | ||
from stat_fastapi.models.opportunity import Opportunity | ||
from stat_fastapi.models.order import OrderPayload | ||
from stat_fastapi.models.product import Product, Provider, ProviderRole | ||
|
||
|
||
@fixture | ||
def products(): | ||
class Constraints(BaseModel): | ||
pass | ||
|
||
yield [ | ||
Product( | ||
id="mock:standard", | ||
description="Mock backend's standard product", | ||
license="CC0-1.0", | ||
providers=[ | ||
Provider( | ||
name="ACME", | ||
roles=[ | ||
ProviderRole.licensor, | ||
ProviderRole.producer, | ||
ProviderRole.processor, | ||
ProviderRole.host, | ||
], | ||
url="http://acme.example.com", | ||
) | ||
], | ||
constraints=Constraints, | ||
links=[], | ||
) | ||
] | ||
|
||
|
||
@fixture | ||
def opportunities(): | ||
yield [ | ||
Opportunity( | ||
geometry=Point(type="Point", coordinates=[13.4, 52.5]), | ||
properties={}, | ||
constraints=Constraints(datetime=(datetime.now(UTC), datetime.now(UTC))), | ||
) | ||
] | ||
|
||
|
||
@fixture | ||
def allowed_order_payloads(products: list[Product]): | ||
yield [ | ||
OrderPayload( | ||
type="Feature", | ||
geometry=Point(type="Point", coordinates=[13.4, 52.5]), | ||
product_id=products[0].id, | ||
properties=Constraints(datetime=(datetime.now(UTC), datetime.now(UTC))), | ||
), | ||
] |
Oops, something went wrong.