From e9e998dfebb0ac0832b634a377648ccd40be3454 Mon Sep 17 00:00:00 2001 From: Carlos Villavicencio Date: Fri, 7 Feb 2025 14:14:57 -0500 Subject: [PATCH 1/3] Make payload optimization default --- docs/reference.rst | 6 +++--- shotgun_api3/shotgun.py | 20 ++++++++++---------- tests/test_unit.py | 10 ++++++---- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/docs/reference.rst b/docs/reference.rst index 6304fc09..b3260b3b 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -950,13 +950,13 @@ Stores the number of milliseconds to wait between request retries. By default, In the case that both this environment variable and the config's ``rpc_attempt_interval`` property are set, the value in ``rpc_attempt_interal`` will be used. -SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION +SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION ======================================= .. note:: (v3.7.0) This is an experimental feature. Feel free to disable this feature if you are experiencing any issues. -When set to ``1``, this environment variable will enable the entity optimization feature. -This feature is disabled by default and is used to reduce the payload size made to the server when retrieving entities +When set to ``1``, this environment variable will disable the entity optimization feature. +This feature is enabled by default and is used to reduce the payload size made to the server when retrieving entities improving overall performance by decreasing network latency and server processing. For example, a ``find`` call like this: diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index d3f2cfba..9003fcc9 100644 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -105,7 +105,7 @@ def _is_mimetypes_broken(): SG_TIMEZONE = SgTimezone() -SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION = False +SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION = False NO_SSL_VALIDATION = False """ @@ -652,9 +652,9 @@ def __init__(self, raise ValueError("Value of SHOTGUN_API_RETRY_INTERVAL must be positive, " "got '%s'." % self.config.rpc_attempt_interval) - global SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION - if os.environ.get("SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION", "0").strip().lower() == "1": - SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION = True + global SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION + if os.environ.get("SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION", "0").strip().lower() == "1": + SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION = True self._connection = None @@ -1136,12 +1136,12 @@ def _add_project_param(self, params, project_entity): def _translate_update_params( self, entity_type, entity_id, data, multi_entity_update_modes ): - global SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION + global SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION def optimize_field(field_dict): - if SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION: - return {k: _get_type_and_id_from_value(v) for k, v in field_dict.items()} - return field_dict + if SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION: + return field_dict + return {k: _get_type_and_id_from_value(v) for k, v in field_dict.items()} full_fields = self._dict_to_list( data, @@ -4493,9 +4493,9 @@ def _translate_filters_simple(sg_filter): # Payload optimization: Do not send a full object # just send the `type` and `id` when combining related queries - global SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION + global SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION if ( - SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION + not SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION and condition["path"] != "id" and condition["relation"] in ["is", "is_not", "in", "not_in"] and isinstance(values[0], dict) diff --git a/tests/test_unit.py b/tests/test_unit.py index c8144d51..84304cab 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -408,6 +408,7 @@ def test_invalid(self): self.assertRaises(api.ShotgunError, api.shotgun._translate_filters, filters, "all") + @mock.patch.dict(os.environ, {"SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION": "1"}) def test_related_object(self): filters = [ [ @@ -434,10 +435,11 @@ def test_related_object(self): } ], } + api.Shotgun("http://server_path", "script_name", "api_key", connect=False) result = api.shotgun._translate_filters(filters, "all") self.assertEqual(result, expected) - @mock.patch.dict(os.environ, {"SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION": "1"}) + @mock.patch("shotgun_api3.shotgun.SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION", False) def test_related_object_entity_optimization_is(self): filters = [ [ @@ -487,7 +489,7 @@ def test_related_object_entity_optimization_is(self): result = api.shotgun._translate_filters(filters, "all") self.assertEqual(result, expected) - @mock.patch.dict(os.environ, {"SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION": "1"}) + @mock.patch("shotgun_api3.shotgun.SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION", False) def test_related_object_entity_optimization_in(self): filters = [ [ @@ -561,7 +563,7 @@ def test_related_object_update_entity(self): result = sg._translate_update_params(entity_type, entity_id, data, multi_entity_update_modes) self.assertEqual(result, expected) - @mock.patch.dict(os.environ, {"SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION": "1"}) + @mock.patch("shotgun_api3.shotgun.SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION", False) def test_related_object_update_optimization_entity(self): entity_type = "Anything" entity_id = 999 @@ -612,7 +614,7 @@ def test_related_object_update_optimization_entity(self): result = sg._translate_update_params(entity_type, entity_id, data, multi_entity_update_modes) self.assertEqual(result, expected) - @mock.patch.dict(os.environ, {"SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION": "1"}) + @mock.patch("shotgun_api3.shotgun.SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION", False) def test_related_object_update_optimization_entity_multi(self): entity_type = "Asset" entity_id = 6626 From 99a57bb998fb7dc85e40cf1e16d0aea6ab6dfdb1 Mon Sep 17 00:00:00 2001 From: Carlos Villavicencio Date: Fri, 7 Feb 2025 14:19:17 -0500 Subject: [PATCH 2/3] Packaging for v3.8.0 --- HISTORY.rst | 9 ++++++++- setup.py | 2 +- shotgun_api3/shotgun.py | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index ccfc44e3..1391ab20 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -4,8 +4,15 @@ Flow Production Tracking Python API Changelog Here you can see the full list of changes between each Python API release. +v3.8.0 (2024 Feb 7) +=================== + +- Keep adding optimizations like the one we did on v3.7.0. Includes ``in``, ``not_in`` filters and ``update`` method. +- Make this payload optimization enabled by default. + They can be disabled with ``SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION`` environment variable. + v3.7.0 (2024 Dec 9) -=========================== +=================== - Remove unnecessary data in the payload when combining related queries before sending it to the server. This would improve overall performance decreasing network latency and server processing. See documentation for more information. diff --git a/setup.py b/setup.py index 3305fd5a..337e3b13 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ setup( name='shotgun_api3', - version='3.7.0', + version='3.8.0', description='Flow Production Tracking Python API', long_description=readme, author='Autodesk', diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 9003fcc9..f0d4faf4 100644 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -118,7 +118,7 @@ def _is_mimetypes_broken(): # ---------------------------------------------------------------------------- # Version -__version__ = "3.7.0" +__version__ = "3.8.0" # ---------------------------------------------------------------------------- # Errors From 6b31a01bf944fa748dfbef44da1f57b92b060da2 Mon Sep 17 00:00:00 2001 From: Carlos Villavicencio <123113322+carlos-villavicencio-adsk@users.noreply.github.com> Date: Tue, 11 Feb 2025 14:32:18 -0500 Subject: [PATCH 3/3] Update HISTORY.rst Co-authored-by: Julien Langlois <16244608+julien-lang@users.noreply.github.com> --- HISTORY.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 1391ab20..82309527 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -7,9 +7,11 @@ Here you can see the full list of changes between each Python API release. v3.8.0 (2024 Feb 7) =================== -- Keep adding optimizations like the one we did on v3.7.0. Includes ``in``, ``not_in`` filters and ``update`` method. -- Make this payload optimization enabled by default. - They can be disabled with ``SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION`` environment variable. +- Extend the payload optimizations to the ``in`` and ``not_in`` filters and + the ``update`` method. +- The payload optimization is now enabled by default. + It can be disabled with the ``SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION`` + environment variable. v3.7.0 (2024 Dec 9) ===================