Skip to content

Commit 562c9ee

Browse files
Sylvain Fankhauserfilak-sap
authored andcommitted
Make custom work with get_entity
1 parent 4528304 commit 562c9ee

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

pyodata/v2/service.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ def __init__(self, url, connection, handler, headers=None):
242242
self._handler = handler
243243
self._headers = headers or dict()
244244
self._logger = logging.getLogger(LOGGER_NAME)
245+
self._customs = {} # string -> string hash
245246

246247
@property
247248
def handler(self):
@@ -256,7 +257,7 @@ def get_path(self):
256257
def get_query_params(self):
257258
"""Get query params"""
258259
# pylint: disable=no-self-use
259-
return {}
260+
return dict(self._customs)
260261

261262
def get_method(self):
262263
"""Get HTTP method"""
@@ -331,6 +332,12 @@ def execute(self):
331332

332333
return self._handler(response)
333334

335+
def custom(self, name, value):
336+
"""Adds a custom name-value pair."""
337+
# returns QueryRequest
338+
self._customs[name] = value
339+
return self
340+
334341

335342
class EntityGetRequest(ODataHttpRequest):
336343
"""Used for GET operations of a single entity"""
@@ -603,15 +610,8 @@ def __init__(self, url, connection, handler, last_segment):
603610
self._select = None
604611
self._expand = None
605612
self._last_segment = last_segment
606-
self._customs = {} # string -> string hash
607613
self._logger.debug('New instance of QueryRequest for last segment: %s', self._last_segment)
608614

609-
def custom(self, name, value):
610-
"""Adds a custom name-value pair."""
611-
# returns QueryRequest
612-
self._customs[name] = value
613-
return self
614-
615615
def count(self):
616616
"""Sets a flag to return the number of items."""
617617
self._count = True
@@ -685,9 +685,6 @@ def get_query_params(self):
685685
if self._select is not None:
686686
qparams['$select'] = self._select
687687

688-
for key, val in self._customs.items():
689-
qparams[key] = val
690-
691688
if self._expand is not None:
692689
qparams['$expand'] = self._expand
693690

tests/test_service_v2.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,3 +2180,18 @@ def test_odata_http_response():
21802180
assert isinstance(response.headers, dict)
21812181
assert response.headers['Content-Type'] == 'application/json'
21822182
assert response.json()['d']['ID'] == 23
2183+
2184+
2185+
@responses.activate
2186+
def test_custom_with_get_entity(service):
2187+
""" Test that `custom` can be called after `get_entity`"""
2188+
2189+
responses.add(
2190+
responses.GET,
2191+
"{0}/MasterEntities('12345')?foo=bar".format(service.url),
2192+
headers={'Content-type': 'application/json'},
2193+
json={'d': {'Key': '12345'}},
2194+
status=200)
2195+
2196+
entity = service.entity_sets.MasterEntities.get_entity('12345').custom("foo", "bar").execute()
2197+
assert entity.Key == '12345'

0 commit comments

Comments
 (0)