rs-client-libraries 0.2__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.
Files changed (62) hide show
  1. config/staging_templates/README.md +17 -0
  2. config/staging_templates/yaml/additionalParameter.yaml +30 -0
  3. config/staging_templates/yaml/bbox.yaml +34 -0
  4. config/staging_templates/yaml/binaryInputValue.yaml +15 -0
  5. config/staging_templates/yaml/confClasses.yaml +22 -0
  6. config/staging_templates/yaml/descriptionType.yaml +36 -0
  7. config/staging_templates/yaml/dpr_openapi_schema.yaml +223 -0
  8. config/staging_templates/yaml/exception.yaml +31 -0
  9. config/staging_templates/yaml/execute.yaml +34 -0
  10. config/staging_templates/yaml/format.yaml +25 -0
  11. config/staging_templates/yaml/inlineOrRefData.yaml +18 -0
  12. config/staging_templates/yaml/inputDescription.yaml +32 -0
  13. config/staging_templates/yaml/inputValue.yaml +17 -0
  14. config/staging_templates/yaml/inputValueNoObject.yaml +22 -0
  15. config/staging_templates/yaml/jobControlOptions.yaml +19 -0
  16. config/staging_templates/yaml/jobList.yaml +27 -0
  17. config/staging_templates/yaml/landingPage.yaml +28 -0
  18. config/staging_templates/yaml/link.yaml +31 -0
  19. config/staging_templates/yaml/metadata.yaml +22 -0
  20. config/staging_templates/yaml/output.yaml +20 -0
  21. config/staging_templates/yaml/outputDescription.yaml +22 -0
  22. config/staging_templates/yaml/process.yaml +23 -0
  23. config/staging_templates/yaml/processList.yaml +27 -0
  24. config/staging_templates/yaml/processSummary.yaml +37 -0
  25. config/staging_templates/yaml/qualifiedInputValue.yaml +22 -0
  26. config/staging_templates/yaml/reference.yaml +21 -0
  27. config/staging_templates/yaml/results.yaml +16 -0
  28. config/staging_templates/yaml/schema.yaml +144 -0
  29. config/staging_templates/yaml/staging_openapi_schema.yaml +223 -0
  30. config/staging_templates/yaml/statusCode.yaml +22 -0
  31. config/staging_templates/yaml/statusInfo.yaml +52 -0
  32. config/staging_templates/yaml/subscriber.yaml +33 -0
  33. config/staging_templates/yaml/transmissionMode.yaml +19 -0
  34. rs_client/__init__.py +18 -0
  35. rs_client/ogcapi/__init__.py +15 -0
  36. rs_client/ogcapi/dpr_client.py +231 -0
  37. rs_client/ogcapi/ogcapi_client.py +282 -0
  38. rs_client/ogcapi/staging_client.py +178 -0
  39. rs_client/rs_client.py +277 -0
  40. rs_client/stac/__init__.py +15 -0
  41. rs_client/stac/auxip_client.py +70 -0
  42. rs_client/stac/cadip_client.py +70 -0
  43. rs_client/stac/catalog_client.py +479 -0
  44. rs_client/stac/stac_base.py +313 -0
  45. rs_client_libraries-0.2.dist-info/LICENSE +201 -0
  46. rs_client_libraries-0.2.dist-info/METADATA +44 -0
  47. rs_client_libraries-0.2.dist-info/RECORD +62 -0
  48. rs_client_libraries-0.2.dist-info/WHEEL +4 -0
  49. rs_common/__init__.py +20 -0
  50. rs_common/config.py +69 -0
  51. rs_common/init_opentelemetry.py +250 -0
  52. rs_common/logging.py +107 -0
  53. rs_common/prefect_utils.py +407 -0
  54. rs_common/utils.py +70 -0
  55. rs_workflows/__init__.py +15 -0
  56. rs_workflows/auxip_flow.py +70 -0
  57. rs_workflows/cadip_flow.py +69 -0
  58. rs_workflows/catalog_flow.py +71 -0
  59. rs_workflows/dpr_flow.py +272 -0
  60. rs_workflows/flow_utils.py +127 -0
  61. rs_workflows/on_demand_processing.py +243 -0
  62. rs_workflows/staging_flow.py +78 -0
