datacosmos 0.0.16__py3-none-any.whl → 0.0.18__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of datacosmos might be problematic. Click here for more details.

@@ -1,9 +1,10 @@
1
1
  """Client to interact with the Datacosmos API with authentication and request handling."""
2
2
  from __future__ import annotations
3
3
 
4
+ import logging
4
5
  import threading
5
6
  from datetime import datetime, timedelta, timezone
6
- from typing import Any, Optional
7
+ from typing import Any, Callable, List, Optional
7
8
 
8
9
  import requests
9
10
  from requests.exceptions import ConnectionError, HTTPError, RequestException, Timeout
@@ -21,6 +22,11 @@ from datacosmos.auth.m2m_authenticator import M2MAuthenticator
21
22
  from datacosmos.config.config import Config
22
23
  from datacosmos.exceptions.datacosmos_exception import DatacosmosException
23
24
 
25
+ _log = logging.getLogger(__name__)
26
+
27
+ RequestHook = Callable[[str, str, Any, Any], None]
28
+ ResponseHook = Callable[[requests.Response], None]
29
+
24
30
 
25
31
  class DatacosmosClient:
26
32
  """Client to interact with the Datacosmos API with authentication and request handling."""
@@ -31,18 +37,24 @@ class DatacosmosClient:
31
37
  self,
32
38
  config: Optional[Config | Any] = None,
33
39
  http_session: Optional[requests.Session | OAuth2Session] = None,
40
+ request_hooks: Optional[List[RequestHook]] = None,
41
+ response_hooks: Optional[List[ResponseHook]] = None,
34
42
  ):
35
43
  """Initialize the DatacosmosClient.
36
44
 
37
45
  Args:
38
46
  config (Optional[Config]): Configuration object (only needed when SDK creates its own session).
39
47
  http_session (Optional[requests.Session]): Pre-authenticated session.
48
+ request_hooks (Optional[List[RequestHook]]): A list of functions to be called before each request.
49
+ response_hooks (Optional[List[ResponseHook]]): A list of functions to be called after each successful response.
40
50
  """
41
51
  self.config = self._coerce_config(config)
42
52
  self.token: Optional[str] = None
43
53
  self.token_expiry: Optional[datetime] = None
44
54
  self._refresh_lock = threading.Lock()
45
55
  self._authenticator: Optional[BaseAuthenticator] = None
56
+ self._request_hooks = request_hooks or []
57
+ self._response_hooks = response_hooks or []
46
58
 
47
59
  if http_session is not None:
48
60
  self._init_with_injected_session(http_session)
@@ -172,11 +184,40 @@ class DatacosmosClient:
172
184
  def request(
173
185
  self, method: str, url: str, *args: Any, **kwargs: Any
174
186
  ) -> requests.Response:
175
- """Send an HTTP request using the authenticated session (with auto-refresh and retries)."""
187
+ """Send an HTTP request using the authenticated session (with auto-refresh and retries).
188
+
189
+ Args:
190
+ method (str): The HTTP method (e.g., "GET", "POST").
191
+ url (str): The URL for the request.
192
+ *args: Positional arguments for requests.request().
193
+ **kwargs: Keyword arguments for requests.request().
194
+
195
+ Returns:
196
+ requests.Response: The HTTP response.
197
+
198
+ Raises:
199
+ DatacosmosException: For any HTTP or request-related errors.
200
+ """
176
201
  self._refresh_token_if_needed()
202
+
203
+ # Call pre-request hooks
204
+ for hook in self._request_hooks:
205
+ try:
206
+ hook(method, url, *args, **kwargs)
207
+ except Exception:
208
+ _log.error("Request hook failed.", exc_info=True)
209
+
177
210
  try:
178
211
  response = self._http_client.request(method, url, *args, **kwargs)
179
212
  response.raise_for_status()
213
+
214
+ # Call post-response hooks on success
215
+ for hook in self._response_hooks:
216
+ try:
217
+ hook(response)
218
+ except Exception:
219
+ _log.error("Response hook failed.", exc_info=True)
220
+
180
221
  return response
181
222
  except HTTPError as e:
182
223
  status = getattr(e.response, "status_code", None)
@@ -3,7 +3,19 @@
3
3
  from enum import Enum
4
4
 
5
5
 
