Add `Operation.deserialize`. (#7427)
This commit adds a `deserialize` method to the Operation object.
The class method is a helper to deserialize the serialized protobuf
operation messages.
diff --git a/google/api_core/operation.py b/google/api_core/operation.py
index 4147c7b..87f42a9 100644
--- a/google/api_core/operation.py
+++ b/google/api_core/operation.py
@@ -101,6 +101,18 @@
self._metadata_type, self._operation.metadata
)
+ @classmethod
+ def deserialize(self, payload):
+ """Deserialize a ``google.longrunning.Operation`` protocol buffer.
+
+ Args:
+ payload (bytes): A serialized operation protocol buffer.
+
+ Returns:
+ ~.operations_pb2.Operation: An Operation protobuf object.
+ """
+ return operations_pb2.Operation.FromString(payload)
+
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
diff --git a/tests/unit/test_operation.py b/tests/unit/test_operation.py
index ceaec82..a5346a7 100644
--- a/tests/unit/test_operation.py
+++ b/tests/unit/test_operation.py
@@ -231,3 +231,11 @@
assert future._metadata_type == struct_pb2.Struct
assert future.operation.name == TEST_OPERATION_NAME
assert future.done
+
+
+def test_deserialize():
+ op = make_operation_proto(name="foobarbaz")
+ serialized = op.SerializeToString()
+ deserialized_op = operation.Operation.deserialize(serialized)
+ assert op.name == deserialized_op.name
+ assert type(op) is type(deserialized_op)