|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import json |
5 | 6 | import logging |
6 | 7 | import os |
7 | 8 | from pathlib import Path |
@@ -338,6 +339,7 @@ class Config(BaseSettings): |
338 | 339 | trino_username: str | None = None |
339 | 340 | trino_password: SecretStr | None = None |
340 | 341 | trino_jwt_token: SecretStr | None = None |
| 342 | + trino_http_headers: SecretStr | None = None |
341 | 343 | # overrides for the contract's servers block |
342 | 344 | trino_host: str | None = None |
343 | 345 | trino_port: int | None = None |
@@ -506,6 +508,29 @@ def _bool_option(self, field_name: str, default: bool) -> bool: |
506 | 508 | # A set-but-empty variable counts as false, matching pre-Config parsing. |
507 | 509 | return str(value).strip().lower() in _TRUTHY |
508 | 510 |
|
| 511 | + def _dict_option(self, field_name: str, required: bool = False) -> dict[str, str] | None: |
| 512 | + """Parse JSON-object or comma-separated ``key=value`` connection properties.""" |
| 513 | + text = self._str_option(field_name, required) |
| 514 | + if not text: |
| 515 | + return None |
| 516 | + if text.startswith("{"): |
| 517 | + try: |
| 518 | + parsed = json.loads(text) |
| 519 | + except json.JSONDecodeError as e: |
| 520 | + key = env_name(field_name, type(self).model_fields[field_name]) |
| 521 | + server_type = field_name.split("_")[0] |
| 522 | + from datacontract.model.exceptions import DataContractException |
| 523 | + |
| 524 | + raise DataContractException( |
| 525 | + type=f"{server_type}-connection", |
| 526 | + name=f"invalid_mapping_{key}", |
| 527 | + reason=f"{key} is not valid JSON: {e}", |
| 528 | + engine="datacontract-cli", |
| 529 | + ) |
| 530 | + return {str(k): str(v) for k, v in parsed.items()} |
| 531 | + pairs = (pair.partition("=") for pair in text.split(",") if pair.strip()) |
| 532 | + return {key.strip(): value.strip() for key, _, value in pairs if key.strip()} |
| 533 | + |
509 | 534 | # --- entropy --- |
510 | 535 | def get_entropy_data_api_key(self, required: bool = False) -> str | None: |
511 | 536 | return self._str_option("entropy_data_api_key", required) |
@@ -706,8 +731,8 @@ def get_iceberg_s3_endpoint(self, required: bool = False) -> str | None: |
706 | 731 | def get_iceberg_signing_name(self, required: bool = False) -> str | None: |
707 | 732 | return self._str_option("iceberg_signing_name", required) |
708 | 733 |
|
709 | | - def get_iceberg_properties(self, required: bool = False) -> str | None: |
710 | | - return self._str_option("iceberg_properties", required) |
| 734 | + def get_iceberg_properties(self, required: bool = False) -> dict[str, str] | None: |
| 735 | + return self._dict_option("iceberg_properties", required) |
711 | 736 |
|
712 | 737 | def get_iceberg_credential(self, required: bool = False) -> str | None: |
713 | 738 | return self._str_option("iceberg_credential", required) |
@@ -1056,6 +1081,9 @@ def get_trino_password(self, required: bool = False) -> str | None: |
1056 | 1081 | def get_trino_jwt_token(self, required: bool = False) -> str | None: |
1057 | 1082 | return self._str_option("trino_jwt_token", required) |
1058 | 1083 |
|
| 1084 | + def get_trino_http_headers(self, required: bool = False) -> dict[str, str] | None: |
| 1085 | + return self._dict_option("trino_http_headers", required) |
| 1086 | + |
1059 | 1087 | def get_trino_host(self, required: bool = False) -> str | None: |
1060 | 1088 | return self._str_option("trino_host", required) |
1061 | 1089 |
|
|
0 commit comments