Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Operation.deserialize. #7427

Merged
merged 1 commit into from
Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions api_core/google/api_core/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ def metadata(self):
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
Expand Down
8 changes: 8 additions & 0 deletions api_core/tests/unit/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)