diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b00004..fb37ade 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # CHANGELOG ## Content Management SDK For Python +--- +## v1.13.0 + +#### Date: 04 December 2026 + +- Taxonomy: added `publish(data)`, `localize(data, locale)` and `unlocalize(locale)`. A 2xx on publish means accepted, not completed. (DX-10478) + --- ## v1.12.0 diff --git a/contentstack_management/__init__.py b/contentstack_management/__init__.py index 98222aa..fc78010 100644 --- a/contentstack_management/__init__.py +++ b/contentstack_management/__init__.py @@ -102,7 +102,7 @@ def get_contentstack_endpoint(region='us', service='', omit_https=False): __author__ = 'dev-ex' __status__ = 'debug' __region__ = 'na' -__version__ = '1.12.0' +__version__ = '1.13.0' __host__ = 'api.contentstack.io' __protocol__ = 'https://' __api_version__ = 'v3' diff --git a/contentstack_management/_messages.py b/contentstack_management/_messages.py index 0e33e83..751daf7 100644 --- a/contentstack_management/_messages.py +++ b/contentstack_management/_messages.py @@ -101,6 +101,7 @@ # Taxonomy messages TAXONOMY_UID_REQUIRED = "Taxonomy UID is required. Provide a valid Taxonomy UID and try again." +TAXONOMY_UID_NOT_ALLOWED = "Taxonomy UID is not allowed for this call. Publish is collection-level: call taxonomy() without a Taxonomy UID and try again." # Terms messages TERMS_UID_REQUIRED = "Terms UID is required. Provide a valid Terms UID and try again." diff --git a/contentstack_management/taxonomies/taxonomy.py b/contentstack_management/taxonomies/taxonomy.py index 1223a28..80fab7b 100644 --- a/contentstack_management/taxonomies/taxonomy.py +++ b/contentstack_management/taxonomies/taxonomy.py @@ -7,7 +7,7 @@ from ..common import Parameter from .._errors import ArgumentException from ..terms.terms import Terms -from .._messages import TAXONOMY_UID_REQUIRED +from .._messages import TAXONOMY_UID_REQUIRED, TAXONOMY_UID_NOT_ALLOWED, LOCALE_CODE_REQUIRED class Taxonomy(Parameter): """ @@ -151,6 +151,106 @@ def validate_taxonomy_uid(self): def terms(self, terms_uid: str = None): self.validate_taxonomy_uid() return Terms(self.client, self.taxonomy_uid, terms_uid) + + def publish(self, data: dict): + """ + The "Publish taxonomies" call starts a job that publishes one or more taxonomies to the + given environments and locales. It is collection-level: call it from taxonomy() without a + Taxonomy UID. + + A 2xx response means the publish request was ACCEPTED and queued. It does not mean the + publish has completed. To confirm the outcome, check the publish queue with + `client.stack('api_key').publish_queue().find()`, or subscribe to the publish-failure webhooks. + + Note: a `branch` header set through `add_header` is written to the client-wide + `client.client.headers` and stays set for later calls on the same client (params are per instance). + + :param data: The `data` parameter is the payload that you want to send in the request body. + It should be a dictionary with `locales`, `environments` and `items`, where each item is `{"uid": ...}`. + :return: Json, with the acknowledgement of the accepted publish request. + + ------------------------------- + [Example:] + >>> data = { + >>> "locales": ["en-us"], + >>> "environments": ["development"], + >>> "items": [{"uid": "taxonomy_1"}] + >>> } + >>> import contentstack_management + >>> client = contentstack_management.Client(authtoken='your_authtoken') + >>> result = client.stack('api_key').taxonomy().publish(data).json() + + ------------------------------- + """ + if self.taxonomy_uid: + raise ArgumentException(TAXONOMY_UID_NOT_ALLOWED) + + data = json.dumps(data) + url = f"{self.path}/publish" + return self.client.post(url, headers = self.client.headers, data=data, params = self.params) + + def localize(self, data: dict, locale: str): + """ + The "Localize taxonomy" call creates a localized version of a taxonomy in the given locale. + It is instance-level: call it from taxonomy(taxonomy_uid). + + :param data: The `data` parameter is the payload that you want to send in the request body, + sent as given. It should be a dictionary, for example `{"taxonomy": {...}}`. + :param locale: Required, must not be blank. The locale to localize the taxonomy into, for example `fr-fr`. + :return: Json, with the localized taxonomy details. + + ------------------------------- + [Example:] + >>> data = { + >>> "taxonomy": { + >>> "name": "Taxonomie 1", + >>> "description": "Description fr" + >>> } + >>> } + >>> import contentstack_management + >>> client = contentstack_management.Client(authtoken='your_authtoken') + >>> result = client.stack('api_key').taxonomy('taxonomy_uid').localize(data, 'fr-fr').json() + + ------------------------------- + """ + if not self.taxonomy_uid: + raise ArgumentException(TAXONOMY_UID_REQUIRED) + + if not locale or not str(locale).strip(): + raise ArgumentException(LOCALE_CODE_REQUIRED) + + self.params['locale'] = locale + url = f"{self.path}/{self.taxonomy_uid}" + data = json.dumps(data) + return self.client.post(url, headers = self.client.headers, data=data, params = self.params) + + def unlocalize(self, locale: str): + """ + The "Unlocalize taxonomy" call removes the localized version of a taxonomy for the given locale. + It is instance-level: call it from taxonomy(taxonomy_uid). + The API responds with 204 No Content and an empty body, so the raw response is returned. + + :param locale: Required, must not be blank. The locale whose localized version of the taxonomy is removed, + for example `fr-fr`. A missing locale is rejected so the call can never become a whole-taxonomy delete. + :return: The raw response. The status code is 204 No Content on success. + + ------------------------------- + [Example:] + >>> import contentstack_management + >>> client = contentstack_management.Client(authtoken='your_authtoken') + >>> result = client.stack('api_key').taxonomy('taxonomy_uid').unlocalize('fr-fr') + + ------------------------------- + """ + if not self.taxonomy_uid: + raise ArgumentException(TAXONOMY_UID_REQUIRED) + + if not locale or not str(locale).strip(): + raise ArgumentException(LOCALE_CODE_REQUIRED) + + self.params['locale'] = locale + url = f"{self.path}/{self.taxonomy_uid}" + return self.client.delete(url, headers = self.client.headers, params = self.params) \ No newline at end of file diff --git a/tests/api/taxonomies/test_taxonomy_api.py b/tests/api/taxonomies/test_taxonomy_api.py index 10e9463..6b7418f 100644 --- a/tests/api/taxonomies/test_taxonomy_api.py +++ b/tests/api/taxonomies/test_taxonomy_api.py @@ -1,4 +1,5 @@ import unittest +import uuid import contentstack_management from tests.cred import get_credentials @@ -51,5 +52,135 @@ def test_delete_taxonomy(self): response = self.client.stack(api_key).taxonomy(taxonomy_uid).delete() self.assertEqual(response.status_code, 200) + # Publish / localize / unlocalize. Environments, locales and branches are looked up on the + # stack that runs the suite; a test skips with a reason when the stack has none to use. + # Taxonomies are created per test with a unique UID and deleted in cleanup. + + def _create_taxonomy(self): + uid = f"sdk_it_{uuid.uuid4().hex[:8]}" + data = { + "taxonomy": { + "uid": uid, + "name": "SDK integration taxonomy", + "description": "Created by the SDK integration tests" + } + } + response = self.client.stack(api_key).taxonomy().create(data) + self.assertEqual(response.status_code, 201) + self.addCleanup(self._delete_taxonomy, uid) + return uid + + def _delete_taxonomy(self, uid): + response = self.client.stack(api_key).taxonomy(uid).delete() + self.assertEqual(response.status_code, 200) + + def _environment_names(self): + response = self.client.stack(api_key).environments().find() + self.assertEqual(response.status_code, 200) + return [env["name"] for env in response.json().get("environments", [])] + + def _locale_codes(self, exclude_master=False): + response = self.client.stack(api_key).locale().find() + self.assertEqual(response.status_code, 200) + return [loc["code"] for loc in response.json().get("locales", []) + if not (exclude_master and loc.get("default"))] + + def _branch_uids(self): + # Unlike environments and locales, which every stack can list (so a failed listing fails the test), + # branching is a plan feature: a stack without it cannot list branches, so the test skips here. + # The skip reason names the HTTP status, so a real 401 or 5xx is named rather than silently passed. + response = self.client.stack(api_key).branch().find() + if not 200 <= response.status_code < 300: + self.skipTest(f"could not list branches (HTTP {response.status_code}); " + "not treated as 'no main branch'") + return [branch["uid"] for branch in response.json().get("branches", [])] + + def test_publish_one_taxonomy_to_one_environment_and_locale_returns_acknowledgement(self): + environments = self._environment_names() + locales = self._locale_codes() + if not environments or not locales: + self.skipTest("stack has no environment or locale to publish to") + uid = self._create_taxonomy() + data = { + "locales": [locales[0]], + "environments": [environments[0]], + "items": [{"uid": uid}] + } + response = self.client.stack(api_key).taxonomy().publish(data) + # The API accepts the publish job; it does not report that publishing completed. + self.assertTrue(200 <= response.status_code < 300, response.text) + body = response.json() + self.assertTrue("notice" in body or "job_id" in body, body) + + def test_publish_multiple_locales_and_environments_returns_acknowledgement(self): + environments = self._environment_names() + locales = self._locale_codes() + if len(environments) < 2 or len(locales) < 2: + self.skipTest("stack needs at least two environments and two locales") + uid = self._create_taxonomy() + data = { + "locales": locales[:2], + "environments": environments[:2], + "items": [{"uid": uid}] + } + response = self.client.stack(api_key).taxonomy().publish(data) + self.assertTrue(200 <= response.status_code < 300, response.text) + body = response.json() + self.assertTrue("notice" in body or "job_id" in body, body) + + def test_publish_with_branch_header_returns_acknowledgement(self): + environments = self._environment_names() + locales = self._locale_codes() + branches = self._branch_uids() + if not environments or not locales: + self.skipTest("stack has no environment or locale to publish with") + if "main" not in branches: + self.skipTest("stack has no branch named 'main'") + uid = self._create_taxonomy() + # Asserts only that publish accepts a `branch` header, not that routing changed. + # Registered after _create_taxonomy's cleanup, so the shared header is removed + # before the taxonomy DELETE runs (cleanups run last-in, first-out). + self.addCleanup(self.client.client.headers.pop, "branch", None) + data = { + "locales": [locales[0]], + "environments": [environments[0]], + "items": [{"uid": uid}] + } + taxonomy = self.client.stack(api_key).taxonomy() + taxonomy.add_header("branch", "main") + response = taxonomy.publish(data) + self.assertTrue(200 <= response.status_code < 300, response.text) + body = response.json() + self.assertTrue("notice" in body or "job_id" in body, body) + + def test_localize_taxonomy_into_non_master_locale(self): + locales = self._locale_codes(exclude_master=True) + if not locales: + self.skipTest("stack has no non-master locale to localize into") + uid = self._create_taxonomy() + data = { + "taxonomy": { + "name": "SDK localized name", + "description": "SDK localized description" + } + } + response = self.client.stack(api_key).taxonomy(uid).localize(data, locales[0]) + self.assertIn(response.status_code, (200, 201), response.text) + self.assertEqual(response.json()["taxonomy"]["uid"], uid) + + def test_unlocalize_taxonomy_returns_204_with_empty_body(self): + locales = self._locale_codes(exclude_master=True) + if not locales: + self.skipTest("stack has no non-master locale to localize and unlocalize") + uid = self._create_taxonomy() + data = {"taxonomy": {"name": "SDK localized name"}} + localized = self.client.stack(api_key).taxonomy(uid).localize(data, locales[0]) + self.assertIn(localized.status_code, (200, 201), localized.text) + response = self.client.stack(api_key).taxonomy(uid).unlocalize(locales[0]) + self.assertEqual(response.status_code, 204) + # 204 No Content: the body is empty and is not parsed as JSON. + self.assertEqual(response.text, "") + + if __name__ == '__main__': unittest.main() diff --git a/tests/unit/taxonomies/test_taxonomy_unit.py b/tests/unit/taxonomies/test_taxonomy_unit.py index 561ca88..4c18993 100644 --- a/tests/unit/taxonomies/test_taxonomy_unit.py +++ b/tests/unit/taxonomies/test_taxonomy_unit.py @@ -1,5 +1,10 @@ +import inspect +import json import unittest import contentstack_management +from contentstack_management import _messages +from contentstack_management._errors import ArgumentException +from contentstack_management.taxonomies.taxonomy import Taxonomy from tests.cred import get_credentials credentials = get_credentials() @@ -71,3 +76,172 @@ def test_delete_taxonomy(self): response = self.client.stack(api_key).taxonomy(taxonomy_uid).delete() self.assertEqual(response.request.url, f"{self.client.endpoint}taxonomies/{taxonomy_uid}") self.assertEqual(response.request.method, "DELETE") + + # ---- publish / localize / unlocalize ---- + + def _publish_payload(self, locales=None, environments=None, items=None): + return { + "locales": ["en-us"] if locales is None else locales, + "environments": ["development"] if environments is None else environments, + "items": [{"uid": "taxonomy_1"}] if items is None else items, + } + + def test_publish_one_taxonomy_to_one_environment_and_locale_posts_flat_body(self): + data = self._publish_payload() + response = self.client.stack(api_key).taxonomy().publish(data) + self.assertEqual(response.request.url, f"{self.client.endpoint}taxonomies/publish") + self.assertEqual(response.request.method, "POST") + self.assertEqual(response.request.headers["Content-Type"], "application/json") + self.assertEqual(json.loads(response.request.body), data) + + def test_publish_multiple_locales_and_environments_sends_all_in_one_request(self): + data = self._publish_payload(locales=["en-us", "fr-fr"], environments=["development", "production"]) + response = self.client.stack(api_key).taxonomy().publish(data) + body = json.loads(response.request.body) + self.assertEqual(body["locales"], ["en-us", "fr-fr"]) + self.assertEqual(body["environments"], ["development", "production"]) + self.assertEqual(response.request.method, "POST") + + def test_publish_with_branch_sends_branch_header_and_no_branch_query_param(self): + taxonomy = self.client.stack(api_key).taxonomy() + taxonomy.add_header("branch", "feature_branch") + response = taxonomy.publish(self._publish_payload()) + self.assertEqual(response.request.headers["branch"], "feature_branch") + self.assertNotIn("branch", response.request.url) + self.assertEqual(response.request.url, f"{self.client.endpoint}taxonomies/publish") + + def test_localize_posts_to_uid_with_locale_query_and_body_as_given(self): + data = {"taxonomy": {"name": "Taxonomie 1", "description": "Description fr"}} + response = self.client.stack(api_key).taxonomy(taxonomy_uid).localize(data, "fr-fr") + self.assertEqual(response.request.url, + f"{self.client.endpoint}taxonomies/{taxonomy_uid}?locale=fr-fr") + self.assertEqual(response.request.method, "POST") + self.assertEqual(json.loads(response.request.body), data) + + def test_unlocalize_deletes_uid_with_locale_query_and_no_body(self): + response = self.client.stack(api_key).taxonomy(taxonomy_uid).unlocalize("fr-fr") + self.assertEqual(response.request.url, + f"{self.client.endpoint}taxonomies/{taxonomy_uid}?locale=fr-fr") + self.assertEqual(response.request.method, "DELETE") + self.assertEqual(response.request.body, None) + + def test_publish_items_carry_uid_only_and_never_term_uid(self): + data = self._publish_payload(items=[{"uid": "taxonomy_1"}, {"uid": "taxonomy_2"}]) + response = self.client.stack(api_key).taxonomy().publish(data) + body = json.loads(response.request.body) + self.assertEqual(body["items"], [{"uid": "taxonomy_1"}, {"uid": "taxonomy_2"}]) + self.assertNotIn("term_uid", response.request.body) + + def test_publish_docstring_says_2xx_means_accepted_and_points_at_monitoring(self): + doc = (Taxonomy.publish.__doc__ or "").lower() + self.assertIn("accepted", doc) + self.assertIn("2xx", doc) + self.assertTrue("publish queue" in doc or "publish-queue" in doc) + self.assertIn("webhook", doc) + self.assertIn("add_header", doc) + self.assertIn("publish_queue().find()", doc) + self.assertIn("client.client.headers", doc) + + def test_publish_on_uid_bound_instance_raises_argument_exception(self): + taxonomy = self.client.stack(api_key).taxonomy(taxonomy_uid) + with self.assertRaises(ArgumentException) as ctx: + taxonomy.publish(self._publish_payload()) + self.assertEqual(str(ctx.exception), _messages.TAXONOMY_UID_NOT_ALLOWED) + + def test_localize_without_uid_raises_argument_exception(self): + taxonomy = self.client.stack(api_key).taxonomy() + with self.assertRaises(ArgumentException) as ctx: + taxonomy.localize({"taxonomy": {"name": "x"}}, "fr-fr") + self.assertEqual(str(ctx.exception), _messages.TAXONOMY_UID_REQUIRED) + + def test_unlocalize_without_uid_raises_argument_exception(self): + taxonomy = self.client.stack(api_key).taxonomy() + with self.assertRaises(ArgumentException) as ctx: + taxonomy.unlocalize("fr-fr") + self.assertEqual(str(ctx.exception), _messages.TAXONOMY_UID_REQUIRED) + + def test_localize_with_none_locale_raises_argument_exception(self): + self._assert_locale_guard(lambda t, locale: t.localize({"taxonomy": {"name": "x"}}, locale), [None]) + + def test_localize_with_blank_locale_raises_argument_exception(self): + self._assert_locale_guard(lambda t, locale: t.localize({"taxonomy": {"name": "x"}}, locale), ["", " "]) + + def test_unlocalize_with_none_locale_raises_argument_exception(self): + self._assert_locale_guard(lambda t, locale: t.unlocalize(locale), [None]) + + def test_unlocalize_with_blank_locale_raises_argument_exception(self): + self._assert_locale_guard(lambda t, locale: t.unlocalize(locale), ["", " "]) + + def _assert_locale_guard(self, call, locales): + for locale in locales: + with self.subTest(locale=locale): + taxonomy = self.client.stack(api_key).taxonomy(taxonomy_uid) + with self.assertRaises(ArgumentException) as ctx: + call(taxonomy, locale) + self.assertEqual(str(ctx.exception), _messages.LOCALE_CODE_REQUIRED) + self.assertNotIn("locale", taxonomy.params) + + def test_publish_with_empty_string_uid_is_treated_as_collection_level(self): + # "" is falsy: the publish guard rejects only a truthy uid (arch contract), so an + # empty uid stays collection-level and publish must still POST to taxonomies/publish. + response = self.client.stack(api_key).taxonomy("").publish(self._publish_payload()) + self.assertEqual(response.request.url, f"{self.client.endpoint}taxonomies/publish") + + def test_localize_with_empty_string_uid_raises_argument_exception(self): + taxonomy = self.client.stack(api_key).taxonomy("") + with self.assertRaises(ArgumentException) as ctx: + taxonomy.localize({"taxonomy": {"name": "x"}}, "fr-fr") + self.assertEqual(str(ctx.exception), _messages.TAXONOMY_UID_REQUIRED) + + def test_unlocalize_with_empty_string_uid_raises_argument_exception(self): + taxonomy = self.client.stack(api_key).taxonomy("") + with self.assertRaises(ArgumentException) as ctx: + taxonomy.unlocalize("fr-fr") + self.assertEqual(str(ctx.exception), _messages.TAXONOMY_UID_REQUIRED) + + def test_publish_sends_empty_locales_environments_and_items_as_given(self): + data = {"locales": [], "environments": [], "items": []} + response = self.client.stack(api_key).taxonomy().publish(data) + self.assertEqual(response.request.method, "POST") + self.assertEqual(json.loads(response.request.body), data) + + def test_localize_locale_argument_wins_over_previously_added_locale_param(self): + taxonomy = self.client.stack(api_key).taxonomy(taxonomy_uid) + taxonomy.add_param("locale", "de-de") + response = taxonomy.localize({"taxonomy": {"name": "x"}}, "fr-fr") + self.assertEqual(response.request.url, + f"{self.client.endpoint}taxonomies/{taxonomy_uid}?locale=fr-fr") + + def test_unlocalize_locale_argument_wins_over_previously_added_locale_param(self): + taxonomy = self.client.stack(api_key).taxonomy(taxonomy_uid) + taxonomy.add_param("locale", "de-de") + response = taxonomy.unlocalize("fr-fr") + self.assertEqual(response.request.url, + f"{self.client.endpoint}taxonomies/{taxonomy_uid}?locale=fr-fr") + + def test_publish_params_do_not_leak_into_a_later_taxonomy_instance(self): + first = self.client.stack(api_key).taxonomy() + first.add_param("include_branch", True) + first_response = first.publish(self._publish_payload()) + self.assertEqual(first_response.request.url, + f"{self.client.endpoint}taxonomies/publish?include_branch=True") + second = self.client.stack(api_key).taxonomy() + response = second.publish(self._publish_payload()) + self.assertEqual(response.request.url, f"{self.client.endpoint}taxonomies/publish") + + def test_localize_added_param_reaches_url_alongside_locale(self): + taxonomy = self.client.stack(api_key).taxonomy(taxonomy_uid) + taxonomy.add_param("include_branch", True) + response = taxonomy.localize({"taxonomy": {"name": "x"}}, "fr-fr") + self.assertIn("include_branch=True", response.request.url) + self.assertIn("locale=fr-fr", response.request.url) + + def test_unlocalize_added_param_reaches_url_alongside_locale(self): + taxonomy = self.client.stack(api_key).taxonomy(taxonomy_uid) + taxonomy.add_param("include_branch", True) + response = taxonomy.unlocalize("fr-fr") + self.assertIn("include_branch=True", response.request.url) + self.assertIn("locale=fr-fr", response.request.url) + + def test_publish_signature_is_exactly_self_and_data(self): + self.assertEqual(list(inspect.signature(Taxonomy.publish).parameters), ["self", "data"])