def change_parser(parser): """ A context manager that allows overriding the function that will be used to parse the response. Usage is with change_parser(new_parser): result = service.resource().method() It works by temporarily substituting the executor with one that replaces the provided parser function with the one the context manager received. """ prev = base.current_executor() try: base.use_executor(lambda request, _: prev(request, parser)) yield finally: base.use_executor(prev)
def extract_request(): """ A context manager that helps extracting the Request object from a function decorated with the apimethod decorator. Usage is: with extract_request(): request = self.decorated_method() It works by temporarily substituting the executor with a dummy one that just returns the request unchanged. """ prev = base.current_executor() try: base.use_executor(lambda request, _: request) yield finally: base.use_executor(prev)