@@ -232,14 +232,15 @@ def __repr__(self):
232232class ODataHttpRequest :
233233 """Deferred HTTP Request"""
234234
235- def __init__ (self , url , connection , handler , headers = None ):
235+ def __init__ (self , url , connection , handler , headers = None , response_hook = None ):
236236 self ._connection = connection
237237 self ._url = url
238238 self ._handler = handler
239239 self ._headers = headers or dict ()
240240 self ._logger = logging .getLogger (LOGGER_NAME )
241241 self ._customs = {} # string -> string hash
242242 self ._next_url = None
243+ self ._response_hook = response_hook
243244
244245 @property
245246 def handler (self ):
@@ -359,6 +360,9 @@ def _call_handler(self, response):
359360 except UnicodeDecodeError :
360361 self ._logger .debug (' body: <cannot be decoded>' )
361362
363+ if self ._response_hook is not None :
364+ self ._response_hook (response )
365+
362366 return self ._handler (response )
363367
364368 def custom (self , name , value ):
@@ -373,7 +377,7 @@ class EntityGetRequest(ODataHttpRequest):
373377
374378 def __init__ (self , handler , entity_key , entity_set_proxy , encode_path = True ):
375379 super (EntityGetRequest , self ).__init__ (entity_set_proxy .service .url , entity_set_proxy .service .connection ,
376- handler )
380+ handler , response_hook = entity_set_proxy . service . response_hook )
377381 self ._logger = logging .getLogger (LOGGER_NAME )
378382 self ._entity_key = entity_key
379383 self ._entity_set_proxy = entity_set_proxy
@@ -465,8 +469,8 @@ class EntityCreateRequest(ODataHttpRequest):
465469 Call execute() to send the create-request to the OData service
466470 and get the newly created entity."""
467471
468- def __init__ (self , url , connection , handler , entity_set , last_segment = None ):
469- super (EntityCreateRequest , self ).__init__ (url , connection , handler )
472+ def __init__ (self , url , connection , handler , entity_set , last_segment = None , response_hook = None ):
473+ super (EntityCreateRequest , self ).__init__ (url , connection , handler , response_hook = response_hook )
470474 self ._logger = logging .getLogger (LOGGER_NAME )
471475 self ._entity_set = entity_set
472476 self ._entity_type = entity_set .entity_type
@@ -552,8 +556,8 @@ def set(self, **kwargs):
552556class EntityDeleteRequest (ODataHttpRequest ):
553557 """Used for deleting entity (DELETE operations on a single entity)"""
554558
555- def __init__ (self , url , connection , handler , entity_set , entity_key , encode_path = True ):
556- super (EntityDeleteRequest , self ).__init__ (url , connection , handler )
559+ def __init__ (self , url , connection , handler , entity_set , entity_key , encode_path = True , response_hook = None ):
560+ super (EntityDeleteRequest , self ).__init__ (url , connection , handler , response_hook = response_hook )
557561 self ._logger = logging .getLogger (LOGGER_NAME )
558562 self ._entity_set = entity_set
559563 self ._entity_key = entity_key
@@ -585,8 +589,9 @@ class EntityModifyRequest(ODataHttpRequest):
585589 ALLOWED_HTTP_METHODS = ['PATCH' , 'PUT' , 'MERGE' ]
586590
587591 # pylint: disable=too-many-arguments
588- def __init__ (self , url , connection , handler , entity_set , entity_key , method = "PATCH" , encode_path = True ):
589- super (EntityModifyRequest , self ).__init__ (url , connection , handler )
592+ def __init__ (self , url , connection , handler , entity_set , entity_key , method = "PATCH" , encode_path = True ,
593+ response_hook = None ):
594+ super (EntityModifyRequest , self ).__init__ (url , connection , handler , response_hook = response_hook )
590595 self ._logger = logging .getLogger (LOGGER_NAME )
591596 self ._entity_set = entity_set
592597 self ._entity_type = entity_set .entity_type
@@ -650,8 +655,8 @@ class QueryRequest(ODataHttpRequest):
650655
651656 # pylint: disable=too-many-instance-attributes
652657
653- def __init__ (self , url , connection , handler , last_segment ):
654- super (QueryRequest , self ).__init__ (url , connection , handler )
658+ def __init__ (self , url , connection , handler , last_segment , response_hook = None ):
659+ super (QueryRequest , self ).__init__ (url , connection , handler , response_hook = response_hook )
655660
656661 self ._logger = logging .getLogger (LOGGER_NAME )
657662 self ._count = None
@@ -767,8 +772,10 @@ def get_query_params(self):
767772class FunctionRequest (QueryRequest ):
768773 """Function import request (Service call)"""
769774
770- def __init__ (self , url , connection , handler , function_import ):
771- super (FunctionRequest , self ).__init__ (url , connection , handler , function_import .name )
775+ def __init__ (self , url , connection , handler , function_import , response_hook = None ):
776+ super (FunctionRequest , self ).__init__ (
777+ url , connection , handler , function_import .name ,
778+ response_hook = response_hook )
772779
773780 self ._function_import = function_import
774781
@@ -1332,8 +1339,8 @@ def __str__(self):
13321339class GetEntitySetRequest (QueryRequest ):
13331340 """GET on EntitySet"""
13341341
1335- def __init__ (self , url , connection , handler , last_segment , entity_type , encode_path = True ):
1336- super (GetEntitySetRequest , self ).__init__ (url , connection , handler , last_segment )
1342+ def __init__ (self , url , connection , handler , last_segment , entity_type , encode_path = True , response_hook = None ):
1343+ super (GetEntitySetRequest , self ).__init__ (url , connection , handler , last_segment , response_hook = response_hook )
13371344
13381345 self ._entity_type = entity_type
13391346 self ._encode_path = encode_path
@@ -1554,7 +1561,7 @@ def get_entities_handler(response):
15541561 entity_set_name = self ._alias if self ._alias is not None else self ._entity_set .name
15551562 return GetEntitySetRequest (self ._service .url , self ._service .connection , get_entities_handler ,
15561563 self ._parent_last_segment + entity_set_name , self ._entity_set .entity_type ,
1557- encode_path = encode_path )
1564+ encode_path = encode_path , response_hook = self . _service . response_hook )
15581565
15591566 def create_entity (self , return_code = HTTP_CODE_CREATED ):
15601567 """Creates a new entity in the given entity-set."""
@@ -1572,7 +1579,7 @@ def create_entity_handler(response):
15721579 return EntityProxy (self ._service , self ._entity_set , self ._entity_set .entity_type , entity_props , etag = etag )
15731580
15741581 return EntityCreateRequest (self ._service .url , self ._service .connection , create_entity_handler , self ._entity_set ,
1575- self .last_segment )
1582+ self .last_segment , response_hook = self . _service . response_hook )
15761583
15771584 def update_entity (self , key = None , method = None , encode_path = True , ** kwargs ):
15781585 """Updates an existing entity in the given entity-set."""
@@ -1595,7 +1602,8 @@ def update_entity_handler(response):
15951602 method = self ._service .config ['http' ]['update_method' ]
15961603
15971604 return EntityModifyRequest (self ._service .url , self ._service .connection , update_entity_handler , self ._entity_set ,
1598- entity_key , method = method , encode_path = encode_path )
1605+ entity_key , method = method , encode_path = encode_path ,
1606+ response_hook = self ._service .response_hook )
15991607
16001608 def delete_entity (self , key : EntityKey = None , encode_path = True , ** kwargs ):
16011609 """Delete the entity"""
@@ -1614,7 +1622,7 @@ def delete_entity_handler(response):
16141622 entity_key = EntityKey (self ._entity_set .entity_type , key , ** kwargs )
16151623
16161624 return EntityDeleteRequest (self ._service .url , self ._service .connection , delete_entity_handler , self ._entity_set ,
1617- entity_key , encode_path = encode_path )
1625+ entity_key , encode_path = encode_path , response_hook = self . _service . response_hook )
16181626
16191627
16201628# pylint: disable=too-few-public-methods
@@ -1735,17 +1743,19 @@ def function_import_handler(fimport, response):
17351743 return response_data
17361744
17371745 return FunctionRequest (self ._service .url , self ._service .connection ,
1738- partial (function_import_handler , fimport ), fimport )
1746+ partial (function_import_handler , fimport ), fimport ,
1747+ response_hook = self ._service .response_hook )
17391748
17401749
17411750class Service :
17421751 """OData service"""
17431752
1744- def __init__ (self , url , schema , connection , config = None ):
1753+ def __init__ (self , url , schema , connection , config = None , response_hook = None ):
17451754 self ._url = url
17461755 self ._schema = schema
17471756 self ._connection = connection
17481757 self ._retain_null = config .retain_null if config else False
1758+ self ._response_hook = response_hook
17491759 self ._entity_container = EntityContainer (self )
17501760 self ._function_container = FunctionContainer (self )
17511761
@@ -1769,6 +1779,12 @@ def connection(self):
17691779
17701780 return self ._connection
17711781
1782+ @property
1783+ def response_hook (self ):
1784+ """Optional hook called with the raw response before domain handler runs"""
1785+
1786+ return self ._response_hook
1787+
17721788 @property
17731789 def retain_null (self ):
17741790 """Whether to respect null-ed values or to substitute them with type specific default values"""
0 commit comments