diff --git a/stripe/raw_request.py b/stripe/raw_request.py index a1175eb40..3c69e2149 100644 --- a/stripe/raw_request.py +++ b/stripe/raw_request.py @@ -11,6 +11,7 @@ def _raw_request(method_, url_, **params): stripe_version = util.read_special_variable(params, "stripe_version", None) stripe_account = util.read_special_variable(params, "stripe_account", None) api_mode = util.read_special_variable(params, "api_mode", None) + stripe_context = util.read_special_variable(params, "stripe_context", None) headers = util.read_special_variable(params, "headers", None) requestor = api_requestor.APIRequestor( @@ -24,6 +25,14 @@ def _raw_request(method_, url_, **params): headers = {} if headers is None else headers.copy() headers.update(util.populate_headers(idempotency_key)) + # stripe-context goes *here* and not in api_requestor. Properties + # go on api_requestor when you want them to persist onto requests + # made when you call instance methods on APIResources that come from + # the first request. No need for that here, as we aren't deserializing APIResources + if stripe_context is not None: + headers = {} if headers is None else headers.copy() + headers.update({"Stripe-Context": stripe_context}) + response, _ = requestor.request(method_, url_, params, headers, api_mode) return response diff --git a/tests/test_preview.py b/tests/test_preview.py index db2ccd377..0901eac19 100644 --- a/tests/test_preview.py +++ b/tests/test_preview.py @@ -70,10 +70,11 @@ def test_delete(self, request_mock): def test_override_default_options(self, request_mock): expected_body = '{"id": "acc_123"}' stripe_version_override = "2022-11-15" + stripe_context = "acct_x" self.set_body(expected_body) resp = stripe.preview.post( - "/v2/accounts", stripe_version=stripe_version_override + "/v2/accounts", stripe_version=stripe_version_override, stripe_context=stripe_context ) req = self.mock_request.mock_calls[0] @@ -82,4 +83,5 @@ def test_override_default_options(self, request_mock): assert method == "post" assert headers["Content-Type"] == "application/json" assert headers["Stripe-Version"] == stripe_version_override + assert headers["Stripe-Context"] == stripe_context assert resp.body == expected_body