109 True if the collection was modified. |
109 True if the collection was modified. |
110 """ |
110 """ |
111 unique_key = (key, inspect.getmodule(function)) |
111 unique_key = (key, inspect.getmodule(function)) |
112 if unique_key in self.__unique_keys: |
112 if unique_key in self.__unique_keys: |
113 return False |
113 return False |
114 self.__content.insert(index, (key, function, service)) |
114 num_args = len(inspect.getargspec(function)[0]) |
|
115 if (inspect.ismethod(function)): |
|
116 num_args -= 1 |
|
117 self.__content.insert(index, (key, function, service, num_args)) |
115 self.__unique_keys.add(unique_key) |
118 self.__unique_keys.add(unique_key) |
116 return True |
119 return True |
117 |
120 |
118 def Append(self, key, function, service=None): |
121 def Append(self, key, function, service=None): |
119 """Appends a hook at the end of the list. |
122 """Appends a hook at the end of the list. |
148 def Clear(self): |
151 def Clear(self): |
149 """Removes all hooks from the list (useful for unit tests).""" |
152 """Removes all hooks from the list (useful for unit tests).""" |
150 self.__content = [] |
153 self.__content = [] |
151 self.__unique_keys = set() |
154 self.__unique_keys = set() |
152 |
155 |
153 def Call(self, service, call, request, response): |
156 def Call(self, service, call, request, response, rpc=None): |
154 """Invokes all hooks in this collection. |
157 """Invokes all hooks in this collection. |
155 |
158 |
156 Args: |
159 Args: |
157 service: string representing which service to call |
160 service: string representing which service to call |
158 call: string representing which function to call |
161 call: string representing which function to call |
159 request: protocol buffer for the request |
162 request: protocol buffer for the request |
160 response: protocol buffer for the response |
163 response: protocol buffer for the response |
161 """ |
164 rpc: optional RPC used to make this call |
162 for key, function, srv in self.__content: |
165 """ |
|
166 for key, function, srv, num_args in self.__content: |
163 if srv is None or srv == service: |
167 if srv is None or srv == service: |
164 function(service, call, request, response) |
168 if num_args == 5: |
|
169 function(service, call, request, response, rpc) |
|
170 else: |
|
171 function(service, call, request, response) |
165 |
172 |
166 |
173 |
167 class APIProxyStubMap(object): |
174 class APIProxyStubMap(object): |
168 """Container of APIProxy stubs for more convenient unittesting. |
175 """Container of APIProxy stubs for more convenient unittesting. |
169 |
176 |
238 Raises: |
245 Raises: |
239 apiproxy_errors.Error or a subclass. |
246 apiproxy_errors.Error or a subclass. |
240 """ |
247 """ |
241 stub = self.GetStub(service) |
248 stub = self.GetStub(service) |
242 assert stub, 'No api proxy found for service "%s"' % service |
249 assert stub, 'No api proxy found for service "%s"' % service |
243 self.__precall_hooks.Call(service, call, request, response) |
250 if hasattr(stub, 'CreateRPC'): |
244 stub.MakeSyncCall(service, call, request, response) |
251 rpc = stub.CreateRPC() |
245 self.__postcall_hooks.Call(service, call, request, response) |
252 self.__precall_hooks.Call(service, call, request, response, rpc) |
|
253 rpc.MakeCall(service, call, request, response) |
|
254 rpc.Wait() |
|
255 rpc.CheckSuccess() |
|
256 self.__postcall_hooks.Call(service, call, request, response, rpc) |
|
257 else: |
|
258 self.__precall_hooks.Call(service, call, request, response) |
|
259 stub.MakeSyncCall(service, call, request, response) |
|
260 self.__postcall_hooks.Call(service, call, request, response) |
246 |
261 |
247 |
262 |
248 class UserRPC(object): |
263 class UserRPC(object): |
249 """Wrapper class for asynchronous RPC. |
264 """Wrapper class for asynchronous RPC. |
250 |
265 |
383 """ |
398 """ |
384 assert self.__rpc.state == apiproxy_rpc.RPC.IDLE, repr(self.state) |
399 assert self.__rpc.state == apiproxy_rpc.RPC.IDLE, repr(self.state) |
385 self.__method = method |
400 self.__method = method |
386 self.__get_result_hook = get_result_hook |
401 self.__get_result_hook = get_result_hook |
387 self.__user_data = user_data |
402 self.__user_data = user_data |
388 apiproxy.GetPreCallHooks().Call(self.__service, method, request, response) |
403 apiproxy.GetPreCallHooks().Call( |
|
404 self.__service, method, request, response, self.__rpc) |
389 self.__rpc.MakeCall(self.__service, method, request, response) |
405 self.__rpc.MakeCall(self.__service, method, request, response) |
390 |
406 |
391 def wait(self): |
407 def wait(self): |
392 """Wait for the call to complete, and call callbacks. |
408 """Wait for the call to complete, and call callbacks. |
393 |
409 |
422 self.wait() |
438 self.wait() |
423 self.__rpc.CheckSuccess() |
439 self.__rpc.CheckSuccess() |
424 if not self.__postcall_hooks_called: |
440 if not self.__postcall_hooks_called: |
425 self.__postcall_hooks_called = True |
441 self.__postcall_hooks_called = True |
426 apiproxy.GetPostCallHooks().Call(self.__service, self.__method, |
442 apiproxy.GetPostCallHooks().Call(self.__service, self.__method, |
427 self.request, self.response) |
443 self.request, self.response, self.__rpc) |
428 |
444 |
429 def get_result(self): |
445 def get_result(self): |
430 """Get the result of the RPC, or possibly raise an exception. |
446 """Get the result of the RPC, or possibly raise an exception. |
431 |
447 |
432 This implies a call to check_success(). If a get-result hook was |
448 This implies a call to check_success(). If a get-result hook was |