Skip to content

Commit

Permalink
Add stripe context
Browse files Browse the repository at this point in the history
  • Loading branch information
richardm-stripe committed May 10, 2023
1 parent b0c35ac commit 3f80e45
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 9 additions & 0 deletions stripe/raw_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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

Expand Down
4 changes: 3 additions & 1 deletion tests/test_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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

0 comments on commit 3f80e45

Please sign in to comment.