Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions stripe/_api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import (
Any,
AsyncIterable,
Callable,
Dict,
List,
Mapping,
Expand Down Expand Up @@ -509,14 +510,14 @@ def request_headers(
ua: Dict[str, Union[str, "AppInfo"]] = {
"bindings_version": VERSION,
"lang": "python",
"publisher": "stripe",
"httplib": self._get_http_client().name,
}
for attr, func in [
["lang_version", platform.python_version],
["platform", platform.platform],
["uname", lambda: " ".join(platform.uname())],
]:
attr_funcs: List[Tuple[str, Callable[[], str]]] = [
("lang_version", platform.python_version),
]
if stripe.enable_telemetry:
attr_funcs.append(("platform", platform.platform))
for attr, func in attr_funcs:
try:
val = func()
except Exception:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ def fail():

mocker.patch("platform.platform", side_effect=fail)

stripe.enable_telemetry = True
requestor.request("get", self.v1_path, {}, {}, base_address="api")

last_call = http_client_mock.get_last_call()
Expand All @@ -767,6 +768,26 @@ def fail():
== "(disabled)"
)

def test_platform_only_used_with_telemetry(
self, requestor, mocker, http_client_mock
):
http_client_mock.stub_request(
"get", path=self.v1_path, rbody="{}", rcode=200
)

def fail():
raise RuntimeError

mocker.patch("platform.platform", side_effect=fail)

requestor.request("get", self.v1_path, {}, {}, base_address="api")

last_call = http_client_mock.get_last_call()
last_call.assert_method("get")
assert "platform" not in json.loads(
last_call.get_raw_header("X-Stripe-Client-User-Agent")
)

def test_uses_given_idempotency_key(self, requestor, http_client_mock):
method = "post"
http_client_mock.stub_request(
Expand Down