Skip to content

Commit d9c3be1

Browse files
lukesneeringercrwilcox
authored andcommitted
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.
1 parent 355a4db commit d9c3be1

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

google/api_core/operation.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ def metadata(self):
101101
self._metadata_type, self._operation.metadata
102102
)
103103

104+
@classmethod
105+
def deserialize(self, payload):
106+
"""Deserialize a ``google.longrunning.Operation`` protocol buffer.
107+
108+
Args:
109+
payload (bytes): A serialized operation protocol buffer.
110+
111+
Returns:
112+
~.operations_pb2.Operation: An Operation protobuf object.
113+
"""
114+
return operations_pb2.Operation.FromString(payload)
115+
104116
def _set_result_from_operation(self):
105117
"""Set the result or exception from the operation if it is complete."""
106118
# This must be done in a lock to prevent the polling thread

tests/unit/test_operation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,11 @@ def test_from_gapic():
231231
assert future._metadata_type == struct_pb2.Struct
232232
assert future.operation.name == TEST_OPERATION_NAME
233233
assert future.done
234+
235+
236+
def test_deserialize():
237+
op = make_operation_proto(name="foobarbaz")
238+
serialized = op.SerializeToString()
239+
deserialized_op = operation.Operation.deserialize(serialized)
240+
assert op.name == deserialized_op.name
241+
assert type(op) is type(deserialized_op)

0 commit comments

Comments
 (0)