diff --git a/CHANGELOG.md b/CHANGELOG.md index f5c2ea96..5a4da4a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ 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 ### Fixed diff --git a/pyodata/v2/service.py b/pyodata/v2/service.py index f69bfb8c..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 = {} @@ -1157,7 +1159,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 +1176,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 +1314,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 +1346,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..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""" @@ -703,6 +717,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""" @@ -714,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):