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

Commit c0d7a23

Browse files
make OpportunityProperties and Opportunity models generics
1 parent 494f952 commit c0d7a23

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

stapi_fastapi/backend.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from typing import Protocol
2-
32
from fastapi import Request
43

54
from stapi_fastapi.models.opportunity import Opportunity, OpportunityRequest

stapi_fastapi/models/opportunity.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Literal, Optional
1+
from typing import Literal, Optional, TypeVar, Generic
22

33
from geojson_pydantic import Feature, FeatureCollection
44
from geojson_pydantic.geometries import Geometry
@@ -7,23 +7,27 @@
77
from stapi_fastapi.types.datetime_interval import DatetimeInterval
88
from stapi_fastapi.types.filter import CQL2Filter
99

10+
# Generic type definition for Opportunity Properties
11+
T = TypeVar("T")
1012

11-
# GENERIC Python
1213
# Copied and modified from https://github.com/stac-utils/stac-pydantic/blob/main/stac_pydantic/item.py#L11
13-
class OpportunityProperties(BaseModel):
14+
class OpportunityProperties(BaseModel, Generic[T]):
1415
datetime: DatetimeInterval
1516
model_config = ConfigDict(extra="allow")
1617

17-
# NOT GENERIC
1818
class OpportunityRequest(BaseModel):
1919
datetime: DatetimeInterval
2020
geometry: Geometry
2121
# TODO: validate the CQL2 filter?
2222
filter: Optional[CQL2Filter] = None
2323
# PHILOSOPH: strict?
2424

25-
# GENERIC: Each product needs an opportunity model (constraints/parameters)
26-
class Opportunity(Feature[Geometry, OpportunityProperties]):
25+
# Generic type definition for Opportunity
26+
P = TypeVar("P", bound=OpportunityProperties)
27+
K = TypeVar("K", bound=Geometry)
28+
29+
# Each product implements its own opportunity model
30+
class Opportunity(Feature[K, P], Generic[K, P]):
2731
type: Literal["Feature"] = "Feature"
2832

2933

stapi_fastapi/products_router.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Generic product router factory
22
from fastapi import APIRouter, HTTPException, status, Request
3-
from typing import Dict, Any
43
from stapi_fastapi.models.opportunity import OpportunityRequest
54
from stapi_fastapi.backend import StapiBackend
65
from stapi_fastapi.exceptions import ConstraintsException
@@ -12,7 +11,6 @@
1211
"""
1312

1413
def create_products_router(product_id: str, backend: StapiBackend) -> APIRouter:
15-
# TODO: map product names to product IDs
1614
"""
1715
Creates a new APIRouter for a specific product type with standardized routes.
1816
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from routers.products_router import create_products_router
22

33
# Create a router for electronics using the base factory function
4-
umbra_spotlight_router = create_products_router("umbra_spotlight")
4+
umbra_spotlight_router = create_products_router("umbra-spotlight-1")

0 commit comments

Comments
 (0)