@@ -0,0 +1,479 @@
1
+ # Copyright 2024 CS Group
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Implement the class CatalogClient that inherits from pystact_client Client."""
16
+
17
+ from __future__ import annotations
18
+
19
+ import getpass
20
+ import logging
21
+ import re
22
+ from collections.abc import Iterator
23
+
24
+ from pystac import Collection, Item, Link, RelType
25
+ from pystac_client import Client
26
+ from pystac_client.collection_client import CollectionClient
27
+ from pystac_client.item_search import ItemSearch
28
+ from requests import HTTPError, Response
29
+
30
+ from rs_client.rs_client import TIMEOUT
31
+ from rs_client.stac.stac_base import StacBase
32
+ from rs_common import utils
33
+ from rs_common.utils import get_href_service
34
+
35
+
36
+ class CatalogClient(StacBase): # type: ignore # pylint: disable=too-many-ancestors
37
+ """CatalogClient inherits from both rs_client.RsClient and pystac_client.Client. The goal of this class is to
38
+ allow an user to use RS-Server services more easily than calling REST endpoints directly.
39
+
40
+ Attributes:
41
+ owner_id (str): The owner of the STAC catalog collections (no special characters allowed).
42
+ If not set, we try to read it from the RSPY_HOST_USER environment variable. If still not set:
43
+ - In local mode, it takes the system username.
44
+ - In cluster mode, it is deduced from the API key or OAuth2 login = your keycloak username.
45
+ - In hybrid mode, we raise an Exception.
46
+ If owner_id is different than your keycloak username, then make sure that your keycloak account has
47
+ the rights to read/write on this catalog owner.
48
+ owner_id is also used in the RS-Client logging.
49
+ """
50
+
51
+ ##################
52
+ # Initialisation #
53
+ ##################
54
+
55
+ def __init__( # pylint: disable=too-many-arguments, too-many-positional-arguments
56
+ self,
57
+ rs_server_href: str | None,
58
+ rs_server_api_key: str | None,
59
+ owner_id: str | None,
60
+ logger: logging.Logger | None = None,
61
+ **kwargs,
62
+ ):
63
+ """CatalogClient class constructor.
64
+
65
+ Args:
66
+ rs_server_href (str | None): The URL of the RS-Server. Pass None for local mode.
67
+ rs_server_api_key (str | None, optional): API key for authentication (default: None).
68
+ owner_id (str | None, optional): ID of the catalog owner (default: None).
69
+ logger (logging.Logger | None, optional): Logger instance (default: None).
70
+
71
+ Raises:
72
+ RuntimeError: If neither an API key nor an OAuth2 cookie is provided for RS-Server authentication.
73
+ RuntimeError: If the computed owner ID is empty or contains only special characters.
74
+ """
75
+ super().__init__(
76
+ rs_server_href,
77
+ rs_server_api_key,
78
+ owner_id,
79
+ logger,
80
+ get_href_service(rs_server_href, "RSPY_HOST_CATALOG") + "/catalog/",
81
+ **kwargs,
82
+ )
83
+
84
+ # Determine automatically the owner id
85
+ if not self.owner_id:
86
+ # In local mode, we use the local system username
87
+ if self.local_mode:
88
+ self.owner_id = getpass.getuser()
89
+
90
+ # In hybrid mode, the API Key Manager check URL is not accessible and there is no OAuth2
91
+ # so the owner id must be set explicitly by the user.
92
+ elif self.hybrid_mode:
93
+ raise RuntimeError(
94
+ "In hybrid mode, the owner_id must be set explicitly by parameter or environment variable",
95
+ )
96
+
97
+ # In cluster mode, we retrieve the OAuth2 or API key login
98
+ else:
99
+ self.owner_id = self.apikey_user_login if self.rs_server_api_key else self.oauth2_user_login
100
+
101
+ # Remove special characters
102
+ self.owner_id = re.sub(r"[^a-zA-Z0-9]+", "", self.owner_id)
103
+
104
+ if not self.owner_id:
105
+ raise RuntimeError("The owner ID is empty or only contains special characters")
106
+
107
+ self.logger.debug(f"Owner ID: {self.owner_id!r}")
108
+
109
+ ##############
110
+ # Properties #
111
+ ##############
112
+
113
+ @property
114
+ def href_service(self) -> str:
115
+ """
116
+ Return the RS-Server Catalog URL hostname.
117
+ This URL can be overwritten using the RSPY_HOST_CATALOG env variable (used e.g. for local mode).
118
+ Otherwise it should just be the RS-Server URL.
119
+ """
120
+
121
+ return get_href_service(self.rs_server_href, "RSPY_HOST_CATALOG")
122
+
123
+ def full_collection_id(self, owner_id: str | None, collection_id: str, concat_char: str | None = None) -> str:
124
+ """
125
+ Generates a full collection identifier by concatenating the owner ID and collection ID.
126
+
127
+ This function constructs a full collection ID by combining the provided `owner_id` (or a
128
+ default owner ID from `self.owner_id`) with `collection_id` using a specified separator.
129
+
130
+ Parameters:
131
+ owner_id (str | None): The owner identifier. If `None`, it defaults to `self.owner_id`.
132
+ collection_id (str): The collection identifier that must always be provided.
133
+ concat_char (str | None, optional): The character used to concatenate `owner_id`
134
+ and `collection_id`. Defaults to ":".
135
+
136
+ Returns:
137
+ str: A string representing the full collection ID, formatted as:
138
+ `"owner_id:collection_id"` by default or using the specified `concat_char`, that may
139
+ be `_`.
140
+
141
+ Raises:
142
+ - **AttributeError**: If `self.owner_id` is not set and `owner_id` is `None`,
143
+ causing an attempt to concatenate a `NoneType` with a string.
144
+
145
+ Notes:
146
+ - This function is useful in scenarios where collections are stored with unique
147
+ identifiers that require owner prefixes for proper scoping.
148
+ """
149
+
150
+ if not concat_char:
151
+ concat_char = ":"
152
+ return f"{owner_id or self.owner_id}{concat_char}{collection_id}"
153
+
154
+ #####################
155
+ # Utility functions #
156
+ #####################
157
+
158
+ def raise_for_status(self, response: Response, ignore: list[int] | None = None):
159
+ """
160
+ Raises :class:`HTTPError`, if one occurred.
161
+
162
+ Args:
163
+ response: HTTP response
164
+ ignore: ignore error its status code is in this list
165
+ """
166
+ if ignore and (response.status_code in ignore):
167
+ return
168
+ try:
169
+ response.raise_for_status()
170
+ except HTTPError as error:
171
+ message = f"{error.args[0]}\nDetail: {utils.read_response_error(response)}"
172
+ raise HTTPError(message, response=response) # pylint: disable=raise-missing-from
173
+
174
+ def clear_collection_cache(self):
175
+ """Clear the lru_caches because they still contains the old collection."""
176
+ StacBase.get_collection.cache_clear() # pylint: disable=no-member
177
+ Client.get_collection.cache_clear() # pylint: disable=no-member
178
+
179
+ ################################
180
+ # Specific STAC implementation #
181
+ ################################
182
+
183
+ # STAC read opperations. These can be done with pystac_client (by calling super StacBase functions)
184
+
185
+ def get_collection( # type: ignore
186
+ self,
187
+ collection_id: str,
188
+ owner_id: str | None = None,
189
+ ) -> Collection | CollectionClient:
190
+ """Get the requested collection"""
191
+ return super().get_collection(self.full_collection_id(owner_id, collection_id, ":"))
192
+
193
+ def get_items(
194
+ self,
195
+ collection_id: str,
196
+ items_ids: list[str] | None = None,
197
+ owner_id: str | None = None,
198
+ ) -> Iterator[Item]:
199
+ """Get all items from a specific collection."""
200
+ return super().get_items(self.full_collection_id(owner_id, collection_id, ":"), items_ids)
201
+
202
+ def get_item(self, collection_id: str, item_id: str, owner_id: str | None = None):
203
+ """Get an item from a specific collection."""
204
+ return super().get_item(self.full_collection_id(owner_id, collection_id, ":"), item_id)
205
+
206
+ def search( # type: ignore # pylint: disable=too-many-arguments, arguments-differ
207
+ self,
208
+ **kwargs,
209
+ ) -> ItemSearch | None:
210
+ """Search items inside a specific collection."""
211
+
212
+ if "collections" in kwargs:
213
+ kwargs["collections"] = [
214
+ self.full_collection_id(kwargs.get("owner_id"), collection, "_") for collection in kwargs["collections"]
215
+ ] # type: ignore
216
+ return super().search(**kwargs) # type: ignore
217
+
218
+ # end of STAC read opperations
219
+
220
+ # STAC write opperations. These can't be done with pystac_client
221
+ # - add_collection
222
+ # - remove_collection
223
+ # - update_collection
224
+ # - add_item
225
+ # - remove_item
226
+ # - update_item
227
+
228
+ def add_collection(
229
+ self,
230
+ collection: Collection,
231
+ add_public_license: bool = True,
232
+ owner_id: str | None = None,
233
+ timeout: int = TIMEOUT,
234
+ ) -> Response:
235
+ """Update the collection links, then post the collection into the catalog.
236
+
237
+ Args:
238
+ collection (Collection): STAC collection
239
+ add_public_license (bool): If True, add a public domain license field and link.
240
+ owner_id (str, optional): Collection owner ID. If missing, we use self.owner_id.
241
+ timeout (int): The timeout duration for the HTTP request.
242
+
243
+ Returns:
244
+ JSONResponse (json): The response of the request.
245
+
246
+ Raises:
247
+ HTTPError in case of server error.
248
+ """
249
+
250
+ full_owner_id = owner_id or self.owner_id
251
+
252
+ # Use owner_id:collection_id instead of just the collection ID, before adding the links,
253
+ # so the links contain the full owner_id:collection_id
254
+ short_collection_id = collection.id
255
+ full_collection_id = self.full_collection_id(owner_id, short_collection_id)
256
+ collection.id = full_collection_id
257
+
258
+ # Default description
259
+ if not collection.description:
260
+ collection.description = f"This is the collection {short_collection_id} from user {full_owner_id}."
261
+
262
+ # Add the owner_id as an extra field
263
+ collection.extra_fields["owner"] = full_owner_id
264
+
265
+ # Add public domain license
266
+ if add_public_license:
267
+ collection.license = "public-domain"
268
+ collection.add_link(
269
+ Link(
270
+ rel=RelType.LICENSE,
271
+ target="https://creativecommons.org/licenses/publicdomain/",
272
+ title="public-domain",
273
+ ),
274
+ )
275
+
276
+ # Restore the short collection_id at the root of the collection
277
+ collection.id = short_collection_id
278
+
279
+ # Check that the collection is compliant to STAC
280
+ collection.validate_all()
281
+
282
+ # Post the collection to the catalog
283
+ response = self.http_session.post(
284
+ f"{self.href_service}/catalog/collections",
285
+ json=collection.to_dict(),
286
+ **self.apikey_headers,
287
+ timeout=timeout,
288
+ )
289
+ self.raise_for_status(response)
290
+ self.clear_collection_cache()
291
+ return response
292
+
293
+ def remove_collection(
294
+ self,
295
+ collection_id: str,
296
+ owner_id: str | None = None,
297
+ timeout: int = TIMEOUT,
298
+ ) -> Response:
299
+ """Remove/delete a collection from the catalog.
300
+
301
+ Args:
302
+ collection_id (str): The collection id.
303
+ owner_id (str, optional): Collection owner ID. If missing, we use self.owner_id.
304
+ timeout (int): The timeout duration for the HTTP request.
305
+
306
+ Returns:
307
+ JSONResponse (json): The response of the request.
308
+
309
+ Raises:
310
+ HTTPError in case of server error.
311
+ """
312
+ # owner_id:collection_id
313
+ full_collection_id = self.full_collection_id(owner_id, collection_id)
314
+
315
+ # Remove the collection from the server catalog
316
+ response = self.http_session.delete(
317
+ f"{self.href_service}/catalog/collections/{full_collection_id}",
318
+ **self.apikey_headers,
319
+ timeout=timeout,
320
+ )
321
+ self.raise_for_status(response, ignore=[404])
322
+ self.clear_collection_cache()
323
+ return response
324
+
325
+ def update_collection(
326
+ self,
327
+ collection: Collection | CollectionClient | dict,
328
+ timeout: int = TIMEOUT,
329
+ ) -> Response:
330
+ """Put/update a collection in the catalog.
331
+
332
+ Args:
333
+ collection: The collection contents.
334
+ timeout (int): The timeout duration for the HTTP request.
335
+
336
+ Returns:
337
+ JSONResponse (json): The response of the request.
338
+
339
+ Raises:
340
+ HTTPError in case of server error.
341
+ """
342
+
343
+ # Convert to dict
344
+ col_dict: dict = collection if isinstance(collection, dict) else collection.to_dict()
345
+
346
+ # Get the collection owner_id and remove it from the collection id
347
+ # which is like <owner_id>_<short_collection_id>
348
+ owner_id = col_dict["owner"]
349
+ collection_id = col_dict["id"].removeprefix(f"{owner_id}_")
350
+ col_dict["id"] = collection_id
351
+
352
+ # owner_id:collection_id
353
+ full_collection_id = self.full_collection_id(owner_id, collection_id)
354
+
355
+ # Update the collection in the server catalog
356
+ response = self.http_session.put(
357
+ f"{self.href_service}/catalog/collections/{full_collection_id}",
358
+ json=col_dict,
359
+ **self.apikey_headers,
360
+ timeout=timeout,
361
+ )
362
+ self.raise_for_status(response)
363
+ self.clear_collection_cache()
364
+ return response
365
+
366
+ def add_item( # type: ignore # pylint: disable=arguments-renamed
367
+ self,
368
+ collection_id: str,
369
+ item: Item,
370
+ owner_id: str | None = None,
371
+ timeout: int = TIMEOUT,
372
+ ) -> Response:
373
+ """Update the item links, then post the item into the catalog.
374
+
375
+ Args:
376
+ collection_id (str): The collection id.
377
+ item (Item): STAC item to update and post
378
+ owner_id (str, optional): Collection owner ID. If missing, we use self.owner_id.
379
+ timeout (int): The timeout duration for the HTTP request.
380
+
381
+ Returns:
382
+ JSONResponse (json): The response of the request.
383
+
384
+ Raises:
385
+ HTTPError in case of server error.
386
+ """
387
+ # owner_id:collection_id
388
+ full_collection_id = self.full_collection_id(owner_id, collection_id)
389
+
390
+ # Check that the item is compliant to STAC
391
+ item.validate()
392
+
393
+ # Post the item to the catalog
394
+ response = self.http_session.post(
395
+ f"{self.href_service}/catalog/collections/{full_collection_id}/items",
396
+ json=item.to_dict(),
397
+ **self.apikey_headers,
398
+ timeout=timeout,
399
+ )
400
+ self.raise_for_status(response)
401
+ self.clear_collection_cache()
402
+ return response
403
+
404
+ def remove_item( # type: ignore # pylint: disable=arguments-differ
405
+ self,
406
+ collection_id: str,
407
+ item_id: str,
408
+ owner_id: str | None = None,
409
+ timeout: int = TIMEOUT,
410
+ ) -> Response:
411
+ """Remove/delete an item from a collection.
412
+
413
+ Args:
414
+ collection_id (str): The collection id.
415
+ item_id (str): The item id.
416
+ owner_id (str, optional): Collection owner ID. If missing, we use self.owner_id.
417
+ timeout (int): The timeout duration for the HTTP request.
418
+
419
+ Returns:
420
+ JSONResponse (json): The response of the request.
421
+
422
+ Raises:
423
+ HTTPError in case of server error.
424
+ """
425
+ # owner_id:collection_id
426
+ full_collection_id = self.full_collection_id(owner_id, collection_id)
427
+
428
+ # Remove the collection from the server catalog
429
+ response = self.http_session.delete(
430
+ f"{self.href_service}/catalog/collections/{full_collection_id}/items/{item_id}",
431
+ **self.apikey_headers,
432
+ timeout=timeout,
433
+ )
434
+ self.raise_for_status(response)
435
+ self.clear_collection_cache()
436
+ return response
437
+
438
+ def update_item(
439
+ self,
440
+ item: Item | dict,
441
+ timeout: int = TIMEOUT,
442
+ ) -> Response:
443
+ """Put/update an item in the catalog.
444
+
445
+ Args:
446
+ item: The item contents.
447
+ timeout (int): The timeout duration for the HTTP request.
448
+
449
+ Returns:
450
+ JSONResponse (json): The response of the request.
451
+
452
+ Raises:
453
+ HTTPError in case of server error.
454
+ """
455
+
456
+ # Convert to dict
457
+ item_dict: dict = item if isinstance(item, dict) else item.to_dict()
458
+
459
+ # Get the collection owner_id and remove it from the collection id
460
+ # which is like <owner_id>_<short_collection_id>
461
+ owner_id = item_dict["properties"]["owner"]
462
+ collection_id = item_dict["collection"].removeprefix(f"{owner_id}_")
463
+ item_dict["collection"] = collection_id
464
+
465
+ # owner_id:collection_id
466
+ full_collection_id = self.full_collection_id(owner_id, collection_id)
467
+
468
+ # Update the item in the server catalog
469
+ response = self.http_session.put(
470
+ f"{self.href_service}/catalog/collections/{full_collection_id}/items/{item_dict['id']}",
471
+ json=item_dict,
472
+ **self.apikey_headers,
473
+ timeout=timeout,
474
+ )
475
+ self.raise_for_status(response)
476
+ self.clear_collection_cache()
477
+ return response
478
+
479
+ # end of STAC write opperations