rs-client-libraries 0.2a13__tar.gz → 0.2a15__tar.gz

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 (30) hide show
  1. {rs_client_libraries-0.2a13 → rs_client_libraries-0.2a15}/PKG-INFO +12 -7
  2. {rs_client_libraries-0.2a13 → rs_client_libraries-0.2a15}/pyproject.toml +35 -13
  3. {rs_client_libraries-0.2a13 → rs_client_libraries-0.2a15}/rs_client/__init__.py +1 -1
  4. rs_client_libraries-0.2a15/rs_client/ogcapi/__init__.py +15 -0
  5. rs_client_libraries-0.2a15/rs_client/ogcapi/dpr_client.py +217 -0
  6. rs_client_libraries-0.2a15/rs_client/ogcapi/ogcapi_client.py +283 -0
  7. rs_client_libraries-0.2a15/rs_client/ogcapi/staging_client.py +179 -0
  8. {rs_client_libraries-0.2a13 → rs_client_libraries-0.2a15}/rs_client/rs_client.py +19 -44
  9. rs_client_libraries-0.2a15/rs_client/stac/__init__.py +15 -0
  10. {rs_client_libraries-0.2a13/rs_client → rs_client_libraries-0.2a15/rs_client/stac}/auxip_client.py +2 -4
  11. {rs_client_libraries-0.2a13/rs_client → rs_client_libraries-0.2a15/rs_client/stac}/cadip_client.py +2 -4
  12. {rs_client_libraries-0.2a13/rs_client → rs_client_libraries-0.2a15/rs_client/stac}/catalog_client.py +51 -3
  13. {rs_client_libraries-0.2a13/rs_client → rs_client_libraries-0.2a15/rs_client/stac}/stac_base.py +2 -2
  14. {rs_client_libraries-0.2a13 → rs_client_libraries-0.2a15}/rs_common/init_opentelemetry.py +82 -73
  15. rs_client_libraries-0.2a15/rs_common/prefect_utils.py +420 -0
  16. {rs_client_libraries-0.2a13 → rs_client_libraries-0.2a15}/rs_common/utils.py +2 -2
  17. rs_client_libraries-0.2a15/rs_workflows/__init__.py +15 -0
  18. rs_client_libraries-0.2a15/rs_workflows/auxip_flow.py +70 -0
  19. rs_client_libraries-0.2a15/rs_workflows/cadip_flow.py +69 -0
  20. rs_client_libraries-0.2a15/rs_workflows/catalog_flow.py +71 -0
  21. rs_client_libraries-0.2a15/rs_workflows/dpr_flow.py +272 -0
  22. rs_client_libraries-0.2a15/rs_workflows/flow_utils.py +127 -0
  23. rs_client_libraries-0.2a15/rs_workflows/on_demand_processing.py +134 -0
  24. rs_client_libraries-0.2a15/rs_workflows/staging_flow.py +78 -0
  25. rs_client_libraries-0.2a13/rs_client/staging_client.py +0 -327
  26. {rs_client_libraries-0.2a13 → rs_client_libraries-0.2a15}/LICENSE +0 -0
  27. {rs_client_libraries-0.2a13 → rs_client_libraries-0.2a15}/README.adoc +0 -0
  28. {rs_client_libraries-0.2a13 → rs_client_libraries-0.2a15}/rs_common/__init__.py +0 -0
  29. {rs_client_libraries-0.2a13 → rs_client_libraries-0.2a15}/rs_common/config.py +0 -0
  30. {rs_client_libraries-0.2a13 → rs_client_libraries-0.2a15}/rs_common/logging.py +0 -0
@@ -1,8 +1,7 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: rs-client-libraries
3
- Version: 0.2a13
3
+ Version: 0.2a15
4
4
  Summary: Copernicus Sentinel rs-client library and processing workflows.
5
- Home-page: https://home.rs-python.eu/
6
5
  License: Apache-2.0
