|
38 | 38 | import pydantic |
39 | 39 | from httpx import URL |
40 | 40 | from pydantic import PrivateAttr |
| 41 | +from detect_agent import determine_agent |
41 | 42 |
|
42 | 43 | from . import _exceptions |
43 | 44 | from ._qs import Querystring |
@@ -675,6 +676,7 @@ def default_headers(self) -> dict[str, str | Omit]: |
675 | 676 | "Content-Type": "application/json", |
676 | 677 | "User-Agent": self.user_agent, |
677 | 678 | **self.platform_headers(), |
| 679 | + **self.agent_headers(), |
678 | 680 | **self.auth_headers, |
679 | 681 | **self._custom_headers, |
680 | 682 | } |
@@ -714,6 +716,12 @@ def platform_headers(self) -> Dict[str, str]: |
714 | 716 | # https://github.com/python/cpython/issues/88476 |
715 | 717 | return platform_headers(self._version, platform=self._platform) |
716 | 718 |
|
| 719 | + def agent_headers(self) -> dict[str, str]: |
| 720 | + # the actual implementation is in a separate `lru_cache` decorated |
| 721 | + # function because adding `lru_cache` to methods will leak memory |
| 722 | + # https://github.com/python/cpython/issues/88476 |
| 723 | + return agent_headers() |
| 724 | + |
717 | 725 | def _parse_retry_after_header(self, response_headers: Optional[httpx.Headers] = None) -> float | None: |
718 | 726 | """Returns a float of the number of seconds (not milliseconds) to wait after retrying, or None if unspecified. |
719 | 727 |
|
@@ -2055,6 +2063,15 @@ def get_platform() -> Platform: |
2055 | 2063 | return "Unknown" |
2056 | 2064 |
|
2057 | 2065 |
|
| 2066 | +@lru_cache(maxsize=None) |
| 2067 | +def agent_headers() -> dict[str, str]: |
| 2068 | + agent = determine_agent() |
| 2069 | + if agent["is_agent"] and agent["agent"]: |
| 2070 | + return {"X-Stainless-Agent": agent["agent"]["name"]} |
| 2071 | + |
| 2072 | + return {} |
| 2073 | + |
| 2074 | + |
2058 | 2075 | @lru_cache(maxsize=None) |
2059 | 2076 | def platform_headers(version: str, *, platform: Platform | None) -> Dict[str, str]: |
2060 | 2077 | return { |
|
0 commit comments