def _set_result_from_operation(self): """Set the result or exception from the operation if it is complete.""" # This must be done in a lock to prevent the polling thread # and main thread from both executing the completion logic # at the same time. with self._completion_lock: # If the operation isn't complete or if the result has already been # set, do not call set_result/set_exception again. # Note: self._result_set is set to True in set_result and # set_exception, in case those methods are invoked directly. if not self._operation.done or self._result_set: return if self._operation.HasField("response"): response = protobuf_helpers.from_any_pb( self._result_type, self._operation.response) self.set_result(response) elif self._operation.HasField("error"): exception = exceptions.GoogleAPICallError( self._operation.error.message, errors=(self._operation.error, ), response=self._operation, ) self.set_exception(exception) else: exception = exceptions.GoogleAPICallError( "Unexpected state: Long-running operation had neither " "response nor error set.") self.set_exception(exception)
def metadata(self): """google.protobuf.Message: the current operation metadata.""" if not self._operation.HasField("metadata"): return None return protobuf_helpers.from_any_pb(self._metadata_type, self._operation.metadata)
def test_from_any_pb_success(): in_message = date_pb2.Date(year=1990) in_message_any = any_pb2.Any() in_message_any.Pack(in_message) out_message = protobuf_helpers.from_any_pb(date_pb2.Date, in_message_any) assert in_message == out_message
def _set_result_from_operation(self): """Set the result or exception from the operation if it is complete.""" # This must be done in a lock to prevent the polling thread # and main thread from both executing the completion logic # at the same time. with self._completion_lock: # If the operation isn't complete or if the result has already been # set, do not call set_result/set_exception again. # Note: self._result_set is set to True in set_result and # set_exception, in case those methods are invoked directly. if not self._operation.done or self._result_set: return if self._operation.HasField("response"): response = protobuf_helpers.from_any_pb( self._result_type, self._operation.response) self.set_result(response) elif self._operation.HasField("error"): exception = exceptions.from_grpc_status( status_code=self._operation.error.code, message=self._operation.error.message, errors=(self._operation.error, ), response=self._operation, ) self.set_exception(exception) else: # Some APIs set `done: true`, with an empty response. # Set the result to an empty message of the expected # result type. # https://google.aip.dev/151 self.set_result(self._result_type())
def _set_result_from_operation(self): """Set the result or exception from the operation if it is complete.""" # This must be done in a lock to prevent the polling thread # and main thread from both executing the completion logic # at the same time. with self._completion_lock: # If the operation isn't complete or if the result has already been # set, do not call set_result/set_exception again. # Note: self._result_set is set to True in set_result and # set_exception, in case those methods are invoked directly. if not self._operation.done or self._result_set: return if self._operation.HasField("response"): response = protobuf_helpers.from_any_pb( self._result_type, self._operation.response ) self.set_result(response) elif self._operation.HasField("error"): exception = exceptions.GoogleAPICallError( self._operation.error.message, errors=(self._operation.error,), response=self._operation, ) self.set_exception(exception) else: exception = exceptions.GoogleAPICallError( "Unexpected state: Long-running operation had neither " "response nor error set." ) self.set_exception(exception)
def metadata(self): """google.protobuf.Message: the current operation metadata.""" if not self._operation.HasField('metadata'): return None return protobuf_helpers.from_any_pb( self._metadata_type, self._operation.metadata)
def test_from_any_pb_wrapped_success(): # Declare a message class conforming to wrapped messages. class WrappedDate(object): def __init__(self, **kwargs): self._pb = date_pb2.Date(**kwargs) def __eq__(self, other): return self._pb == other @classmethod def pb(cls, msg): return msg._pb # Run the same test as `test_from_any_pb_success`, but using the # wrapped class. in_message = date_pb2.Date(year=1990) in_message_any = any_pb2.Any() in_message_any.Pack(in_message) out_message = protobuf_helpers.from_any_pb(WrappedDate, in_message_any) assert out_message == in_message
def test_from_any_pb_failure(): in_message = any_pb2.Any() in_message.Pack(date_pb2.Date(year=1990)) with pytest.raises(TypeError): protobuf_helpers.from_any_pb(timeofday_pb2.TimeOfDay, in_message)