33
44import logging
55import os
6- from typing import Optional , Dict , Union , IO , Type , Tuple , Any
6+ from typing import Optional , Dict , Union , IO , Type , Tuple , Any , Callable
77
88import httpx
99import pydantic
@@ -29,16 +29,17 @@ def __init__(
2929 auth : Auth ,
3030 timeout : int = consts .DEFAULT_TIMEOUT ,
3131 upload_timeout : int = consts .DEFAULT_UPLOAD_TIMEOUT ,
32+ header_injector : Optional [Callable [[], Dict [str , str ]]] = None ,
3233 ):
3334 self .api_base_url = api_base_url
3435 self .http_client = http_client
3536 self .auth = auth
3637 self .timeout = timeout
3738 self .upload_timeout = upload_timeout
39+ self .header_injector = header_injector
3840
3941 def _build_url (self , path : str ) -> str :
4042 return f"{ self .api_base_url } { path } "
41-
4243 def _set_headers (self , headers : Optional [Dict [str , str ]] = None ) -> Dict [str , str ]:
4344 res = user_agent_header ()
4445 if headers :
@@ -52,6 +53,14 @@ def _set_headers(self, headers: Optional[Dict[str, str]] = None) -> Dict[str, st
5253 if ppe_env :
5354 res ["x-use-ppe" ] = "1"
5455
56+ if self .header_injector :
57+ try :
58+ injected_headers = self .header_injector ()
59+ if injected_headers :
60+ res .update (injected_headers )
61+ except Exception as e :
62+ logger .debug (f"Header injection failed: { e } " )
63+
5564 return res
5665
5766 def request (
0 commit comments