7
6
  Keywords: stac,copernicus,sentinel,processing
8
7
  Author: ESA under contract with Airbus and CS
@@ -20,17 +19,23 @@ Classifier: Programming Language :: Python :: 3.13
20
19
  Classifier: Topic :: Scientific/Engineering :: Image Processing
21
20
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
21
  Requires-Dist: cachetools (>=5.5.2,<6.0.0)
22
+ Requires-Dist: click (==8.1.8)
23
23
  Requires-Dist: openapi-core (>=0.19.5,<0.20.0)
24
- Requires-Dist: opentelemetry-distro (>=0.53b1,<0.54)
25
- Requires-Dist: opentelemetry-exporter-otlp (>=1.32.1,<2.0.0)
26
- Requires-Dist: opentelemetry-instrumentation-aws-lambda (>=0.53b1,<0.54)
27
- Requires-Dist: opentelemetry-test-utils (>=0.53b1,<0.54)
24
+ Requires-Dist: opentelemetry-distro (>=0.55b1,<0.56)
25
+ Requires-Dist: opentelemetry-exporter-otlp (>=1.34.1,<2.0.0)
26
+ Requires-Dist: opentelemetry-instrumentation-aws-lambda (>=0.55b1,<0.56)
27
+ Requires-Dist: opentelemetry-test-utils (>=0.55b1,<0.56)
28
+ Requires-Dist: prefect[aws] (==3.2.13)
29
+ Requires-Dist: protobuf (>=5.29.5)
28
30
  Requires-Dist: pystac (>=1.13.0,<2.0.0)
29
31
  Requires-Dist: pystac-client (>=0.8.6,<0.9.0)
30
32
  Requires-Dist: python-dateutil (>=2.9.0.post0,<3.0.0)
31
33
  Requires-Dist: python-logging-loki (>=0.3.1,<0.4.0)
34
+ Requires-Dist: requests (>=2.32.4,<3.0.0)
32
35
  Requires-Dist: stac-pydantic (>=3.1.5,<4.0.0)
36
+ Requires-Dist: urllib3 (>=2.5.0)
33
37
  Project-URL: Documentation, https://home.rs-python.eu/rs-documentation/rs-client-libraries/docs/doc/
38
+ Project-URL: Homepage, https://home.rs-python.eu/
34
39
  Project-URL: Repository, https://github.com/RS-PYTHON/rs-client-libraries
35
40
  Description-Content-Type: text/plain
36
41
 
@@ -1,10 +1,10 @@
1
1
  [project]
2
2
  name = "rs-client-libraries"
3
- version = "0.0.0" # see: https://github.com/mtkennerly/poetry-dynamic-versioning
3
+ dynamic = ["version"] # see: https://github.com/mtkennerly/poetry-dynamic-versioning
4
4
 
5
5
  [tool.poetry]
6
6
  name = "rs-client-libraries"
7
- version = "0.2a13"
7
+ version = "0.2a15"
8
8
  description = "Copernicus Sentinel rs-client library and processing workflows."
9
9
  license = "Apache-2.0"
10
10
  authors = ["ESA under contract with Airbus and CS <ops_coprs@airbus.com>"]
