From 219f1e4cde0bff6d6f4a65413ada422d1c690386 Mon Sep 17 00:00:00 2001 From: Evgenii Petrov Date: Wed, 25 Nov 2026 10:09:31 +0200 Subject: [PATCH 1/2] add excluded customers resource and exclude option on customer delete Co-Authored-By: Claude Opus 5.5 (1M context) --- README.md | 16 +++++ chartmogul/__init__.py | 1 + chartmogul/api/customer.py | 9 ++- chartmogul/api/excluded_customer.py | 38 ++++++++++ test/api/test_customer.py | 38 ++++++++++ test/api/test_excluded_customer.py | 107 ++++++++++++++++++++++++++++ 6 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 chartmogul/api/excluded_customer.py create mode 100644 test/api/test_excluded_customer.py diff --git a/README.md b/README.md index ad54c95..20baf05 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,9 @@ chartmogul.Customer.modify(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4c # Customer and Contact create/modify, Customer.createContact, and # CustomAttributes add/update/remove (including add by email). chartmogul.Customer.destroy(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb') +# exclude=True also adds the customer to the account's exclude list so it is not +# re-imported; it is ignored when the account doesn't have customer exclusion enabled +chartmogul.Customer.destroy(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', exclude=True) chartmogul.Customer.subscriptions(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb') chartmogul.Customer.connectSubscriptions(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', data={ 'subscriptions': [ @@ -211,6 +214,19 @@ chartmogul.Customer.tasks(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb chartmogul.Customer.createTask(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', data={}) ``` +#### Excluded Customers + +```python +# Newest first; each entry is one excluded data source customer, so the same +# customer_uuid can appear once per data source +chartmogul.ExcludedCustomer.all(config, cursor='aabbcc', per_page=20) +# Removes the customer from the exclude list; the result has customer_uuid and +# status ('reimporting' or 'manual_reimport_required') +chartmogul.ExcludedCustomer.destroy(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb') +``` + +Both endpoints respond 404 when the account doesn't have customer exclusion enabled, and `destroy` responds 404 when the customer is not excluded. + #### [Contacts](https://dev.chartmogul.com/reference/contacts/) ```python diff --git a/chartmogul/__init__.py b/chartmogul/__init__.py index 818324d..d70e695 100644 --- a/chartmogul/__init__.py +++ b/chartmogul/__init__.py @@ -13,6 +13,7 @@ from .api.customer import Customer from .api.customer_note import CustomerNote from .api.entity_note import EntityNote +from .api.excluded_customer import ExcludedCustomer from .api.contact import Contact from .api.data_source import DataSource from .api.invoice import Invoice diff --git a/chartmogul/api/customer.py b/chartmogul/api/customer.py index 7facb95..c3f4eff 100644 --- a/chartmogul/api/customer.py +++ b/chartmogul/api/customer.py @@ -36,7 +36,7 @@ class Customer(Resource): _path = "/customers{/uuid}" _root_key = "entries" - _bool_query_params = ["with_overrides"] + _bool_query_params = ["with_overrides", "exclude"] _many = namedtuple("Customers", [_root_key, "has_more", "cursor"], defaults=[None, None]) class _Schema(Schema): @@ -84,6 +84,13 @@ def make(self, data, **kwargs): _schema = _Schema(unknown=EXCLUDE) + @classmethod + def destroy(cls, config, **kwargs): + # Non-GET requests only send query params passed explicitly as query_params + if "exclude" in kwargs: + kwargs["query_params"] = {"exclude": kwargs.pop("exclude")} + return super(Customer, cls).destroy(config, **kwargs) + Customer.search = Customer._method("all", "get", "/customers/search") Customer.merge = Customer._method("merge", "post", "/customers/merges") diff --git a/chartmogul/api/excluded_customer.py b/chartmogul/api/excluded_customer.py new file mode 100644 index 0000000..160a033 --- /dev/null +++ b/chartmogul/api/excluded_customer.py @@ -0,0 +1,38 @@ +from marshmallow import Schema, fields, post_load, EXCLUDE +from ..resource import Resource +from collections import namedtuple + + +class ExcludedCustomer(Resource): + """ + Customers on the account's exclude list, which keeps them from being + re-imported. Each entry is one excluded data source customer, so the same + customer_uuid can appear once per data source. + """ + + _path = "/excluded_customers{/uuid}" + _root_key = "entries" + _many = namedtuple("ExcludedCustomers", [_root_key, "has_more", "cursor"], defaults=[None, None]) + + class _Schema(Schema): + customer_uuid = fields.String() + data_source_uuid = fields.String(allow_none=True) + external_id = fields.String(allow_none=True) + name = fields.String(allow_none=True) + email = fields.String(allow_none=True) + excluded_at = fields.DateTime(allow_none=True) + # Returned only when a customer is re-included (destroy); load_default=None + # keeps the attribute present on list entries + status = fields.String(load_default=None) + + @post_load + def make(self, data, **kwargs): + return ExcludedCustomer(**data) + + _schema = _Schema(unknown=EXCLUDE) + + @classmethod + def _load(cls, response): + # destroy responds 202 with a body, which Resource._load would discard + response.raise_for_status() + return cls._loadJSON(response.json()) diff --git a/test/api/test_customer.py b/test/api/test_customer.py index 6536920..0df79d5 100644 --- a/test/api/test_customer.py +++ b/test/api/test_customer.py @@ -1054,3 +1054,41 @@ def test_createContact_with_overrides(self, mock_requests): self.assertEqual(mock_requests.call_count, 1, "expected call") self.assertEqual(mock_requests.last_request.json(), jsonRequest) self.assertEqual(result.overrides, overrides) + + @requests_mock.mock() + def test_destroy(self, mock_requests): + mock_requests.register_uri( + "DELETE", + "https://api.chartmogul.com/v1/customers/cus_00000000-0000-0000-0000-000000000000", + status_code=202, + json={"message": "Customer deleted"}, + ) + + config = Config("token") + result = Customer.destroy(config, uuid="cus_00000000-0000-0000-0000-000000000000").get() + + self.assertEqual(mock_requests.call_count, 1, "expected call") + self.assertEqual(mock_requests.last_request.qs, {}) + self.assertEqual(mock_requests.last_request.text, None) + self.assertIsNone(result) + + # case_sensitive asserts that exclude=True is sent as the lowercase string the API expects + @requests_mock.Mocker(case_sensitive=True) + def test_destroy_with_exclude(self, mock_requests): + mock_requests.register_uri( + "DELETE", + "https://api.chartmogul.com/v1/customers/cus_00000000-0000-0000-0000-000000000000", + status_code=202, + json={"message": "Customer deleted"}, + ) + + config = Config("token") + result = Customer.destroy( + config, uuid="cus_00000000-0000-0000-0000-000000000000", exclude=True + ).get() + + self.assertEqual(mock_requests.call_count, 1, "expected call") + self.assertEqual(mock_requests.last_request.path, "/v1/customers/cus_00000000-0000-0000-0000-000000000000") + self.assertEqual(mock_requests.last_request.qs, {"exclude": ["true"]}) + self.assertEqual(mock_requests.last_request.text, None) + self.assertIsNone(result) diff --git a/test/api/test_excluded_customer.py b/test/api/test_excluded_customer.py new file mode 100644 index 0000000..a3eff4f --- /dev/null +++ b/test/api/test_excluded_customer.py @@ -0,0 +1,107 @@ +import unittest +from datetime import datetime, timezone + +import requests_mock + +from chartmogul import ExcludedCustomer, Config, APIError + +allExcludedCustomers = { + "entries": [ + { + "customer_uuid": "cus_00000000-0000-0000-0000-000000000001", + "data_source_uuid": "ds_00000000-0000-0000-0000-000000000001", + "external_id": "ext-1", + "name": "Acme", + "email": "acme@example.com", + "excluded_at": "2026-11-20T10:00:00.000Z", + }, + { + "customer_uuid": "cus_00000000-0000-0000-0000-000000000001", + "data_source_uuid": "ds_00000000-0000-0000-0000-000000000002", + "external_id": "ext-2", + "name": "Acme", + "email": None, + "excluded_at": "2026-11-19T10:00:00.000Z", + }, + ], + "cursor": "cursor==", + "has_more": True, +} + + +class ExcludedCustomerTestCase(unittest.TestCase): + @requests_mock.mock() + def test_all(self, mock_requests): + mock_requests.register_uri( + "GET", + "https://api.chartmogul.com/v1/excluded_customers?cursor=prev%3D%3D&per_page=2", + request_headers={"Authorization": "Basic dG9rZW46"}, + status_code=200, + json=allExcludedCustomers, + ) + + config = Config("token") + result = ExcludedCustomer.all(config, cursor="prev==", per_page=2).get() + + self.assertEqual(mock_requests.call_count, 1, "expected call") + self.assertEqual( + mock_requests.last_request.qs, {"cursor": ["prev=="], "per_page": ["2"]} + ) + self.assertEqual(mock_requests.last_request.text, None) + self.assertEqual(result.cursor, "cursor==") + self.assertTrue(result.has_more) + self.assertEqual(len(result.entries), 2) + entry = result.entries[0] + self.assertTrue(isinstance(entry, ExcludedCustomer)) + self.assertEqual(entry.customer_uuid, "cus_00000000-0000-0000-0000-000000000001") + self.assertEqual(entry.data_source_uuid, "ds_00000000-0000-0000-0000-000000000001") + self.assertEqual(entry.external_id, "ext-1") + self.assertEqual(entry.name, "Acme") + self.assertEqual(entry.email, "acme@example.com") + self.assertEqual(entry.excluded_at, datetime(2026, 11, 20, 10, tzinfo=timezone.utc)) + self.assertIsNone(entry.status) + self.assertEqual(result.entries[1].data_source_uuid, "ds_00000000-0000-0000-0000-000000000002") + self.assertIsNone(result.entries[1].email) + + @requests_mock.mock() + def test_destroy(self, mock_requests): + mock_requests.register_uri( + "DELETE", + "https://api.chartmogul.com/v1/excluded_customers/cus_00000000-0000-0000-0000-000000000001", + request_headers={"Authorization": "Basic dG9rZW46"}, + status_code=202, + json={ + "customer_uuid": "cus_00000000-0000-0000-0000-000000000001", + "status": "reimporting", + }, + ) + + config = Config("token") + result = ExcludedCustomer.destroy( + config, uuid="cus_00000000-0000-0000-0000-000000000001" + ).get() + + self.assertEqual(mock_requests.call_count, 1, "expected call") + self.assertEqual(mock_requests.last_request.qs, {}) + self.assertEqual(mock_requests.last_request.text, None) + self.assertTrue(isinstance(result, ExcludedCustomer)) + self.assertEqual(result.customer_uuid, "cus_00000000-0000-0000-0000-000000000001") + self.assertEqual(result.status, "reimporting") + + @requests_mock.mock() + def test_destroy_not_excluded(self, mock_requests): + mock_requests.register_uri( + "DELETE", + "https://api.chartmogul.com/v1/excluded_customers/cus_00000000-0000-0000-0000-000000000001", + status_code=404, + json={"error": "Customer not found"}, + ) + + config = Config("token") + with self.assertRaises(APIError) as context: + ExcludedCustomer.destroy( + config, uuid="cus_00000000-0000-0000-0000-000000000001" + ).get() + + self.assertEqual(mock_requests.call_count, 1, "expected call") + self.assertTrue("Customer not found" in str(context.exception)) From 9a11a32475d610e036e0a0b0f15fdd34e5a6396d Mon Sep 17 00:00:00 2001 From: Evgenii Petrov Date: Wed, 25 Nov 2026 11:38:41 +0200 Subject: [PATCH 2/2] rename excluded customer re-include to reinclude with its own result type Co-Authored-By: Claude Opus 5.5 (1M context) --- README.md | 8 +++---- chartmogul/__init__.py | 2 +- chartmogul/api/excluded_customer.py | 34 ++++++++++++++++++++++------- test/api/test_excluded_customer.py | 22 +++++++++++-------- 4 files changed, 44 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 20baf05..c758540 100644 --- a/README.md +++ b/README.md @@ -220,12 +220,12 @@ chartmogul.Customer.createTask(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133 # Newest first; each entry is one excluded data source customer, so the same # customer_uuid can appear once per data source chartmogul.ExcludedCustomer.all(config, cursor='aabbcc', per_page=20) -# Removes the customer from the exclude list; the result has customer_uuid and -# status ('reimporting' or 'manual_reimport_required') -chartmogul.ExcludedCustomer.destroy(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb') +# Removes the customer from the exclude list; returns a ReincludedCustomer with +# customer_uuid and status ('reimporting' or 'manual_reimport_required') +chartmogul.ExcludedCustomer.reinclude(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb') ``` -Both endpoints respond 404 when the account doesn't have customer exclusion enabled, and `destroy` responds 404 when the customer is not excluded. +Both endpoints respond 404 when the account doesn't have customer exclusion enabled, and `reinclude` responds 404 when the customer is not excluded. #### [Contacts](https://dev.chartmogul.com/reference/contacts/) diff --git a/chartmogul/__init__.py b/chartmogul/__init__.py index d70e695..f1bccc9 100644 --- a/chartmogul/__init__.py +++ b/chartmogul/__init__.py @@ -13,7 +13,7 @@ from .api.customer import Customer from .api.customer_note import CustomerNote from .api.entity_note import EntityNote -from .api.excluded_customer import ExcludedCustomer +from .api.excluded_customer import ExcludedCustomer, ReincludedCustomer from .api.contact import Contact from .api.data_source import DataSource from .api.invoice import Invoice diff --git a/chartmogul/api/excluded_customer.py b/chartmogul/api/excluded_customer.py index 160a033..684dafa 100644 --- a/chartmogul/api/excluded_customer.py +++ b/chartmogul/api/excluded_customer.py @@ -3,6 +3,28 @@ from collections import namedtuple +class ReincludedCustomer(Resource): + """ + Result of removing a customer from the exclude list with ExcludedCustomer.reinclude. + """ + + class _Schema(Schema): + customer_uuid = fields.String() + status = fields.String() + + @post_load + def make(self, data, **kwargs): + return ReincludedCustomer(**data) + + _schema = _Schema(unknown=EXCLUDE) + + @classmethod + def _load(cls, response): + # The API responds 202 with a body, which Resource._load would discard + response.raise_for_status() + return cls._loadJSON(response.json()) + + class ExcludedCustomer(Resource): """ Customers on the account's exclude list, which keeps them from being @@ -21,9 +43,6 @@ class _Schema(Schema): name = fields.String(allow_none=True) email = fields.String(allow_none=True) excluded_at = fields.DateTime(allow_none=True) - # Returned only when a customer is re-included (destroy); load_default=None - # keeps the attribute present on list entries - status = fields.String(load_default=None) @post_load def make(self, data, **kwargs): @@ -31,8 +50,7 @@ def make(self, data, **kwargs): _schema = _Schema(unknown=EXCLUDE) - @classmethod - def _load(cls, response): - # destroy responds 202 with a body, which Resource._load would discard - response.raise_for_status() - return cls._loadJSON(response.json()) + +ExcludedCustomer.reinclude = ReincludedCustomer._method( + "destroy", "delete", "/excluded_customers{/uuid}", useCallerClass=True +) diff --git a/test/api/test_excluded_customer.py b/test/api/test_excluded_customer.py index a3eff4f..a41a6a6 100644 --- a/test/api/test_excluded_customer.py +++ b/test/api/test_excluded_customer.py @@ -3,7 +3,7 @@ import requests_mock -from chartmogul import ExcludedCustomer, Config, APIError +from chartmogul import ExcludedCustomer, ReincludedCustomer, Config, APIError allExcludedCustomers = { "entries": [ @@ -59,12 +59,12 @@ def test_all(self, mock_requests): self.assertEqual(entry.name, "Acme") self.assertEqual(entry.email, "acme@example.com") self.assertEqual(entry.excluded_at, datetime(2026, 11, 20, 10, tzinfo=timezone.utc)) - self.assertIsNone(entry.status) + self.assertFalse(hasattr(entry, "status")) self.assertEqual(result.entries[1].data_source_uuid, "ds_00000000-0000-0000-0000-000000000002") self.assertIsNone(result.entries[1].email) @requests_mock.mock() - def test_destroy(self, mock_requests): + def test_reinclude(self, mock_requests): mock_requests.register_uri( "DELETE", "https://api.chartmogul.com/v1/excluded_customers/cus_00000000-0000-0000-0000-000000000001", @@ -77,31 +77,35 @@ def test_destroy(self, mock_requests): ) config = Config("token") - result = ExcludedCustomer.destroy( + result = ExcludedCustomer.reinclude( config, uuid="cus_00000000-0000-0000-0000-000000000001" ).get() self.assertEqual(mock_requests.call_count, 1, "expected call") self.assertEqual(mock_requests.last_request.qs, {}) self.assertEqual(mock_requests.last_request.text, None) - self.assertTrue(isinstance(result, ExcludedCustomer)) + self.assertTrue(isinstance(result, ReincludedCustomer)) self.assertEqual(result.customer_uuid, "cus_00000000-0000-0000-0000-000000000001") self.assertEqual(result.status, "reimporting") @requests_mock.mock() - def test_destroy_not_excluded(self, mock_requests): + def test_reinclude_not_excluded(self, mock_requests): mock_requests.register_uri( "DELETE", "https://api.chartmogul.com/v1/excluded_customers/cus_00000000-0000-0000-0000-000000000001", status_code=404, - json={"error": "Customer not found"}, + json={ + "code": 404, + "message": "Excluded customer \"00000000-0000-0000-0000-000000000001\" does not exist", + "param": "customer_uuid", + }, ) config = Config("token") with self.assertRaises(APIError) as context: - ExcludedCustomer.destroy( + ExcludedCustomer.reinclude( config, uuid="cus_00000000-0000-0000-0000-000000000001" ).get() self.assertEqual(mock_requests.call_count, 1, "expected call") - self.assertTrue("Customer not found" in str(context.exception)) + self.assertTrue("does not exist" in str(context.exception))