6
- class ProcessingLevel(Enum):
6
+ class CaseInsensitiveEnum(Enum):
7
+ """An enum that can be initialized with case-insensitive strings."""
8
+
9
+ @classmethod
10
+ def _missing_(cls, value: object):
11
+ if isinstance(value, str):
12
+ for member in cls:
13
+ if member.value.lower() == value.lower():
14
+ return member
15
+ return super()._missing_(value)
16
+
17
+
18
+ class ProcessingLevel(CaseInsensitiveEnum):
7
19
  """Enum class for the processing levels of the data."""
8
20
 
9
21
  RAW = "RAW"
@@ -1,28 +1,111 @@
1
1
  """Model representing a datacosmos item."""
2
2
 
3
+ import math
3
4
  from datetime import datetime
5
+ from typing import Any
4
6
 
5
- from pydantic import BaseModel
7
+ from pydantic import BaseModel, ConfigDict, field_validator, model_validator
8
+ from shapely.errors import ShapelyError
9
+ from shapely.geometry import Polygon, shape
6
10
 
11
+ from datacosmos.exceptions.datacosmos_exception import DatacosmosException
7
12
  from datacosmos.stac.enums.processing_level import ProcessingLevel
8
13
  from datacosmos.stac.item.models.asset import Asset
9
14
 
15
+ _REQUIRED_DATACOSMOS_PROPERTIES = [
16
+ "datetime",
17
+ "processing:level",
18
+ "sat:platform_international_designator",
19
+ ]
20
+
10
21
 
11
22
  class DatacosmosItem(BaseModel):
12
- """Model representing a datacosmos item."""
23
+ """Model representing a flexible Datacosmos STAC item with mandatory business fields."""
24
+
25
+ model_config = ConfigDict(extra="allow")
13
26
 
14
27
  id: str
15
28
  type: str
16
- stac_version: str
17
- stac_extensions: list | None
18
- geometry: dict
19
- properties: dict
20
- links: list
29
+ geometry: dict[str, Any]
30
+ bbox: list[float]
31
+ properties: dict[str, Any]
32
+
33
+ links: list[dict[str, Any]]
21
34
  assets: dict[str, Asset]
22
- collection: str
23
- bbox: tuple[float, float, float, float]
24
35
 
25
- def get_property(self, key: str) -> str | None:
36
+ stac_version: str | None = None
37
+ stac_extensions: list[str] | None = None
38
+ collection: str | None = None
39
+
40
+ @field_validator("properties", mode="before")
41
+ @classmethod
42
+ def validate_datacosmos_properties(
43
+ cls, properties_data: dict[str, Any]
44
+ ) -> dict[str, Any]:
45
+ """Validates that Datacosmos-specific properties exist."""
46
+ missing_keys = [
47
+ key for key in _REQUIRED_DATACOSMOS_PROPERTIES if key not in properties_data
48
+ ]
49
+
50
+ if missing_keys:
51
+ raise DatacosmosException(
52
+ f"Datacosmos-specific properties are missing: {', '.join(missing_keys)}."
53
+ )
54
+ return properties_data
55
+
56
+ @field_validator("geometry", mode="before")
57
+ @classmethod
58
+ def validate_geometry_is_polygon(
59
+ cls, geometry_data: dict[str, Any]
60
+ ) -> dict[str, Any]:
61
+ """Validates that the geometry is a Polygon with coordinates and correct winding order."""
62
+ if geometry_data.get("type") != "Polygon" or not geometry_data.get(
63
+ "coordinates"
64
+ ):
65
+ raise DatacosmosException("Geometry must be a Polygon with coordinates.")
66
+
67
+ try:
68
+ # Use shape() for robust GeoJSON parsing and validation
69
+ polygon = shape(geometry_data)
70
+
71
+ if not polygon.is_valid:
72
+ raise ValueError(f"Polygon geometry is invalid: {polygon.geom_type}")
73
+
74
+ # right-hand rule validation:
75
+ # The right-hand rule means exterior ring must be counter-clockwise (CCW).
76
+ # Shapely's Polygon stores the exterior as CCW if the input is valid.
77
+ if not polygon.exterior.is_ccw:
78
+ raise ValueError(
79
+ "Polygon winding order violates GeoJSON Right-Hand Rule (Exterior ring is clockwise)."
80
+ )
81
+
82
+ except (KeyError, ShapelyError, ValueError) as e:
83
+ raise DatacosmosException(f"Invalid geometry data: {e}") from e
84
+
85
+ return geometry_data
86
+
87
+ @model_validator(mode="after")
88
+ def validate_bbox_vs_geometry(self) -> "DatacosmosItem":
89
+ """Validates that the bbox tightly encloses the geometry."""
90
+ if self.geometry and self.bbox:
91
+ try:
92
+ geom_shape = shape(self.geometry)
93
+ true_bbox = list(geom_shape.bounds)
94
+
95
+ # Check for floating point equality within a tolerance
96
+ if not all(
97
+ math.isclose(a, b, rel_tol=1e-9)
98
+ for a, b in zip(self.bbox, true_bbox)
99
+ ):
100
+ raise DatacosmosException(
101
+ "Provided bbox does not match geometry bounds."
102
+ )
103
+ except Exception as e:
104
+ # Catch any errors from Shapely or the comparison
105
+ raise DatacosmosException(f"Invalid bbox or geometry: {e}") from e
106
+ return self
107
+
108
+ def get_property(self, key: str) -> Any | None:
26
109
  """Get a property value from the Datacosmos item."""
