|
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 |
8 | 9 |
|
9 | | -from pydantic import Field, SecretStr |
| 10 | +from pydantic import Field, SecretStr, TypeAdapter, field_validator |
10 | 11 | from pydantic_settings import BaseSettings, SettingsConfigDict |
11 | 12 |
|
12 | 13 | logger = logging.getLogger(__name__) |
@@ -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: dict[str, str] | 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 |
@@ -402,7 +404,9 @@ def from_yaml(cls, path: "str | Path") -> "Config": |
402 | 404 | def walk(prefix: str, node: dict): |
403 | 405 | for key, value in node.items(): |
404 | 406 | name = f"{prefix}_{key}" if prefix else str(key) |
405 | | - if isinstance(value, dict): |
| 407 | + if name in cls.model_fields and isinstance(value, dict): |
| 408 | + flat[name] = {k: _interpolate_env(v, path) for k, v in value.items()} |
| 409 | + elif isinstance(value, dict): |
406 | 410 | walk(name, value) |
407 | 411 | else: |
408 | 412 | flat[name] = _interpolate_env(value, path) |
@@ -1040,6 +1044,14 @@ def get_trino_password(self, required: bool = False) -> str | None: |
1040 | 1044 | def get_trino_jwt_token(self, required: bool = False) -> str | None: |
1041 | 1045 | return self._str_option("trino_jwt_token", required) |
1042 | 1046 |
|
| 1047 | + @field_validator("trino_http_headers", mode="before") |
| 1048 | + @classmethod |
| 1049 | + def _parse_trino_http_headers(cls, value): |
| 1050 | + return TypeAdapter(dict[str, str]).validate_json(value) if isinstance(value, str) else value |
| 1051 | + |
| 1052 | + def get_trino_http_headers(self) -> dict[str, str] | None: |
| 1053 | + return self._parse_trino_http_headers(self._raw_option("trino_http_headers")) |
| 1054 | + |
1043 | 1055 | def get_trino_host(self, required: bool = False) -> str | None: |
1044 | 1056 | return self._str_option("trino_host", required) |
1045 | 1057 |
|
@@ -1090,6 +1102,8 @@ def env_name(field_name: str, field) -> str: |
1090 | 1102 | def _as_env_value(value) -> str: |
1091 | 1103 | if isinstance(value, SecretStr): |
1092 | 1104 | return value.get_secret_value() |
| 1105 | + if isinstance(value, dict): |
| 1106 | + return json.dumps(value) |
1093 | 1107 | if isinstance(value, bool): |
1094 | 1108 | return "true" if value else "false" |
1095 | 1109 | return str(value) |
|
0 commit comments