|
6 | 6 | import sys |
7 | 7 | from typing import Any, Dict, List, Mapping, Optional, Sequence, TypeVar, Union |
8 | 8 |
|
9 | | -from pydantic.v1 import parse_obj_as, parse_raw_as |
| 9 | +from pydantic.v1 import BaseModel, parse_obj_as, parse_raw_as |
10 | 10 |
|
11 | 11 | from pyatlan.client.aio import AsyncAtlanClient |
12 | 12 | from pyatlan.client.atlan import AtlanClient |
|
17 | 17 | # Type variable for client types |
18 | 18 | ClientType = TypeVar("ClientType", AtlanClient, AsyncAtlanClient) |
19 | 19 |
|
| 20 | + |
| 21 | +class PackageHeaders(BaseModel): |
| 22 | + """Typed container for Atlan package HTTP headers.""" |
| 23 | + |
| 24 | + agent: str = "workflow" |
| 25 | + workflow_id: Optional[str] = None |
| 26 | + app_name: Optional[str] = None |
| 27 | + run_id: Optional[str] = None |
| 28 | + |
| 29 | + class Config: |
| 30 | + allow_mutation = False |
| 31 | + |
| 32 | + def to_headers(self) -> Dict[str, str]: |
| 33 | + return { |
| 34 | + "x-atlan-agent": self.agent, |
| 35 | + "x-atlan-agent-id": self.workflow_id or "", |
| 36 | + "x-atlan-agent-package-name": self.app_name or "", |
| 37 | + "x-atlan-agent-app-name": self.app_name or "", |
| 38 | + "x-atlan-agent-workflow-id": self.run_id or "", |
| 39 | + } |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def from_env(cls) -> Optional["PackageHeaders"]: |
| 43 | + workflow_id = os.environ.get("X_ATLAN_AGENT_ID") |
| 44 | + if not workflow_id: |
| 45 | + return None |
| 46 | + return cls( |
| 47 | + workflow_id=workflow_id, |
| 48 | + app_name=os.environ.get("X_ATLAN_AGENT_PACKAGE_NAME", ""), |
| 49 | + run_id=os.environ.get("X_ATLAN_AGENT_WORKFLOW_ID", ""), |
| 50 | + ) |
| 51 | + |
| 52 | + |
20 | 53 | # Try to import OpenTelemetry libraries |
21 | 54 | try: |
22 | 55 | from opentelemetry.exporter.otlp.proto.grpc._log_exporter import ( # type:ignore |
@@ -186,33 +219,33 @@ def set_package_ops(run_time_config: RuntimeConfig) -> AtlanClient: |
186 | 219 | :returns: an intialized AtlanClient that should be used for any calls to the SDK |
187 | 220 | """ |
188 | 221 | client = get_client(run_time_config.user_id or "") |
189 | | - if run_time_config.agent == "workflow": |
190 | | - client = set_package_headers(client) |
| 222 | + if run_time_config.agent: |
| 223 | + client = set_package_headers( |
| 224 | + client, |
| 225 | + headers=PackageHeaders( |
| 226 | + agent=run_time_config.agent, |
| 227 | + workflow_id=run_time_config.agent_id, |
| 228 | + app_name=run_time_config.agent_pkg, |
| 229 | + run_id=run_time_config.agent_wfl, |
| 230 | + ), |
| 231 | + ) |
191 | 232 | return client |
192 | 233 |
|
193 | 234 |
|
194 | | -def set_package_headers(client: ClientType) -> ClientType: |
| 235 | +def set_package_headers( |
| 236 | + client: ClientType, |
| 237 | + headers: Optional[PackageHeaders] = None, |
| 238 | +) -> ClientType: |
195 | 239 | """ |
196 | | - Configure the AtlanClient or AsyncAtlanClient with package headers from environment variables. |
| 240 | + Configure the AtlanClient or AsyncAtlanClient with package headers. |
197 | 241 |
|
198 | 242 | :param client: AtlanClient or AsyncAtlanClient instance to configure |
| 243 | + :param headers: PackageHeaders instance; if None, reads from environment variables |
199 | 244 | :returns: updated client instance of the same type. |
200 | 245 | """ |
201 | | - |
202 | | - if (agent := os.environ.get("X_ATLAN_AGENT")) and ( |
203 | | - agent_id := os.environ.get("X_ATLAN_AGENT_ID") |
204 | | - ): |
205 | | - headers: Dict[str, str] = { |
206 | | - "x-atlan-agent": agent, |
207 | | - "x-atlan-agent-id": agent_id, |
208 | | - "x-atlan-agent-package-name": os.environ.get( |
209 | | - "X_ATLAN_AGENT_PACKAGE_NAME", "" |
210 | | - ), |
211 | | - "x-atlan-agent-workflow-id": os.environ.get( |
212 | | - "X_ATLAN_AGENT_WORKFLOW_ID", "" |
213 | | - ), |
214 | | - } |
215 | | - client.update_headers(headers) |
| 246 | + pkg_headers = headers or PackageHeaders.from_env() |
| 247 | + if pkg_headers and pkg_headers.workflow_id: |
| 248 | + client.update_headers(pkg_headers.to_headers()) |
216 | 249 | return client |
217 | 250 |
|
218 | 251 |
|
|
0 commit comments