Skip to content

Commit e4c5319

Browse files
committed
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.
1 parent adf9f75 commit e4c5319

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88

99
### Added
1010
- Specify PATCH or PUT method for EntityUpdateRequest - Barton Ip
11+
- Add a Service wide configuration (e.g. http.update\_method) - Jakub Filak
1112
- <, <=, >, >= operators on GetEntitySetFilter - Barton Ip
1213

1314
### Fixed

pyodata/v2/service.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ def create_entity_handler(response):
11571157
return EntityCreateRequest(self._service.url, self._service.connection, create_entity_handler, self._entity_set,
11581158
self.last_segment)
11591159

1160-
def update_entity(self, key=None, method="PATCH", **kwargs):
1160+
def update_entity(self, key=None, method=None, **kwargs):
11611161
"""Updates an existing entity in the given entity-set."""
11621162

11631163
def update_entity_handler(response):
@@ -1174,6 +1174,9 @@ def update_entity_handler(response):
11741174

11751175
self._logger.info('Updating entity %s for key %s and args %s', self._entity_set.entity_type.name, key, kwargs)
11761176

1177+
if method is None:
1178+
method = self._service.config['http']['update_method']
1179+
11771180
return EntityModifyRequest(self._service.url, self._service.connection, update_entity_handler, self._entity_set,
11781181
entity_key, method=method)
11791182

@@ -1309,6 +1312,8 @@ def __init__(self, url, schema, connection):
13091312
self._entity_container = EntityContainer(self)
13101313
self._function_container = FunctionContainer(self)
13111314

1315+
self._config = {'http': {'update_method': 'PATCH'}}
1316+
13121317
@property
13131318
def schema(self):
13141319
"""Parsed metadata"""
@@ -1339,6 +1344,12 @@ def functions(self):
13391344

13401345
return self._function_container
13411346

1347+
@property
1348+
def config(self):
1349+
"""Service specific configuration"""
1350+
1351+
return self._config
1352+
13421353
def http_get(self, path, connection=None):
13431354
"""HTTP GET response for the passed path in the service"""
13441355

tests/test_service_v2.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,22 @@ def test_update_entity_with_no_method_specified(service):
703703
assert query.get_method() == "PATCH"
704704

705705

706+
def test_update_entity_with_service_config_set_to_put(service):
707+
"""Make sure the method update_entity handles correctly when no method is specified"""
708+
709+
# pylint: disable=redefined-outer-name
710+
711+
712+
key = EntityKey(
713+
service.schema.entity_type('TemperatureMeasurement'),
714+
Sensor='sensor1',
715+
Date=datetime.datetime(2017, 12, 24, 18, 0))
716+
717+
service.config['http']['update_method'] = "PUT"
718+
query = service.entity_sets.TemperatureMeasurements.update_entity(key)
719+
assert query.get_method() == "PUT"
720+
721+
706722
def test_update_entity_with_wrong_method_specified(service):
707723
"""Make sure the method update_entity raises ValueError when wrong method is specified"""
708724

0 commit comments

Comments
 (0)