Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions fastfuels_sdk/domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
UpdateDomainRequest,
)

_DOMAIN_API = DomainsApi(get_client())
# _DOMAIN_API = DomainsApi(get_client())


class Domain(DomainModel):
Expand Down Expand Up @@ -80,6 +80,7 @@ class Domain(DomainModel):

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.API = DomainsApi(get_client())

@classmethod
def from_id(cls, domain_id: str) -> "Domain":
Expand All @@ -104,7 +105,7 @@ def from_id(cls, domain_id: str) -> "Domain":
>>> domain.id
'abc123'
"""
get_domain_response = _DOMAIN_API.get_domain(domain_id)
get_domain_response = self.API.get_domain(domain_id)
return cls(**get_domain_response.model_dump())

@classmethod
Expand Down Expand Up @@ -172,7 +173,7 @@ def from_geojson(
}

request = CreateDomainRequest.from_dict(feature_data)
response = _DOMAIN_API.create_domain(
response = self.API.create_domain(
create_domain_request=request.model_dump() # noqa
)
return cls(**response.model_dump()) if response else None
Expand Down Expand Up @@ -291,7 +292,7 @@ def get(self, in_place: bool = False) -> "Domain":
ensure all references to this Domain instance see the updated data.
"""
# Fetch latest data from API
response = _DOMAIN_API.get_domain(self.id)
response = self.API.get_domain(self.id)

