|
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 |
@@ -334,6 +335,7 @@ class Config(BaseSettings): |
334 | 335 | trino_username: str | None = None |
335 | 336 | trino_password: SecretStr | None = None |
336 | 337 | trino_jwt_token: SecretStr | None = None |
| 338 | + trino_http_headers: SecretStr | None = None |
337 | 339 | # overrides for the contract's servers block |
338 | 340 | trino_host: str | None = None |
339 | 341 | trino_port: int | None = None |
@@ -502,6 +504,29 @@ def _bool_option(self, field_name: str, default: bool) -> bool: |
502 | 504 | # A set-but-empty variable counts as false, matching pre-Config parsing. |
503 | 505 | return str(value).strip().lower() in _TRUTHY |
504 | 506 |
|
| 507 | + def _dict_option(self, field_name: str, required: bool = False) -> dict[str, str] | None: |
| 508 | + """Parse JSON-object or comma-separated ``key=value`` connection properties.""" |
| 509 | + text = self._str_option(field_name, required) |
| 510 | + if not text: |
| 511 | + return None |
| 512 | + if text.startswith("{"): |
| 513 | + try: |
| 514 | + parsed = json.loads(text) |
| 515 | + except json.JSONDecodeError as e: |
| 516 | + key = env_name(field_name, type(self).model_fields[field_name]) |
| 517 | + server_type = field_name.split("_")[0] |
| 518 | + from datacontract.model.exceptions import DataContractException |
| 519 | + |
| 520 | + raise DataContractException( |
| 521 | + type=f"{server_type}-connection", |
| 522 | + name=f"invalid_mapping_{key}", |
| 523 | + reason=f"{key} is not valid JSON: {e}", |
| 524 | + engine="datacontract-cli", |
| 525 | + ) |
| 526 | + return {str(k): str(v) for k, v in parsed.items()} |
| 527 | + pairs = (pair.partition("=") for pair in text.split(",") if pair.strip()) |
| 528 | + return {key.strip(): value.strip() for key, _, value in pairs if key.strip()} |
| 529 | + |
505 | 530 | # --- entropy --- |
506 | 531 | def get_entropy_data_api_key(self, required: bool = False) -> str | None: |
507 | 532 | return self._str_option("entropy_data_api_key", required) |
@@ -696,8 +721,8 @@ def get_iceberg_s3_endpoint(self, required: bool = False) -> str | None: |
696 | 721 | def get_iceberg_signing_name(self, required: bool = False) -> str | None: |
697 | 722 | return self._str_option("iceberg_signing_name", required) |
698 | 723 |
|
699 | | - def get_iceberg_properties(self, required: bool = False) -> str | None: |
700 | | - return self._str_option("iceberg_properties", required) |
| 724 | + def get_iceberg_properties(self, required: bool = False) -> dict[str, str] | None: |
| 725 | + return self._dict_option("iceberg_properties", required) |
701 | 726 |
|
702 | 727 | def get_iceberg_credential(self, required: bool = False) -> str | None: |
703 | 728 | return self._str_option("iceberg_credential", required) |
@@ -1040,6 +1065,9 @@ def get_trino_password(self, required: bool = False) -> str | None: |
1040 | 1065 | def get_trino_jwt_token(self, required: bool = False) -> str | None: |
1041 | 1066 | return self._str_option("trino_jwt_token", required) |
1042 | 1067 |
|
| 1068 | + def get_trino_http_headers(self, required: bool = False) -> dict[str, str] | None: |
| 1069 | + return self._dict_option("trino_http_headers", required) |
| 1070 | + |
1043 | 1071 | def get_trino_host(self, required: bool = False) -> str | None: |
1044 | 1072 | return self._str_option("trino_host", required) |
1045 | 1073 |
|
|
0 commit comments