Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit 5ae0deb

Browse files
author
Phil Varner
committed
Merge branch 'main' into pv/subsume-Feature-into-Order
2 parents 099b500 + 490c526 commit 5ae0deb

File tree

16 files changed

+430
-227
lines changed

16 files changed

+430
-227
lines changed

.github/workflows/pr.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ jobs:
2424
run: |
2525
python -m pip install pre-commit
2626
pre-commit run --all-files
27+
- name: Type-check code
28+
if: ${{ matrix.python-version == 3.12 }}
29+
run: poetry run mypy src
2730
- name: Run tests
2831
run: poetry run nox

.pre-commit-config.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ repos:
1919
rev: v1.13.0
2020
hooks:
2121
- id: mypy
22-
files: ".*\\.py$"
22+
args: [] # override default of [--strict, --ignore-missing-imports]
23+
files: src/
2324
additional_dependencies:
2425
- types-pyRFC3339~=1.1.1
26+
- pydantic~=2.10.1
27+
- returns~=0.23.0
28+
- fastapi~=0.115.0
29+
- geojson_pydantic~=1.1.1

CHANGELOG.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,43 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8-
## [Unreleased]
8+
## [unreleased]
99

1010
### Added
1111

1212
none
1313

1414
### Changed
1515

16+
- OrderStatusCode and ProviderRole are now StrEnum instead of (str, Enum)
17+
- All types using `Result[A, Exception]` have been replace with the equivalent type `ResultE[A]`
18+
19+
### Deprecated
20+
21+
none
22+
23+
### Removed
24+
25+
none
26+
27+
### Fixed
28+
29+
none
30+
31+
### Security
32+
33+
none
34+
35+
## [v0.2.0] - 2024-11-23
36+
37+
### Added
38+
39+
none
40+
41+
### Changed
42+
43+
- RootBackend and ProductBackend protocols use `returns` library types Result and Maybe instead of
44+
raising exceptions.
1645
- Create Order endpoint from `.../order` to `.../orders`
1746
- Order field `id` must be a string, instead of previously allowing int. This is because while an
1847
order ID may an integral numeric value, it is not a "number" in the sense that math will be performed
@@ -50,5 +79,6 @@ Initial release
5079
- Add links `opportunities` and `create-order` to Product
5180
- Add link `create-order` to OpportunityCollection
5281

53-
[unreleased]: https://github.com/stapi-spec/stapi-fastapi/compare/v0.1.0...main
82+
[unreleased]: https://github.com/stapi-spec/stapi-fastapi/compare/v0.2.0...main
83+
[v0.2.0]: https://github.com/stapi-spec/stapi-fastapi/tree/v0.2.0
5484
[v0.1.0]: https://github.com/stapi-spec/stapi-fastapi/tree/v0.1.0

bin/server.py

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
from uuid import uuid4
33

44
from fastapi import FastAPI, Request
5+
from returns.maybe import Maybe
6+
from returns.result import Failure, ResultE, Success
57

68
from stapi_fastapi.backends.product_backend import ProductBackend
79
from stapi_fastapi.backends.root_backend import RootBackend
8-
from stapi_fastapi.exceptions import NotFoundException
910
from stapi_fastapi.models.conformance import CORE
1011
from stapi_fastapi.models.opportunity import (
1112
Opportunity,
@@ -37,20 +38,18 @@ class MockRootBackend(RootBackend):
3738
def __init__(self, orders: MockOrderDB) -> None:
3839
self._orders: MockOrderDB = orders
3940

40-
async def get_orders(self, request: Request) -> OrderCollection:
41+
async def get_orders(self, request: Request) -> ResultE[OrderCollection]:
4142
"""
4243
Show all orders.
4344
"""
44-
return OrderCollection(features=list(self._orders.values()))
45+
return Success(OrderCollection(features=list(self._orders.values())))
4546

46-
async def get_order(self, order_id: str, request: Request) -> Order:
47+
async def get_order(self, order_id: str, request: Request) -> ResultE[Maybe[Order]]:
4748
"""
4849
Show details for order with `order_id`.
4950
"""
50-
try:
51-
return self._orders[order_id]
52-
except KeyError:
53-
raise NotFoundException()
51+
52+
return Success(Maybe.from_optional(self._orders.get(order_id)))
5453

5554

5655
class MockProductBackend(ProductBackend):
@@ -64,41 +63,49 @@ async def search_opportunities(
6463
product_router: ProductRouter,
6564
search: OpportunityRequest,
6665
request: Request,
67-
) -> list[Opportunity]:
68-
return [o.model_copy(update=search.model_dump()) for o in self._opportunities]
66+
) -> ResultE[list[Opportunity]]:
67+
try:
68+
return Success(
69+
[o.model_copy(update=search.model_dump()) for o in self._opportunities]
70+
)
71+
except Exception as e:
72+
return Failure(e)
6973

7074
async def create_order(
7175
self, product_router: ProductRouter, payload: OrderRequest, request: Request
72-
) -> Order:
76+
) -> ResultE[Order]:
7377
"""
7478
Create a new order.
7579
"""
76-
order = Order(
77-
id=str(uuid4()),
78-
geometry=payload.geometry,
79-
properties={
80-
"product_id": product_router.product.id,
81-
"created": datetime.now(timezone.utc),
82-
"status": OrderStatus(
83-
timestamp=datetime.now(timezone.utc),
84-
status_code=OrderStatusCode.accepted,
85-
),
86-
"search_parameters": {
87-
"geometry": payload.geometry,
88-
"datetime": payload.datetime,
89-
"filter": payload.filter,
90-
},
91-
"order_parameters": payload.order_parameters.model_dump(),
92-
"opportunity_properties": {
93-
"datetime": "2024-01-29T12:00:00Z/2024-01-30T12:00:00Z",
94-
"off_nadir": 10,
80+
try:
81+
order = Order(
82+
id=str(uuid4()),
83+
geometry=payload.geometry,
84+
properties={
85+
"product_id": product_router.product.id,
86+
"created": datetime.now(timezone.utc),
87+
"status": OrderStatus(
88+
timestamp=datetime.now(timezone.utc),
89+
status_code=OrderStatusCode.accepted,
90+
),
91+
"search_parameters": {
92+
"geometry": payload.geometry,
93+
"datetime": payload.datetime,
94+
"filter": payload.filter,
95+
},
96+
"order_parameters": payload.order_parameters.model_dump(),
97+
"opportunity_properties": {
98+
"datetime": "2024-01-29T12:00:00Z/2024-01-30T12:00:00Z",
99+
"off_nadir": 10,
100+
},
95101
},
96-
},
97-
links=[],
98-
)
102+
links=[],
103+
)
99104

100-
self._orders[order.id] = order
101-
return order
105+
self._orders[order.id] = order
106+
return Success(order)
107+
except Exception as e:
108+
return Failure(e)
102109

103110

104111
class MyOpportunityProperties(OpportunityProperties):

0 commit comments

Comments
 (0)