if in_place:
# Update all attributes of current instance
Expand Down Expand Up @@ -365,7 +366,7 @@ def update(
# Only make API call if there are fields to update
if update_data:
request = UpdateDomainRequest(**update_data)
response = _DOMAIN_API.update_domain(
response = self.API.update_domain(
domain_id=self.id, update_domain_request=request
)

Expand Down Expand Up @@ -401,7 +402,7 @@ def delete(self) -> None:
>>> domain.get()
# Raises NotFoundException
"""
_DOMAIN_API.delete_domain(domain_id=self.id)
self.API.delete_domain(domain_id=self.id)
return None


Expand Down Expand Up @@ -462,9 +463,11 @@ def list_domains(
- The maximum page size is 1000 items.
- When calculating total pages, use: ceil(response.totalItems / response.pageSize)
"""

API = DomainsApi(get_client())
sort_by = DomainSortField(sort_by) if sort_by else None
sort_order = DomainSortOrder(sort_order) if sort_order else None
list_response = _DOMAIN_API.list_domains(
list_response = API.list_domains(
page=page, size=size, sort_by=sort_by, sort_order=sort_order
)
list_response.domains = [Domain(**d.model_dump()) for d in list_response.domains]
Expand Down
65 changes: 31 additions & 34 deletions fastfuels_sdk/exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,6 @@
FeatureGridApi,
)

# Initialize API clients
_TREE_INVENTORY_API = TreeInventoryApi(get_client())
_GRIDS_API = GridsApi(get_client())
_TREE_GRID_API = TreeGridApi(get_client())
_SURFACE_GRID_API = SurfaceGridApi(get_client())
_TOPOGRAPHY_GRID_API = TopographyGridApi(get_client())
_FEATURE_GRID_API = FeatureGridApi(get_client())

# Define a mapping of (resource, sub_resource) tuples to their corresponding API methods
_API_METHODS = {
("inventories", "tree"): _TREE_INVENTORY_API.get_tree_inventory_export,
("grids", None): _GRIDS_API.get_grid_export,
("grids", "tree"): _TREE_GRID_API.get_tree_grid_export,
("grids", "surface"): _SURFACE_GRID_API.get_surface_grid_export,
("grids", "topography"): _TOPOGRAPHY_GRID_API.get_topography_grid_export,
# ("grids", "feature"): _FEATURE_GRID_API.get_feature_grid_export, # Not yet implemented
}

_FILE_NAMES = {
("inventories", "tree", "csv"): "tree_inventory.csv",
("inventories", "tree", "parquet"): "tree_inventory.parquet.zip",
("inventories", "tree", "geojson"): "tree_inventory.geojson",
("grids", None, "QUIC-Fire"): "quicfire.zip",
("grids", None, "zarr"): "grid.zarr.zip",
("grids", "tree", "zarr"): "tree_grid.zarr.zip",
("grids", "surface", "zarr"): "surface_grid.zarr.zip",
("grids", "surface", "geotiff"): "surface_grid.tif",
("grids", "topography", "zarr"): "topography_grid.zarr.zip",
("grids", "topography", "geotiff"): "topography_grid.tif",
}


class Export(ExportModel):
"""
Class for handling exports of various resources from the FastFuels API.
Expand Down Expand Up @@ -93,8 +61,37 @@ def __init__(self, **data: Any):
If the resource and sub_resource combination is not supported
"""
super().__init__(**data)

api_method = _API_METHODS.get((self.resource, self.sub_resource))
# Initialize API clients
self._TREE_INVENTORY_API = TreeInventoryApi(get_client())
self._GRIDS_API = GridsApi(get_client())
self._TREE_GRID_API = TreeGridApi(get_client())
self._SURFACE_GRID_API = SurfaceGridApi(get_client())
self._TOPOGRAPHY_GRID_API = TopographyGridApi(get_client())
self._FEATURE_GRID_API = FeatureGridApi(get_client())

# Define a mapping of (resource, sub_resource) tuples to their corresponding API methods
self._API_METHODS = {
("inventories", "tree"): self._TREE_INVENTORY_API.get_tree_inventory_export,
("grids", None): self._GRIDS_API.get_grid_export,
("grids", "tree"): self._TREE_GRID_API.get_tree_grid_export,
("grids", "surface"): self._SURFACE_GRID_API.get_surface_grid_export,
("grids", "topography"): self._TOPOGRAPHY_GRID_API.get_topography_grid_export,
# ("grids", "feature"): _FEATURE_GRID_API.get_feature_grid_export, # Not yet implemented
}

self._FILE_NAMES = {
("inventories", "tree", "csv"): "tree_inventory.csv",
("inventories", "tree", "parquet"): "tree_inventory.parquet.zip",
("inventories", "tree", "geojson"): "tree_inventory.geojson",
("grids", None, "QUIC-Fire"): "quicfire.zip",
("grids", None, "zarr"): "grid.zarr.zip",
("grids", "tree", "zarr"): "tree_grid.zarr.zip",
("grids", "surface", "zarr"): "surface_grid.zarr.zip",
("grids", "surface", "geotiff"): "surface_grid.tif",
("grids", "topography", "zarr"): "topography_grid.zarr.zip",
("grids", "topography", "geotiff"): "topography_grid.tif",
}
api_method = self._API_METHODS.get((self.resource, self.sub_resource))
if api_method is None:
raise NotImplementedError(
f"Export not implemented for resource={self.resource}, "
Expand Down
24 changes: 13 additions & 11 deletions fastfuels_sdk/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
WaterFeatureSource,
)

_FEATURES_API = FeaturesApi(get_client())
_ROAD_FEATURE_API = RoadFeatureApi(get_client())
_WATER_FEATURE_API = WaterFeatureApi(get_client())


class Features(FeaturesModel):
"""Geographic features (roads and water bodies) associated with a domain.
Expand Down Expand Up @@ -68,6 +64,12 @@ class Features(FeaturesModel):
road: Optional[RoadFeature]
water: Optional[WaterFeature]

def __init__(self, **kwargs):
super.__init__(**kwargs)
self._FEATURES_API = FeaturesApi(get_client())
self._ROAD_FEATURE_API = RoadFeatureApi(get_client())
self._WATER_FEATURE_API = WaterFeatureApi(get_client())

@classmethod
def from_domain_id(cls, domain_id: str) -> Features:
"""Retrieve the features (roads and water bodies) associated with a domain.
Expand Down Expand Up @@ -101,7 +103,7 @@ def from_domain_id(cls, domain_id: str) -> Features:
--------
Features.get : Refresh feature data
"""
features_response = _FEATURES_API.get_features(domain_id=domain_id)
features_response = self._FEATURES_API.get_features(domain_id=domain_id)
response_data = _convert_api_models_to_sdk_classes(
domain_id, features_response.model_dump()
)
Expand Down Expand Up @@ -193,7 +195,7 @@ def create_road_feature(
)

# Call API
response = _ROAD_FEATURE_API.create_road_feature(
response = self._ROAD_FEATURE_API.create_road_feature(
domain_id=self.domain_id, create_road_feature_request=request
)

Expand Down Expand Up @@ -284,7 +286,7 @@ def create_water_feature(
)

# Call API
response = _WATER_FEATURE_API.create_water_feature(
response = self._WATER_FEATURE_API.create_water_feature(
domain_id=self.domain_id, create_water_feature_request=request
)

Expand Down Expand Up @@ -396,7 +398,7 @@ def get(self, in_place: bool = False) -> RoadFeature:
>>> # Or update the existing instance
>>> road.get(in_place=True)
"""
response = _ROAD_FEATURE_API.get_road_feature(domain_id=self.domain_id)
response = self._ROAD_FEATURE_API.get_road_feature(domain_id=self.domain_id)
if in_place:
# Update all attributes of current instance
for key, value in response.model_dump().items():
Expand Down Expand Up @@ -476,7 +478,7 @@ def delete(self) -> None:
>>> # Subsequent operations will raise NotFoundException
>>> road.get() # raises NotFoundException
"""
_ROAD_FEATURE_API.delete_road_feature(domain_id=self.domain_id)
self._ROAD_FEATURE_API.delete_road_feature(domain_id=self.domain_id)
return None


Expand Down Expand Up @@ -543,7 +545,7 @@ def get(self, in_place: bool = False) -> WaterFeature:
>>> # Or update the existing instance
>>> water.get(in_place=True)
"""
response = _WATER_FEATURE_API.get_water_feature(domain_id=self.domain_id)
response = self._WATER_FEATURE_API.get_water_feature(domain_id=self.domain_id)
if in_place:
# Update all attributes of current instance
for key, value in response.model_dump().items():
Expand Down Expand Up @@ -623,7 +625,7 @@ def delete(self) -> None:
>>> # Subsequent operations will raise NotFoundException
>>> water.get() # raises NotFoundException
"""
_WATER_FEATURE_API.delete_water_feature(domain_id=self.domain_id)
self._WATER_FEATURE_API.delete_water_feature(domain_id=self.domain_id)
return None


Expand Down
14 changes: 9 additions & 5 deletions fastfuels_sdk/grids/feature_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
GridAttributeMetadataResponse,
)

_SURFACE_GRID_API = FeatureGridApi(get_client())



class FeatureGrid(FeatureGridModel):
"""Feature grid data within a domain's spatial boundaries."""

domain_id: str

def __init__(self, **kwargs):
super.__init__(**kwargs)
self._SURFACE_GRID_API = FeatureGridApi(get_client())

@classmethod
def from_domain_id(cls, domain_id: str) -> "FeatureGrid":
"""Retrieve an existing feature grid for a domain.
Expand All @@ -41,7 +45,7 @@ def from_domain_id(cls, domain_id: str) -> "FeatureGrid":
>>> print(grid.status)
'completed'
"""
response = _SURFACE_GRID_API.get_feature_grid(domain_id=domain_id)
response =self._SURFACE_GRID_API.get_feature_grid(domain_id=domain_id)
return cls(domain_id=domain_id, **response.model_dump())

def get(self, in_place: bool = False) -> "FeatureGrid":
Expand Down Expand Up @@ -69,7 +73,7 @@ def get(self, in_place: bool = False) -> "FeatureGrid":
>>> # Or update the existing instance
>>> grid.get(in_place=True)
"""
response = _SURFACE_GRID_API.get_feature_grid(domain_id=self.domain_id)
response =self._SURFACE_GRID_API.get_feature_grid(domain_id=self.domain_id)
if in_place:
# Update all attributes of current instance
for key, value in response.model_dump().items():
Expand Down Expand Up @@ -194,7 +198,7 @@ def get_attributes(self) -> GridAttributeMetadataResponse:
>>> print(metadata.shape)
[100, 100, 50]
"""
return _SURFACE_GRID_API.get_feature_grid_attribute_metadata(
return self._SURFACE_GRID_API.get_feature_grid_attribute_metadata(
domain_id=self.domain_id
)

Expand All @@ -217,5 +221,5 @@ def delete(self) -> None:
>>> # Subsequent operations will raise NotFoundException
>>> grid.get() # raises NotFoundException
"""
_SURFACE_GRID_API.delete_feature_grid(domain_id=self.domain_id)
self._SURFACE_GRID_API.delete_feature_grid(domain_id=self.domain_id)
return None
34 changes: 21 additions & 13 deletions fastfuels_sdk/grids/grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
CreateFeatureGridRequest,
)

_GRIDS_API = GridsApi(get_client())
_TREE_GRID_API = TreeGridApi(get_client())
_SURFACE_GRID_API = SurfaceGridApi(get_client())
_TOPOGRAPHY_GRID_API = TopographyGridApi(get_client())
_FEATURE_GRID_API = FeatureGridApi(get_client())
# _GRIDS_API = GridsApi(get_client())
# _TREE_GRID_API = TreeGridApi(get_client())
# _SURFACE_GRID_API = SurfaceGridApi(get_client())
# _TOPOGRAPHY_GRID_API = TopographyGridApi(get_client())
# _FEATURE_GRID_API = FeatureGridApi(get_client())


class Grids(GridsModel):
Expand Down Expand Up @@ -103,6 +103,14 @@ class Grids(GridsModel):
topography: Optional[TopographyGrid]
feature: Optional[FeatureGrid]

def __init__(self, **kwargs):
self._GRIDS_API = GridsApi(get_client())
self._TREE_GRID_API = TreeGridApi(get_client())
self._SURFACE_GRID_API = SurfaceGridApi(get_client())
self._TOPOGRAPHY_GRID_API = TopographyGridApi(get_client())
self._FEATURE_GRID_API = FeatureGridApi(get_client())
super.__init__(**kwargs)

@classmethod
def from_domain_id(cls, domain_id: str) -> Grids:
"""Retrieve the grids associated with a domain.
Expand All @@ -127,7 +135,7 @@ def from_domain_id(cls, domain_id: str) -> Grids:
>>> if grids.surface:
... print("Domain has surface grid data")
"""
grids_response = _GRIDS_API.get_grids(domain_id=domain_id)
grids_response =self._GRIDS_API.get_grids(domain_id=domain_id)
response_data = grids_response.model_dump()
response_data = _convert_api_models_to_sdk_classes(domain_id, response_data)

Expand Down Expand Up @@ -157,7 +165,7 @@ def get(self, in_place: bool = False) -> Grids:
>>> # Or update the existing instance
>>> grids.get(in_place=True)
"""
response = _GRIDS_API.get_grids(domain_id=self.domain_id)
response =self._GRIDS_API.get_grids(domain_id=self.domain_id)
response_data = response.model_dump()
response_data = _convert_api_models_to_sdk_classes(
self.domain_id, response_data
Expand Down Expand Up @@ -270,7 +278,7 @@ def create_surface_grid(
),
)

response = _SURFACE_GRID_API.create_surface_grid(
response = self._SURFACE_GRID_API.create_surface_grid(
domain_id=self.domain_id, create_surface_grid_request=request
)

Expand Down Expand Up @@ -407,7 +415,7 @@ def create_topography_grid(
aspect=(TopographyGridAspectSource.from_dict(aspect) if aspect else None),
)

response = _TOPOGRAPHY_GRID_API.create_topography_grid(
response = self._TOPOGRAPHY_GRID_API.create_topography_grid(
domain_id=self.domain_id, create_topography_grid_request=request
)

Expand Down Expand Up @@ -541,7 +549,7 @@ def create_tree_grid(
SAVR=(TreeGridSAVRSource.from_dict(savr) if savr else None),
)

response = _TREE_GRID_API.create_tree_grid(
response = self._TREE_GRID_API.create_tree_grid(
domain_id=self.domain_id, create_tree_grid_request=request
)

Expand Down Expand Up @@ -613,7 +621,7 @@ def create_feature_grid(
attributes=attributes, # type: ignore # pydantic handles this for us
)

response = _FEATURE_GRID_API.create_feature_grid(
response = self._FEATURE_GRID_API.create_feature_grid(
domain_id=self.domain_id, create_feature_grid_request=request
)

Expand Down Expand Up @@ -656,7 +664,7 @@ def create_export(self, export_format: str) -> Export:
>>> export.wait_until_completed()
>>> export.to_file("grid_data.zip")
"""
response = _GRIDS_API.create_grid_export(
response =self._GRIDS_API.create_grid_export(
domain_id=self.domain_id, export_format=export_format
)
return Export(**response.model_dump())
Expand Down Expand Up @@ -686,7 +694,7 @@ def get_export(self, export_format: str) -> Export:
>>> if export.status == "completed":
... export.to_file("grid_data.zarr")
"""
response = _GRIDS_API.get_grid_export(
response =self._GRIDS_API.get_grid_export(
domain_id=self.domain_id, export_format=export_format
)
return Export(**response.model_dump())
Expand Down
Loading
Loading