diff --git a/README.md b/README.md index ad54c95..c758540 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; 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 `reinclude` 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..f1bccc9 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, ReincludedCustomer 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..684dafa --- /dev/null +++ b/chartmogul/api/excluded_customer.py @@ -0,0 +1,56 @@ +from marshmallow import Schema, fields, post_load, EXCLUDE +from ..resource import Resource +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 + 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) + + @post_load + def make(self, data, **kwargs): + return ExcludedCustomer(**data) + + _schema = _Schema(unknown=EXCLUDE) + + +ExcludedCustomer.reinclude = ReincludedCustomer._method( + "destroy", "delete", "/excluded_customers{/uuid}", useCallerClass=True +) 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..a41a6a6 --- /dev/null +++ b/test/api/test_excluded_customer.py @@ -0,0 +1,111 @@ +import unittest +from datetime import datetime, timezone + +import requests_mock + +from chartmogul import ExcludedCustomer, ReincludedCustomer, 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.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_reinclude(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.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, ReincludedCustomer)) + self.assertEqual(result.customer_uuid, "cus_00000000-0000-0000-0000-000000000001") + self.assertEqual(result.status, "reimporting") + + @requests_mock.mock() + 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={ + "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.reinclude( + config, uuid="cus_00000000-0000-0000-0000-000000000001" + ).get() + + self.assertEqual(mock_requests.call_count, 1, "expected call") + self.assertTrue("does not exist" in str(context.exception))