@@ -27,6 +27,7 @@ classifiers = [
27
27
  packages = [
28
28
  { include = "rs_client" },
29
29
  { include = "rs_common" },
30
+ { include = "rs_workflows" },
30
31
  ]
31
32
 
32
33
  # Add configuration files to the .whl package
@@ -36,10 +37,11 @@ include = [
36
37
 
37
38
  [tool.poetry.dependencies]
38
39
  python = "^3.11"
39
- opentelemetry-distro = "^0.53b1" # then run 'poetry run opentelemetry-bootstrap -a install'
40
- opentelemetry-instrumentation-aws-lambda = "^0.53b1"
41
- opentelemetry-test-utils = "^0.53b1"
42
- opentelemetry-exporter-otlp = "^1.32.1"
40
+ opentelemetry-distro = "^0.55b1" # then run 'poetry run opentelemetry-bootstrap -a install'
41
+ opentelemetry-instrumentation-aws-lambda = "^0.55b1"
42
+ opentelemetry-test-utils = "^0.55b1"
43
+ opentelemetry-exporter-otlp = "^1.34.1"
44
+ protobuf = ">=5.29.5"
43
45
  python-logging-loki = "^0.3.1" # to avoid otl error: No module named 'logging_loki'
44
46
  cachetools = "^5.5.2"
45
47
  pystac = "^1.13.0"
@@ -47,20 +49,24 @@ pystac-client = "^0.8.6"
47
49
  python-dateutil = "^2.9.0.post0"
48
50
  stac-pydantic = "^3.1.5"
49
51
  openapi-core = "^0.19.5"
52
+ click = "8.1.8"
53
+ prefect = { version = "3.2.13", extras = ["aws"] }
54
+ requests = "^2.32.4"
55
+ urllib3 = ">=2.5.0"
50
56
 
51
57
  [tool.poetry.group.dev]
52
58
  optional = true
53
59
 
54
60
  [tool.poetry.group.dev.dependencies]
55
61
  pytest = "^8.3.5"
56
- pytest-cov = "^6.0.0"
62
+ pytest-cov = "^6.2.1"
57
63
  pytest-mock = "^3.14.0"
58
64
  pytest-error-for-skips = "^2.0.2"
59
65
  black = "==25.1.0"
60
- mypy = "^1.15.0"
66
+ mypy = "^1.16.0"
61
67
  pre-commit = "^4.1.0"
62
- bandit = "^1.8.2"
63
- safety = "^3.2.14"
68
+ bandit = "^1.8.3"
69
+ safety = "^3.5.2"
64
70
  pylint = "^3.3.4"
65
71
  pytest-docker = "^3.1.2"
66
72
  sphinx = "^8.1.3"
@@ -69,13 +75,18 @@ flake8 = "^7.1.1"
69
75
  flake8-pyproject = "^1.2.3"
70
76
  responses = "^0.25.7"
71
77
  starlette = "^0.45.3"
78
+ pytest-asyncio = "^1.0.0"
72
79
 
73
80
  [tool.poetry.group.demo.dependencies] # for hybrid mode
74
81
  jupyterlab = "^4.2.1"
75
- boto3 = "^1.37.11"
82
+ boto3 = "^1.38.34"
76
83
  papermill = "^2.6.0"
84
+ tornado = "^6.5" # fix trivy vulnerability
77
85
 
78
- [tool.poetry-dynamic-versioning] # needs: poetry self add "poetry-dynamic-versioning[plugin]"
86
+ [tool.poetry.requires-plugins]
87
+ poetry-dynamic-versioning = { version = ">=1.0.0,<2.0.0", extras = ["plugin"] }
88
+
89
+ [tool.poetry-dynamic-versioning]
79
90
  enable = false
80
91
  metadata = false
81
92
 
@@ -91,6 +102,7 @@ markers = [
91
102
  "unit", # mark a test as unittest
92
103
  "integration", # mark a test as integration test
93
104
  ]
105
+ asyncio_default_fixture_loop_scope = "function"
94
106
  asyncio_mode = "auto"
95
107
 
96
108
  [tool.mypy]
@@ -98,6 +110,7 @@ warn_redundant_casts = true
98
110
  # Force analyze of partially annotate function signatures
99
111
  check_untyped_defs = true
100
112
  ignore_missing_imports = true
113
+ disable_error_code = ["call-overload"]
101
114
 
102
115
  [tool.flake8]
103
116
  max-line-length = 120
@@ -107,7 +120,16 @@ max-line-length = 120
107
120
 
108
121
  [tool.pylint.messages_control]
109
122
  max-line-length=120
110
- disable = "logging-fstring-interpolation"
123
+ disable = [
124
+ "logging-fstring-interpolation",
125
+ "global-statement",
126
+ "too-many-arguments",
127
+ "too-many-positional-arguments",
128
+ "not-context-manager",
129
+ "too-many-locals",
130
+ "too-many-branches",
131
+ "too-many-statements",
132
+ ]
111
133
 
112
134
  [tool.pylint.MASTER]
113
135
  ignore-paths = '^docs/.*$'
@@ -15,4 +15,4 @@
15
15
  """rsclient main module."""
16
16
 
17
17
  # Set automatically by running `poetry dynamic-versioning`
18
- __version__ = "0.2a13"
18
+ __version__ = "0.2a15"
@@ -0,0 +1,15 @@
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
+ """ogc api modules."""
@@ -0,0 +1,217 @@
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
+ """Use "DPR as a service" implemented by rs-dpr-service"""
16
+
17
+ import ast
18
+ import os.path as osp
19
+ import tempfile
20
+
21
+ import yaml
22
+ from openapi_core import OpenAPI # Spec, validate_request, validate_response
23
+
24
+ from rs_client.ogcapi.ogcapi_client import OgcApiClient
25
+ from rs_common import prefect_utils
26
+ from rs_common.utils import get_href_service
27
+
28
+ PATH_TO_YAML_OPENAPI = osp.realpath(
29
+ osp.join(
30
+ osp.dirname(__file__),
31
+ "../../config",
32
+ "staging_templates",
33
+ "yaml",
34
+ "dpr_openapi_schema.yaml",
35
+ ),
36
+ )
37
+
38
+
39
+ class DprClient(OgcApiClient):
40
+ """Implement the OGC API client for 'DPR as a service'."""
41
+
42
+ ##########################################
43
+ # Override parent methods and attributes #
44
+ ##########################################
45
+
46
+ # Init the OpenAPI instance from config file
47
+ openapi = OpenAPI.from_file_path(PATH_TO_YAML_OPENAPI)
48
+
49
+ @property
50
+ def endpoint_prefix(self) -> str:
51
+ """Return the endpoints prefix, if any."""
52
+ return "dpr/"
53
+
54
+ @property
55
+ def href_service(self) -> str:
56
+ """
57
+ Return the rs-dpr-service URL hostname.
58
+ This URL can be overwritten using the RSPY_HOST_DPR_SERVICE env variable (used e.g. for local mode).
59
+ Otherwise it should just be the RS-Server URL.
60
+ """
61
+ return get_href_service(self.rs_server_href, "RSPY_HOST_DPR_SERVICE")
62
+
63
+ def run_process(
64
+ self,
65
+ process: str,
66
+ s3_config_dir: str,
67
+ payload_subpath: str,
68
+ s3_report_dir: str | None,
69
+ use_dpr_mockup: bool = False,
70
+ ) -> dict:
71
+ """Method to start the process from rs-client - Call the endpoint /processes/{process}/execution
72
+
73
+ Args:
74
+ process: Process name
75
+ s3_config_dir: S3 bucket folder that contains the payload and configuration files to pass to the processor
76
+ payload_subpath: Payload file path, relative to the config folder
77
+ s3_report_dir: S3 bucket folder were the processor report files will be written (optional). All the eopf
78
+ local files written in the local "./reports" directory will be pushed to this S3 bucket folder.
79
+ use_dpr_mockup: Use the real or the mockup DPR processor ?
80
+
81
+ Return:
82
+ job_id (int, str): Returns the status code of the request + the identifier
83
+ (or None if endpoint fails) of the running job
84
+ """
85
+
86
+ # Data to pass to the real processor
87
+ data = {}
88
+ if not use_dpr_mockup:
89
+ data = {
90
+ "s3_config_dir": s3_config_dir,
91
+ "payload_subpath": payload_subpath,
92
+ "s3_report_dir": s3_report_dir,
93
+ }
94
+
95
+ # For the mockup processor, pass the payload contents.
96
+ # Download the payload file into a temp file.
97
+ else:
98
+ with tempfile.NamedTemporaryFile() as temp:
99
+ prefect_utils.s3_download_file( # type: ignore
100
+ osp.join(s3_config_dir, payload_subpath),
101
+ temp.name,
102
+ _sync=True, # type: ignore
103
+ )
104
+
105
+ # Read it as a yaml file
106
+ with open(temp.name, encoding="utf-8") as opened:
107
+ data = yaml.safe_load(opened)
108
+
109
+ # Add extra info
110
+ data.update({"use_mockup": use_dpr_mockup})
111
+
112
+ # Call the parent method
113
+ return super()._run_process(process, data)
114
+
115
+ def wait_for_job(self, *args, **kwargs) -> list[dict]: # type: ignore
116
+ """
117
+ Wait for job to finish.
118
+
119
+ Returns:
120
+ EOPF results
121
+ """
122
+ # Call parent method and parse results
123
+ job_status = super().wait_for_job(*args, **kwargs)
124
+ return ast.literal_eval(job_status["message"])
125
+
126
+ ######################################################
127
+ # These endpoints are not implemented by the service #
128
+ ######################################################
129
+
130
+ def get_processes(self) -> dict:
131
+ raise NotImplementedError
132
+
133
+ def get_jobs(self) -> dict:
134
+ raise NotImplementedError
135
+
136
+ def delete_job(self, _: str) -> dict:
137
+ raise NotImplementedError
138
+
139
+ def get_job_results(self, _: str) -> dict:
140
+ raise NotImplementedError
141
+
142
+ ####################
143
+ # Specific methods #
144
+ ####################
145
+
146
+ async def update_configuration(self, local_path: str, s3_path: str, is_payload: bool = False, **kwargs):
147
+ """
148
+ Update local configuration file depending on the environment, upload it to the s3 bucket,
149
+ and initialize output bucket folders.
150
+
151
+ Args:
152
+ local_path: Local configuration file path
153
+ s3_path: S3 bucket path where to upload to modified updated configuration file
154
+ is_payload: Specific behavior for the processor payload files
155
+ kwargs: Specific environment variables to expand in the configuration file
156
+ """
157
+
158
+ to_expand = {}
159
+
160
+ if self.local_mode:
161
+ # In local mode, replace the S3_xxx_CLUSTER env vars, by the env vars from the ~/.s3cfg
162
+ # config file, that contains access to the "real" s3 bucket
163
+ to_expand = {
164
+ "S3_ACCESSKEY_CLUSTER": "${access_key}",
165
+ "S3_SECRETKEY_CLUSTER": "${secret_key}",
166
+ "S3_ENDPOINT_CLUSTER": "${host_bucket}",
167
+ "S3_REGION_CLUSTER": "${bucket_location}",
168
+ }
169
+
170
+ else:
171
+ # In cluster mode, just use the "real" s3 bucket
172
+ to_expand = {
173
+ "S3_ACCESSKEY_CLUSTER": "${S3_ACCESSKEY}",
174
+ "S3_SECRETKEY_CLUSTER": "${S3_SECRETKEY}",
175
+ "S3_ENDPOINT_CLUSTER": "${S3_ENDPOINT}",
176
+ "S3_REGION_CLUSTER": "${S3_REGION}",
177
+ }
178
+
179
+ # Also expand the user-given parameters
180
+ to_expand.update(kwargs)
181
+
182
+ # Open the input local file
183
+ with open(local_path, encoding="utf-8") as opened:
184
+ contents = opened.read()
185
+
186
+ # Expand the env vars as $key, ${key} or %key%
187
+ for key, value in to_expand.items():
188
+ for key2 in f"${key}", f"${{{key}}}", f"%{key}%":
189
+ contents = contents.replace(key2, str(value))
190
+
191
+ # Read the payload contents
192
+ if is_payload:
193
+ payload = yaml.safe_load(contents)
194
+
195
+ # We need to create the output S3 folder with a dummy file before running DPR
196
+ for output_product in payload["I/O"]["output_products"]:
197
+ s3_output_dir = output_product["path"]
198
+ s3_empty_file = f"{s3_output_dir}/.empty"
199
+ self.logger.info(f"Write empty file: {self.logger.level} {s3_empty_file!r}")
200
+ await prefect_utils.s3_upload_empty_file(s3_empty_file)
201
+
202
+ # Change the dask authentication for local mode
203
+ cluster_config = payload["dask_context"]["cluster_config"]
204
+ if self.local_mode:
205
+ cluster_config["auth"] = cluster_config["auth_local_mode"]
206
+ del cluster_config["auth_local_mode"]
207
+
208
+ # yaml to str conversion
209
+ contents = yaml.dump(payload, default_flow_style=False, sort_keys=False)
210
+
211
+ # Write the modified contents to a temp file
212
+ with tempfile.NamedTemporaryFile() as tmp:
213
+ tmp.write(contents.encode("utf-8"))
214
+ tmp.flush()
215
+
216
+ # Upload the temp file to the s3 bucket
217
+ return await prefect_utils.s3_upload_file(tmp.name, s3_path)
@@ -0,0 +1,283 @@
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
+ """Use OGC API services with rs-client-libraries"""
16
+
17
+ import json
18
+ import math
19
+ import time
20
+ from typing import Any
21
+
22
+ # openapi_core libraries used for endpoints validation
23
+ import requests
24
+ from openapi_core import OpenAPI # Spec, validate_request, validate_response
25
+ from openapi_core.contrib.requests import (
26
+ RequestsOpenAPIRequest,
27
+ RequestsOpenAPIResponse,
28
+ )
29
+ from requests import Response
30
+ from requests.models import PreparedRequest
31
+
32
+ from rs_client.rs_client import TIMEOUT, RsClient
33
+
34
+
35
+ class OgcValidationException(Exception):
36
+ """
37
+ Exception raised when an error occurs during the OGC validation of the endpoints
38
+ """
39
+
40
+
41
+ class OgcApiClient(RsClient):
42
+ """
43
+ Class to handle OGC API processes in rs-client-libraries
44
+
45
+ This class provides python methods to call the different endpoints of the OGC API service.
46
+
47
+ Remark: this class don't inherits from the owslib.ogcapi.processes.Processes class because the latter
48
+ doesn't provide wrapping for all endpoints defined in our services (it only provides the /processes
49
+ and /processes/{processId}/execution endpoints + it doesn't allow to manage apikey_header parameter which
50
+ is passed as an extra argument).
51
+ """
52
+
53
+ @property
54
+ def endpoint_prefix(self) -> str:
55
+ """Return the endpoints prefix, if any."""
56
+ return "" # no prefix by default
57
+
58
+ @classmethod
59
+ def get_openapi(cls) -> OpenAPI:
60
+ """Return the OpenAPI instance of the subclass"""
61
+ return cls.openapi # type: ignore # pylint: disable=no-member
62
+
63
+ def validate_and_unmarshal_request(self, request: PreparedRequest) -> Any:
64
+ """Validate an endpoint request according to the ogc specifications
65
+
66
+ Args:
67
+ request (Request): endpoint request
68
+
69
+ Returns:
70
+ ResponseUnmarshalResult.data: data validated by the openapi_core
71
+ unmarshal_response method
72
+ """
73
+ openapi_request = RequestsOpenAPIRequest(request)
74
+
75
+ # validate_request(request, spec=Spec.from_file_path(PATH_TO_YAML_OPENAPI))
76
+ result = self.get_openapi().unmarshal_request(openapi_request)
77
+
78
+ if result.errors:
79
+ raise OgcValidationException(
80
+ f"Error validating the request of the enpoint "
81
+ f"{openapi_request.path}: {str(result.errors[0])}", # type: ignore
82
+ )
83
+ if not result.body:
84
+ raise OgcValidationException(
85
+ f"Error validating the request of the enpoint "
86
+ f"{openapi_request.path}: 'data' field of ResponseUnmarshalResult"
87
+ f"object is empty",
88
+ )
89
+ return result.body
90
+
91
+ def validate_and_unmarshal_response(self, response: Response) -> Any:
92
+ """
93
+ Validate an endpoint response according to the ogc specifications
94
+ (described as yaml schemas)
95
+
96
+ Args:
97
+ response (Response): endpoint response
98
+ Returns:
99
+ ResponseUnmarshalResult.data: data validated by the openapi_core
100
+ unmarshal_response method
101
+ """
102
+ openapi_request = RequestsOpenAPIRequest(response.request)
103
+ openapi_response = RequestsOpenAPIResponse(response)
104
+
105
+ # Alternative method to validate the response
106
+ # validate_response(response=response, spec= Spec.from_file_path(PATH_TO_YAML_OPENAPI), request=request)
107
+ result = self.get_openapi().unmarshal_response(openapi_request, openapi_response) # type: ignore
108
+ if result.errors:
109
+ raise OgcValidationException( # type: ignore
110
+ f"Error validating the response of the enpoint {openapi_request.path} - "
111
+ f"Server response content: {response.json()} - "
112
+ f"Validation error of the server response: {str(result.errors[0])}", # type: ignore
113
+ )
114
+ if not result.data:
115
+ raise OgcValidationException(
116
+ f"Error validating the response of the enpoint "
117
+ f"{openapi_request.path}: 'data' field of ResponseUnmarshalResult"
118
+ f"object is empty",
119
+ )
120
+ return json.loads(response.content)
121
+
122
+ ##################################
123
+ # Call OGC API service endpoints #
124
+ ##################################
125
+
126
+ def get_processes(self) -> dict:
127
+ """_summary_
128
+
129
+ Returns:
130
+ dict: dictionary containing the content of the response
131
+ """
132
+ response = self.http_session.get(
133
+ url=f"{self.href_service}/{self.endpoint_prefix}processes",
134
+ timeout=TIMEOUT,
135
+ **self.apikey_headers,
136
+ )
137
+ return self.validate_and_unmarshal_response(response)
138
+
139
+ def get_process(self, process_id: str) -> dict:
140
+ """
141
+ Wrapper to get a specific process
142
+ Args:
143
+ process_id (str): name of the resource
144
+ """
145
+ response = self.http_session.get(
146
+ url=f"{self.href_service}/{self.endpoint_prefix}processes/{process_id}",
147
+ timeout=TIMEOUT,
148
+ **self.apikey_headers,
149
+ )
150
+ return self.validate_and_unmarshal_response(response)
151
+
152
+ def _run_process(self, process: str, body: dict) -> dict:
153
+ """Method to start the process from rs-client - Call the endpoint /processes/{process}/execution
154
+
155
+ Args:
156
+ process: Process name
157
+ body: POST HTTP request body as a JSON dict
158
+
159
+ Return:
160
+ job_id (int, str): Returns the status code of the request + the identifier
161
+ (or None if endpoint fails) of the running job
162
+ """
163
+
164
+ # Check that the request containing the body is valid
165
+ request = requests.Request(
166
+ method="POST",
167
+ url=f"{self.href_service}/{self.endpoint_prefix}processes/{process}/execution",
168
+ json=body,
169
+ ).prepare()
170
+
171
+ # Validate the body of the request that will be sent to the service
172
+ self.validate_and_unmarshal_request(request)
173
+
174
+ response = self.http_session.post(
175
+ url=f"{self.href_service}/{self.endpoint_prefix}processes/{process}/execution",
176
+ json=body,
177
+ **self.apikey_headers,
178
+ timeout=TIMEOUT,
179
+ )
180
+ return self.validate_and_unmarshal_response(response)
181
+
182
+ def get_jobs(self) -> dict:
183
+ """Method to get running jobs"""
184
+ response = self.http_session.get(
185
+ url=f"{self.href_service}/{self.endpoint_prefix}jobs",
186
+ **self.apikey_headers,
187
+ timeout=TIMEOUT,
188
+ )
189
+ return self.validate_and_unmarshal_response(response)
190
+
191
+ def get_job_info(self, job_id: str) -> dict: # pylint: disable=too-many-locals
192
+ """Method to get a specific job response"""
193
+ response = self.http_session.get(
194
+ url=f"{self.href_service}/{self.endpoint_prefix}jobs/{job_id}",
195
+ **self.apikey_headers,
196
+ timeout=TIMEOUT,
197
+ )
198
+ return self.validate_and_unmarshal_response(response)
199
+
200
+ def delete_job(self, job_id: str) -> dict: # pylint: disable=too-many-locals
201
+ """Method to get a specific job response"""
202
+ response = self.http_session.delete(
203
+ url=f"{self.href_service}/{self.endpoint_prefix}jobs/{job_id}",
204
+ **self.apikey_headers,
205
+ timeout=TIMEOUT,
206
+ )
207
+ return self.validate_and_unmarshal_response(response)
208
+
209
+ def get_job_results(self, job_id: str) -> dict:
210
+ """Wrapper to get the result of a specfific job
211
+
212
+ Args:
213
+ job_id (str): _description_
214
+ """
215
+ response = self.http_session.get(
216
+ url=f"{self.href_service}/{self.endpoint_prefix}jobs/{job_id}/results",
217
+ timeout=TIMEOUT,
218
+ **self.apikey_headers,
219
+ )
220
+ return self.validate_and_unmarshal_response(response)
221
+
222
+ def wait_for_job(
223
+ self,
224
+ job_status: dict,
225
+ logger=None,
226
+ job_name: str = "",
227
+ timeout: int | float = math.inf,
228
+ poll_interval: int = 2,
229
+ ) -> dict:
230
+ """
231
+ Wait for job to finish.
232
+
233
+ Args:
234
+ job_status: Returned by `_run_process`
235
+ logger: To show advancement in logger
236
+ job_name: Job name to show in the logger
237
+ timeout: Job completion timeout in seconds
238
+ poll_interval: When to check again for job completion in seconds
239
+
240
+ Returns:
241
+ Job status
242
+
243
+ Raises:
244
+ RuntimeError in case of error
245
+ """
246
+ try:
247
+ status_type = ""
248
+ job_identifier = job_status.get("jobID")
249
+ if not job_identifier:
250
+ raise RuntimeError("Job identifier is missing.")
251
+
252
+ while True:
253
+ job_status = self.get_job_info(job_identifier)
254
+ if logger:
255
+ logger.info(f"job_status: {job_status}")
256
+ status_type = job_status.get("status", "")
257
+ if logger:
258
+ logger.info(
259
+ f"----- {job_name} job {job_identifier!r}: {status_type.upper()} \n",
260
+ )
261
+
262
+ # Exit the loop
263
+ if status_type in {"successful", "failed", "dismissed"}:
264
+ break
265
+
266
+ # Or sleep n seconds and try again
267
+ timeout -= poll_interval
268
+ if timeout <= 0:
269
+ raise TimeoutError(f"Timed out while waiting for {job_name} job {job_identifier!r}")
270
+ time.sleep(poll_interval)
271
+
272
+ # Log all exceptions except the timeout
273
+ except TimeoutError:
274
+ raise
275
+ except Exception as e:
276
+ raise RuntimeError(f"Exception while monitoring job {job_name}: {job_status}") from e
277
+
278
+ if status_type == "successful":
279
+ if logger:
280
+ logger.info(f"----- {job_name} job {job_identifier!r}: COMPLETED \n")
281
+ return job_status
282
+
283
+ raise RuntimeError(f"{job_name} job {job_identifier!r}: FAILED")