From fd556127b02705429a8ce51e3dcb526a91d7df0e Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Fri, 4 Apr 2025 16:04:34 +0530 Subject: [PATCH] Allow users to override the query parameters in content type. --- CHANGELOG.md | 6 ++++++ contentstack_management/__init__.py | 2 +- contentstack_management/assets/assets.py | 2 ++ .../content_types/content_type.py | 10 +++++---- tests/unit/content_types/test_content_type.py | 21 ++++++++++++++++++- 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dcf58c7..042e6cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## Content Management SDK For Python --- +## v1.3.2 + +#### Date: 07 April 2025 + +- Allow users to override the query parameters. +--- ## v1.3.1 #### Date: 06 February 2025 diff --git a/contentstack_management/__init__.py b/contentstack_management/__init__.py index 91077eb..91ac1ad 100644 --- a/contentstack_management/__init__.py +++ b/contentstack_management/__init__.py @@ -72,7 +72,7 @@ __author__ = 'ishaileshmishra' __status__ = 'debug' __region__ = 'na' -__version__ = '1.3.1' +__version__ = '1.3.2' __host__ = 'api.contentstack.io' __protocol__ = 'https://' __api_version__ = 'v3' diff --git a/contentstack_management/assets/assets.py b/contentstack_management/assets/assets.py index 95f9ac0..3f33149 100644 --- a/contentstack_management/assets/assets.py +++ b/contentstack_management/assets/assets.py @@ -21,6 +21,8 @@ def __init__(self, client, asset_uid, branch): self.client = client self.asset_uid = asset_uid self.branch = branch + if self.branch: + self.add_header('branch', branch) self.api_key = self.client.headers['api_key'] super().__init__(self.client) diff --git a/contentstack_management/content_types/content_type.py b/contentstack_management/content_types/content_type.py index ba79bd9..bc50341 100644 --- a/contentstack_management/content_types/content_type.py +++ b/contentstack_management/content_types/content_type.py @@ -40,11 +40,12 @@ def find(self): >>> response = content_type.find() -------------------------------- """ - self.params = { + defaults = { "include_count": "false", "include_global_field_schema": "true", "include_branch": "false" } + self.params = {**defaults, **(self.params or {})} url = "content_types" return self.client.get(url, headers=self.client.headers, params=self.params) @@ -64,11 +65,11 @@ def fetch(self): >>> response = content_type.fetch() -------------------------------- """ - self.params = { - "version": 1, + defaults = { "include_global_field_schema": "true", "include_branch": "false" } + self.params = {**defaults, **(self.params or {})} url = f"content_types/{self.content_type_uid}" return self.client.get(url, headers=self.client.headers, params=self.params) @@ -296,10 +297,11 @@ def references(self): >>> response = content_type.references() -------------------------------- """ - self.params = { + defaults = { "include_global_fields": "true", "include_branch": "false" } + self.params = {**defaults, **(self.params or {})} url = f"content_types/{self.content_type_uid}/references" return self.client.get(url, headers=self.client.headers, params=self.params) diff --git a/tests/unit/content_types/test_content_type.py b/tests/unit/content_types/test_content_type.py index 962e144..cfaed08 100644 --- a/tests/unit/content_types/test_content_type.py +++ b/tests/unit/content_types/test_content_type.py @@ -27,11 +27,30 @@ def test_get_all_content_types(self): self.assertEqual(response.request.method, "GET") self.assertEqual(response.request.headers["Content-Type"], "application/json") self.assertEqual(response.request.body, None) + + def test_get_all_content_types_with_params(self): + query = self.client.stack(api_key).content_types() + query.add_param("include_count", True) + response = query.find() + self.assertEqual(response.request.url, f"{self.client.endpoint}content_types?include_count=True&include_global_field_schema=true&include_branch=false") + self.assertEqual(response.request.method, "GET") + self.assertEqual(response.request.headers["Content-Type"], "application/json") + self.assertEqual(response.request.body, None) def test_get_a_content_types(self): response = self.client.stack(api_key).content_types(content_type_uid).fetch() self.assertEqual(response.request.url, - f"{self.client.endpoint}content_types/{content_type_uid}?version=1&include_global_field_schema=true&include_branch=false") + f"{self.client.endpoint}content_types/{content_type_uid}?include_global_field_schema=true&include_branch=false") + self.assertEqual(response.request.method, "GET") + self.assertEqual(response.request.headers["Content-Type"], "application/json") + self.assertEqual(response.request.body, None) + + def test_get_a_content_types_with_params(self): + query = self.client.stack(api_key).content_types(content_type_uid) + query.add_param("include_branch", True) + response = query.fetch() + self.assertEqual(response.request.url, + f"{self.client.endpoint}content_types/{content_type_uid}?include_global_field_schema=true&include_branch=True") self.assertEqual(response.request.method, "GET") self.assertEqual(response.request.headers["Content-Type"], "application/json") self.assertEqual(response.request.body, None)