aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Sneeringer <luke@sneeringer.com>2019-02-23 15:39:50 -0800
committerChristopher Wilcox <crwilcox@google.com>2019-02-23 15:39:50 -0800
commitd9c3be106967426c1214bba3e28a0c1a7e970a67 (patch)
tree2d7b2018f6ccd638a7689b139eeb9ff0c16681ef
parent355a4db4a597534a94deb1ff824b98eb901d0e68 (diff)
downloadpython-api-core-d9c3be106967426c1214bba3e28a0c1a7e970a67.tar.gz
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.
-rw-r--r--google/api_core/operation.py12
-rw-r--r--tests/unit/test_operation.py8
2 files changed, 20 insertions, 0 deletions
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 @@ class Operation(polling.PollingFuture):
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 @@ def test_from_gapic():
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)