27
110
  return self.properties.get(key)
28
111
 
@@ -43,12 +126,13 @@ class DatacosmosItem(BaseModel):
43
126
  @property
44
127
  def sat_int_designator(self) -> str:
45
128
  """Get the satellite international designator of the Datacosmos item."""
46
- property = self.get_property("sat:platform_international_designator")
47
- if property is None:
48
- raise ValueError(
49
- "sat:platform_international_designator is missing in STAC item"
50
- )
51
- return property
129
+ return self.properties["sat:platform_international_designator"]
130
+
131
+ @property
132
+ def polygon(self) -> Polygon:
133
+ """Returns the polygon of the item."""
134
+ coordinates = self.geometry["coordinates"][0]
135
+ return Polygon(coordinates)
52
136
 
53
137
  def to_dict(self) -> dict:
54
138
  """Converts the DatacosmosItem instance to a dictionary."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: datacosmos
3
- Version: 0.0.16
3
+ Version: 0.0.18
4
4
  Summary: A library for interacting with DataCosmos from Python code
5
5
  Author-email: Open Cosmos <support@open-cosmos.com>
6
6
  Classifier: Programming Language :: Python :: 3
@@ -16,6 +16,7 @@ Requires-Dist: pystac==1.12.1
16
16
  Requires-Dist: pyyaml==6.0.2
17
17
  Requires-Dist: structlog==24.4.0
18
18
  Requires-Dist: tenacity>=8.2.3
19
+ Requires-Dist: shapely>=1.8.0
19
20
  Provides-Extra: dev
20
21
  Requires-Dist: black==22.3.0; extra == "dev"
21
22
  Requires-Dist: ruff==0.9.5; extra == "dev"
@@ -1,5 +1,5 @@
1
1
  datacosmos/__init__.py,sha256=dVHKpbz5FVtfoJAWHRdsUENG6H-vs4UrkuwnIvOGJr4,66
2
- datacosmos/datacosmos_client.py,sha256=xt_kHhP2tELNQyvSz_y3bDCSNoR705xpoAmNUh8FXG8,8975
2
+ datacosmos/datacosmos_client.py,sha256=yK9rit_AELs11ReJWm-zXx46wEWyvlDJDUOXy27bP3c,10524
3
3
  datacosmos/auth/__init__.py,sha256=ynCThS9QyLKV9miRdnjm8uF_breiGGiCcI0FaOSw_2o,45
4
4
  datacosmos/auth/base_authenticator.py,sha256=bSlb-N-vIUTl4K9KnDd3Dz21MevN_nvpWuwxgifdWBE,1814
5
5
  datacosmos/auth/local_authenticator.py,sha256=a3jIBZAMB1UlEFUZyECyJTCgSH2yjbTcrFZuUer0F90,2914
@@ -29,7 +29,7 @@ datacosmos/stac/collection/models/collection_update.py,sha256=XC6-29nLz1VGWMxYAw
29
29
  datacosmos/stac/constants/__init__.py,sha256=dDRSsF7CKqNF44yIlNdE-PD1sp0Q5mhTEPT7hHIK7YE,26
30
30
  datacosmos/stac/constants/satellite_name_mapping.py,sha256=EJqNdO9uW5B-sIeDF72AjnW7va5BM9mm4oNwijtl51w,575
31
31
  datacosmos/stac/enums/__init__.py,sha256=GUEL2xGtdjsrszrxivs0X6daxkaZs2JsTu2JoBtsvB4,22
32
- datacosmos/stac/enums/processing_level.py,sha256=kWGhpMXgzoyTzwR4yoSFd3UQ5IBw3cUf3TaM1FaOtUg,263
32
+ datacosmos/stac/enums/processing_level.py,sha256=_k4FO818VlZWtlm-rULhg-CYkbewkOdTUfqeCfHN8h4,641
33
33
  datacosmos/stac/enums/product_type.py,sha256=7lL0unJ1hxevW8Pepn9rmydUUWIORu2x4MEtp6rSFbA,196
34
34
  datacosmos/stac/enums/season.py,sha256=QvUzXBYtPEfixhlbV0SAw2u_HK3tRFEnHKshJyIatdg,241
35
35
  datacosmos/stac/item/__init__.py,sha256=lRuD_yp-JxoLqBA23q0XMkCNImf4T-X3BJnSw9u_3Yk,200
@@ -37,7 +37,7 @@ datacosmos/stac/item/item_client.py,sha256=HCHl3cHp0u2qxbwLxPk0xkujC1D4uwIBIFI-f
37
37
  datacosmos/stac/item/models/__init__.py,sha256=bcOrOcIxGxGBrRVIyQVxSM3C3Xj_qzxIHgQeWo6f7Q8,34
38
38
  datacosmos/stac/item/models/asset.py,sha256=mvg_fenYCGOTMGwXXpK2nyqBk5RMsUYxl6KhQTWW_b0,631
39
39
  datacosmos/stac/item/models/catalog_search_parameters.py,sha256=3HrUm37VezujwuCR45jhMryS5m1FGc1XmX8-fdTy4jU,4870
40
- datacosmos/stac/item/models/datacosmos_item.py,sha256=AImz0GRxrpZfIETdzzNfaKX35wpr39Q4f4u0z6r8eys,1745
40
+ datacosmos/stac/item/models/datacosmos_item.py,sha256=MJXUJu_kNJIoaTfIG05OlSys9th3KI4hgbzj2m2xI0Q,5027
41
41
  datacosmos/stac/item/models/eo_band.py,sha256=YC3Scn_wFhIo51pIVcJeuJienF7JGWoEv39JngDM6rI,309
42
42
  datacosmos/stac/item/models/item_update.py,sha256=_CpjQn9SsfedfuxlHSiGeptqY4M-p15t9YX__mBRueI,2088
43
43
  datacosmos/stac/item/models/raster_band.py,sha256=CoEVs-YyPE5Fse0He9DdOs4dGZpzfCsCuVzOcdXa_UM,354
@@ -54,8 +54,8 @@ datacosmos/utils/http_response/check_api_response.py,sha256=dKWW01jn2_lWV0xpOBAB
54
54
  datacosmos/utils/http_response/models/__init__.py,sha256=Wj8YT6dqw7rAz_rctllxo5Or_vv8DwopvQvBzwCTvpw,45
55
55
  datacosmos/utils/http_response/models/datacosmos_error.py,sha256=Uqi2uM98nJPeCbM7zngV6vHSk97jEAb_nkdDEeUjiQM,740
56
56
  datacosmos/utils/http_response/models/datacosmos_response.py,sha256=oV4n-sue7K1wwiIQeHpxdNU8vxeqF3okVPE2rydw5W0,336
57
- datacosmos-0.0.16.dist-info/licenses/LICENSE.md,sha256=vpbRI-UUbZVQfr3VG_CXt9HpRnL1b5kt8uTVbirxeyI,1486
58
- datacosmos-0.0.16.dist-info/METADATA,sha256=WiWMHwzjJKXyW5-s_qDyuZW4WogZGh4f9HmkwMslc6I,970
59
- datacosmos-0.0.16.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
60
- datacosmos-0.0.16.dist-info/top_level.txt,sha256=ueobs5CNeyDbPMgXPcVV0d0yNdm8CvGtDT3CaksRVtA,11
61
- datacosmos-0.0.16.dist-info/RECORD,,
57
+ datacosmos-0.0.18.dist-info/licenses/LICENSE.md,sha256=vpbRI-UUbZVQfr3VG_CXt9HpRnL1b5kt8uTVbirxeyI,1486
58
+ datacosmos-0.0.18.dist-info/METADATA,sha256=rTT3QYoHGLf1LxLT7i11oLG68l0KUjXaYeC07hFvjOs,1000
59
+ datacosmos-0.0.18.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
60
+ datacosmos-0.0.18.dist-info/top_level.txt,sha256=ueobs5CNeyDbPMgXPcVV0d0yNdm8CvGtDT3CaksRVtA,11
61
+ datacosmos-0.0.18.dist-info/RECORD,,