From 598345ebb5d49de1fa14a09b439f07605a0775ee Mon Sep 17 00:00:00 2001 From: Jakub Filak Date: Fri, 26 Jun 2020 16:08:46 +0200 Subject: [PATCH 1/2] service: add service wide configuration In the case where we need to update several entity sets, we probably don't want to specify HTTP method for each of them. This patch does nothing but enhances user convenience. --- CHANGELOG.md | 1 + pyodata/v2/service.py | 13 ++++++++++++- tests/test_service_v2.py | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5c2ea96..e2acb555 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - Specify PATCH or PUT method for EntityUpdateRequest - Barton Ip +- Add a Service wide configuration (e.g. http.update\_method) - Jakub Filak - <, <=, >, >= operators on GetEntitySetFilter - Barton Ip ### Fixed diff --git a/pyodata/v2/service.py b/pyodata/v2/service.py index f69bfb8c..0bbafa56 100644 --- a/pyodata/v2/service.py +++ b/pyodata/v2/service.py @@ -1157,7 +1157,7 @@ def create_entity_handler(response): return EntityCreateRequest(self._service.url, self._service.connection, create_entity_handler, self._entity_set, self.last_segment) - def update_entity(self, key=None, method="PATCH", **kwargs): + def update_entity(self, key=None, method=None, **kwargs): """Updates an existing entity in the given entity-set.""" def update_entity_handler(response): @@ -1174,6 +1174,9 @@ def update_entity_handler(response): self._logger.info('Updating entity %s for key %s and args %s', self._entity_set.entity_type.name, key, kwargs) + if method is None: + method = self._service.config['http']['update_method'] + return EntityModifyRequest(self._service.url, self._service.connection, update_entity_handler, self._entity_set, entity_key, method=method) @@ -1309,6 +1312,8 @@ def __init__(self, url, schema, connection): self._entity_container = EntityContainer(self) self._function_container = FunctionContainer(self) + self._config = {'http': {'update_method': 'PATCH'}} + @property def schema(self): """Parsed metadata""" @@ -1339,6 +1344,12 @@ def functions(self): return self._function_container + @property + def config(self): + """Service specific configuration""" + + return self._config + def http_get(self, path, connection=None): """HTTP GET response for the passed path in the service""" diff --git a/tests/test_service_v2.py b/tests/test_service_v2.py index 9dee5fb3..c4edaa4f 100644 --- a/tests/test_service_v2.py +++ b/tests/test_service_v2.py @@ -703,6 +703,22 @@ def test_update_entity_with_no_method_specified(service): assert query.get_method() == "PATCH" +def test_update_entity_with_service_config_set_to_put(service): + """Make sure the method update_entity handles correctly when no method is specified""" + + # pylint: disable=redefined-outer-name + + + key = EntityKey( + service.schema.entity_type('TemperatureMeasurement'), + Sensor='sensor1', + Date=datetime.datetime(2017, 12, 24, 18, 0)) + + service.config['http']['update_method'] = "PUT" + query = service.entity_sets.TemperatureMeasurements.update_entity(key) + assert query.get_method() == "PUT" + + def test_update_entity_with_wrong_method_specified(service): """Make sure the method update_entity raises ValueError when wrong method is specified""" From 5c86e9104d4a2ff048709e598ecee899342929b4 Mon Sep 17 00:00:00 2001 From: Jakub Filak Date: Fri, 26 Jun 2020 17:45:31 +0200 Subject: [PATCH 2/2] service: allow MERGE method for Update Entity Closes #103 --- CHANGELOG.md | 2 +- pyodata/v2/service.py | 10 ++++++---- tests/test_service_v2.py | 20 ++++++++++++++++++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2acb555..5a4da4a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ### Added -- Specify PATCH or PUT method for EntityUpdateRequest - Barton Ip +- Specify PATCH, PUT, or MERGE method for EntityUpdateRequest - Barton Ip - Add a Service wide configuration (e.g. http.update\_method) - Jakub Filak - <, <=, >, >= operators on GetEntitySetFilter - Barton Ip diff --git a/pyodata/v2/service.py b/pyodata/v2/service.py index 0bbafa56..ad01be0e 100644 --- a/pyodata/v2/service.py +++ b/pyodata/v2/service.py @@ -512,6 +512,8 @@ class EntityModifyRequest(ODataHttpRequest): Call execute() to send the update-request to the OData service and get the modified entity.""" + ALLOWED_HTTP_METHODS = ['PATCH', 'PUT', 'MERGE'] + def __init__(self, url, connection, handler, entity_set, entity_key, method="PATCH"): super(EntityModifyRequest, self).__init__(url, connection, handler) self._logger = logging.getLogger(LOGGER_NAME) @@ -519,10 +521,10 @@ def __init__(self, url, connection, handler, entity_set, entity_key, method="PAT self._entity_type = entity_set.entity_type self._entity_key = entity_key - if method.upper() not in ["PATCH", "PUT"]: - raise ValueError("Method must be either PATCH or PUT") - - self._method = method + self._method = method.upper() + if self._method not in EntityModifyRequest.ALLOWED_HTTP_METHODS: + raise ValueError('The value "{}" is not on the list of allowed Entity Update HTTP Methods: {}' + .format(method, ', '.join(EntityModifyRequest.ALLOWED_HTTP_METHODS))) self._values = {} diff --git a/tests/test_service_v2.py b/tests/test_service_v2.py index c4edaa4f..59861d70 100644 --- a/tests/test_service_v2.py +++ b/tests/test_service_v2.py @@ -687,6 +687,20 @@ def test_update_entity_with_patch_method_specified(service): query = service.entity_sets.TemperatureMeasurements.update_entity(key, method="PATCH") assert query.get_method() == "PATCH" +def test_update_entity_with_merge_method_specified(service): + """Make sure the method update_entity handles correctly when MERGE method is specified""" + + # pylint: disable=redefined-outer-name + + + key = EntityKey( + service.schema.entity_type('TemperatureMeasurement'), + Sensor='sensor1', + Date=datetime.datetime(2017, 12, 24, 18, 0)) + + query = service.entity_sets.TemperatureMeasurements.update_entity(key, method='merge') + assert query.get_method() == 'MERGE' + def test_update_entity_with_no_method_specified(service): """Make sure the method update_entity handles correctly when no method is specified""" @@ -730,8 +744,10 @@ def test_update_entity_with_wrong_method_specified(service): Sensor='sensor1', Date=datetime.datetime(2017, 12, 24, 18, 0)) - with pytest.raises(ValueError): - service.entity_sets.TemperatureMeasurements.update_entity(key, method="DELETE") + with pytest.raises(ValueError) as caught_ex: + service.entity_sets.TemperatureMeasurements.update_entity(key, method='DELETE') + + assert str(caught_ex.value).startswith('The value "DELETE" is not on the list of allowed Entity Update HTTP Methods: PATCH, PUT, MERGE') def test_get_entity_with_entity_key_and_other_params(service):