Skip to content

Commit 0b7a467

Browse files
wscourgeclaude
andcommitted
Add flat-param interface and disable/enable for SubscriptionEvent
Add SubscriptionEvent.destroy() and .modify() that accept flat params (e.g. data={"id": 123}) and auto-wrap in the subscription_event envelope. The old _with_params methods are preserved for backwards compatibility. Add SubscriptionEvent.disable() and .enable() convenience methods for toggling the disabled state of subscription events. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 383f43a commit 0b7a467

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

chartmogul/api/subscription_event.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,47 @@ def make(self, data, **kwargs):
4646
SubscriptionEvent.modify_with_params = SubscriptionEvent._method(
4747
"modify_with_params", "patch", "/subscription_events"
4848
)
49+
50+
51+
@classmethod
52+
def _destroy(cls, config, **kwargs):
53+
"""Accept flat params and wrap in subscription_event envelope for the API."""
54+
data = kwargs.get("data", {})
55+
if "subscription_event" not in data:
56+
data = {"subscription_event": data}
57+
return cls.destroy_with_params(config, data=data)
58+
59+
60+
@classmethod
61+
def _modify(cls, config, **kwargs):
62+
"""Accept flat params and wrap in subscription_event envelope for the API."""
63+
data = kwargs.get("data", {})
64+
if "subscription_event" not in data:
65+
data = {"subscription_event": data}
66+
return cls.modify_with_params(config, data=data)
67+
68+
69+
@classmethod
70+
def _disable(cls, config, **kwargs):
71+
"""Disable a subscription event by setting disabled to true."""
72+
data = kwargs.get("data", {})
73+
data["disabled"] = True
74+
if "subscription_event" not in data:
75+
data = {"subscription_event": data}
76+
return cls.modify_with_params(config, data=data)
77+
78+
79+
@classmethod
80+
def _enable(cls, config, **kwargs):
81+
"""Enable a subscription event by setting disabled to false."""
82+
data = kwargs.get("data", {})
83+
data["disabled"] = False
84+
if "subscription_event" not in data:
85+
data = {"subscription_event": data}
86+
return cls.modify_with_params(config, data=data)
87+
88+
89+
SubscriptionEvent.destroy = _destroy
90+
SubscriptionEvent.modify = _modify
91+
SubscriptionEvent.disable = _disable
92+
SubscriptionEvent.enable = _enable

test/api/test_subscription_event.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,85 @@ def test_all_subscription_events(self, mock_requests):
224224
)
225225
self.assertTrue(isinstance(subscription_events.subscription_events[0], SubscriptionEvent))
226226

227+
@requests_mock.mock()
228+
def test_destroy_flat_params(self, mock_requests):
229+
mock_requests.register_uri(
230+
"DELETE",
231+
"https://api.chartmogul.com/v1/subscription_events",
232+
request_headers={"Authorization": "Basic dG9rZW46"},
233+
status_code=204,
234+
)
235+
236+
config = Config("token")
237+
result = SubscriptionEvent.destroy(config, data={"id": 7654321}).get()
238+
239+
self.assertEqual(mock_requests.call_count, 1, "expected call")
240+
self.assertEqual(
241+
mock_requests.last_request.json(),
242+
{"subscription_event": {"id": 7654321}},
243+
)
244+
self.assertTrue(result is None)
245+
246+
@requests_mock.mock()
247+
def test_modify_flat_params(self, mock_requests):
248+
mock_requests.register_uri(
249+
"PATCH",
250+
"https://api.chartmogul.com/v1/subscription_events",
251+
request_headers={"Authorization": "Basic dG9rZW46"},
252+
status_code=200,
253+
json=expected_sub_ev,
254+
)
255+
256+
config = Config("token")
257+
sub_ev = SubscriptionEvent.modify(
258+
config, data={"id": 7654321, "amount_in_cents": 10}
259+
).get()
260+
261+
self.assertEqual(mock_requests.call_count, 1, "expected call")
262+
self.assertEqual(
263+
mock_requests.last_request.json(),
264+
{"subscription_event": {"id": 7654321, "amount_in_cents": 10}},
265+
)
266+
self.assertTrue(isinstance(sub_ev, SubscriptionEvent))
267+
self.assertEqual(sub_ev.id, 7654321)
268+
269+
@requests_mock.mock()
270+
def test_disable_subscription_event(self, mock_requests):
271+
mock_requests.register_uri(
272+
"PATCH",
273+
"https://api.chartmogul.com/v1/subscription_events",
274+
request_headers={"Authorization": "Basic dG9rZW46"},
275+
status_code=200,
276+
json=expected_sub_ev,
277+
)
278+
279+
config = Config("token")
280+
sub_ev = SubscriptionEvent.disable(config, data={"id": 7654321}).get()
281+
282+
self.assertEqual(mock_requests.call_count, 1, "expected call")
283+
body = mock_requests.last_request.json()
284+
self.assertEqual(body["subscription_event"]["id"], 7654321)
285+
self.assertTrue(body["subscription_event"]["disabled"])
286+
self.assertTrue(isinstance(sub_ev, SubscriptionEvent))
287+
288+
@requests_mock.mock()
289+
def test_enable_subscription_event(self, mock_requests):
290+
mock_requests.register_uri(
291+
"PATCH",
292+
"https://api.chartmogul.com/v1/subscription_events",
293+
request_headers={"Authorization": "Basic dG9rZW46"},
294+
status_code=200,
295+
json=expected_sub_ev,
296+
)
297+
298+
config = Config("token")
299+
sub_ev = SubscriptionEvent.enable(config, data={"id": 7654321}).get()
300+
301+
self.assertEqual(mock_requests.call_count, 1, "expected call")
302+
body = mock_requests.last_request.json()
303+
self.assertEqual(body["subscription_event"]["id"], 7654321)
304+
self.assertFalse(body["subscription_event"]["disabled"])
305+
227306
@requests_mock.mock()
228307
def test_all_subscription_events_with_filters(self, mock_requests):
229308
mock_requests.register_uri(

0 commit comments

Comments
 (0)