Skip to content

Commit

Permalink
Add Speech get_operation method (#2309)
Browse files Browse the repository at this point in the history
  • Loading branch information
blowmage committed Aug 23, 2018
1 parent 80366a0 commit 2e313c9
Show file tree
Hide file tree
Showing 4 changed files with 352 additions and 0 deletions.
42 changes: 42 additions & 0 deletions google-cloud-speech/lib/google/cloud/speech/v1/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,48 @@ def streaming_recognize streaming_config, options: nil
end
)
end

##
# Gets the latest state of a long-running operation. Clients can use
# this method to poll the operation result at intervals as recommended
# by the API service.
#
# @param name [String]
# The name of the operation resource.
# @param options [Google::Gax::CallOptions]
# Overrides the default settings for this call, e.g, timeout,
# retries, etc.
# @return [Google::Gax::Operation]
# @raise [Google::Gax::GaxError] if the RPC is aborted.
# @example
# require "google/cloud/speech"
#
# speech_client = Google::Cloud::Speech.new version: :v1
#
# op = speech_client.get_operation "-"
#
# # Process error operations.
# log_error op.error if op.error?
#
# if op.done?
# # Process completed operations.
# log_finished op.response, op.metadata
# else
# # Process pending operations.
# log_pending op.name, op.metadata
# end
#
def get_operation name, options: nil
proto_op = @operations_client.get_operation name, options: options

Google::Gax::Operation.new(
proto_op,
@operations_client,
Google::Cloud::Speech::V1::LongRunningRecognizeResponse,
Google::Cloud::Speech::V1::LongRunningRecognizeMetadata,
call_options: options
)
end
end
end
end
Expand Down
42 changes: 42 additions & 0 deletions google-cloud-speech/lib/google/cloud/speech/v1p1beta1/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,48 @@ def streaming_recognize streaming_config, options: nil
end
)
end

##
# Gets the latest state of a long-running operation. Clients can use
# this method to poll the operation result at intervals as recommended
# by the API service.
#
# @param name [String]
# The name of the operation resource.
# @param options [Google::Gax::CallOptions]
# Overrides the default settings for this call, e.g, timeout,
# retries, etc.
# @return [Google::Gax::Operation]
# @raise [Google::Gax::GaxError] if the RPC is aborted.
# @example
# require "google/cloud/speech"
#
# speech_client = Google::Cloud::Speech.new version: :v1p1beta1
#
# op = speech_client.get_operation "-"
#
# # Process error operations.
# log_error op.error if op.error?
#
# if op.done?
# # Process completed operations.
# log_finished op.response, op.metadata
# else
# # Process pending operations.
# log_pending op.name, op.metadata
# end
#
def get_operation name, options: nil
proto_op = @operations_client.get_operation name, options: options

Google::Gax::Operation.new(
proto_op,
@operations_client,
Google::Cloud::Speech::V1p1beta1::LongRunningRecognizeResponse,
Google::Cloud::Speech::V1p1beta1::LongRunningRecognizeMetadata,
call_options: options
)
end
end
end
end
Expand Down
134 changes: 134 additions & 0 deletions google-cloud-speech/test/google/cloud/speech/v1/get_operation_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative "./speech_client_test"
require "google/longrunning/operations_services_pb"

describe Google::Cloud::Speech::V1::SpeechClient, :get_operation do
let(:custom_error) { CustomTestError_v1.new "Custom test error for Google::Cloud::Speech::V1::SpeechClient#get_operation." }

it "invokes get_operation without error" do
# Create request parameters
name = "operation123"

# Create expected grpc response
expected_response = {}
expected_response = Google::Gax::to_proto(expected_response, Google::Cloud::Speech::V1::LongRunningRecognizeResponse)
result = Google::Protobuf::Any.new
result.pack(expected_response)
operation = Google::Longrunning::Operation.new(
name: "operations/get_operation_test",
done: true,
response: result
)

# Mock Grpc layer
mock_method = proc do |request|
assert_instance_of(Google::Longrunning::GetOperationRequest, request)
assert_equal(name, request.name)
OpenStruct.new(execute: operation)
end
mock_stub = MockGrpcClientStub_v1.new(:get_operation, mock_method)

# Mock auth layer
mock_credentials = MockSpeechCredentials_v1.new("get_operation")

Google::Cloud::Speech::V1::Speech::Stub.stub(:new, mock_stub) do
Google::Longrunning::Operations::Stub.stub(:new, mock_stub) do
Google::Cloud::Speech::V1::Credentials.stub(:default, mock_credentials) do
client = Google::Cloud::Speech.new(version: :v1)

# Call method
response = client.get_operation(name)

# Verify the response
assert_equal(expected_response, response.response)
end
end
end
end

it "invokes get_operation and returns an operation error." do
# Create request parameters
name = "operation123"

# Create expected grpc response
operation_error = Google::Rpc::Status.new(
message: "Operation error for Google::Cloud::Speech::V1::SpeechClient#get_operation."
)
operation = Google::Longrunning::Operation.new(
name: "operations/get_operation_test",
done: true,
error: operation_error
)

# Mock Grpc layer
mock_method = proc do |request|
assert_instance_of(Google::Longrunning::GetOperationRequest, request)
assert_equal(name, request.name)
OpenStruct.new(execute: operation)
end
mock_stub = MockGrpcClientStub_v1.new(:get_operation, mock_method)

# Mock auth layer
mock_credentials = MockSpeechCredentials_v1.new("get_operation")

Google::Cloud::Speech::V1::Speech::Stub.stub(:new, mock_stub) do
Google::Longrunning::Operations::Stub.stub(:new, mock_stub) do
Google::Cloud::Speech::V1::Credentials.stub(:default, mock_credentials) do
client = Google::Cloud::Speech.new(version: :v1)

# Call method
response = client.get_operation(name)

# Verify the response
assert(response.error?)
assert_equal(operation_error, response.error)
end
end
end
end

it "invokes get_operation with error" do
# Create request parameters
name = "operation123"

# Mock Grpc layer
mock_method = proc do |request|
assert_instance_of(Google::Longrunning::GetOperationRequest, request)
assert_equal(name, request.name)
raise custom_error
end
mock_stub = MockGrpcClientStub_v1.new(:get_operation, mock_method)

# Mock auth layer
mock_credentials = MockSpeechCredentials_v1.new("get_operation")

Google::Cloud::Speech::V1::Speech::Stub.stub(:new, mock_stub) do
Google::Longrunning::Operations::Stub.stub(:new, mock_stub) do
Google::Cloud::Speech::V1::Credentials.stub(:default, mock_credentials) do
client = Google::Cloud::Speech.new(version: :v1)

# Call method
err = assert_raises Google::Gax::GaxError do
response = client.get_operation(name)
end

# Verify the GaxError wrapped the custom error that was raised.
assert_match(custom_error.message, err.message)
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative "./speech_client_test"
require "google/longrunning/operations_services_pb"

describe Google::Cloud::Speech::V1p1beta1::SpeechClient, :get_operation do
let(:custom_error) { CustomTestError_v1p1beta1.new "Custom test error for Google::Cloud::Speech::V1p1beta1::SpeechClient#get_operation." }

it "invokes get_operation without error" do
# Create request parameters
name = "operation123"

# Create expected grpc response
expected_response = {}
expected_response = Google::Gax::to_proto(expected_response, Google::Cloud::Speech::V1p1beta1::LongRunningRecognizeResponse)
result = Google::Protobuf::Any.new
result.pack(expected_response)
operation = Google::Longrunning::Operation.new(
name: "operations/get_operation_test",
done: true,
response: result
)

# Mock Grpc layer
mock_method = proc do |request|
assert_instance_of(Google::Longrunning::GetOperationRequest, request)
assert_equal(name, request.name)
OpenStruct.new(execute: operation)
end
mock_stub = MockGrpcClientStub_v1p1beta1.new(:get_operation, mock_method)

# Mock auth layer
mock_credentials = MockSpeechCredentials_v1p1beta1.new("get_operation")

Google::Cloud::Speech::V1p1beta1::Speech::Stub.stub(:new, mock_stub) do
Google::Longrunning::Operations::Stub.stub(:new, mock_stub) do
Google::Cloud::Speech::V1p1beta1::Credentials.stub(:default, mock_credentials) do
client = Google::Cloud::Speech.new(version: :v1p1beta1)

# Call method
response = client.get_operation(name)

# Verify the response
assert_equal(expected_response, response.response)
end
end
end
end

it "invokes get_operation and returns an operation error." do
# Create request parameters
name = "operation123"

# Create expected grpc response
operation_error = Google::Rpc::Status.new(
message: "Operation error for Google::Cloud::Speech::V1p1beta1::SpeechClient#get_operation."
)
operation = Google::Longrunning::Operation.new(
name: "operations/get_operation_test",
done: true,
error: operation_error
)

# Mock Grpc layer
mock_method = proc do |request|
assert_instance_of(Google::Longrunning::GetOperationRequest, request)
assert_equal(name, request.name)
OpenStruct.new(execute: operation)
end
mock_stub = MockGrpcClientStub_v1p1beta1.new(:get_operation, mock_method)

# Mock auth layer
mock_credentials = MockSpeechCredentials_v1p1beta1.new("get_operation")

Google::Cloud::Speech::V1p1beta1::Speech::Stub.stub(:new, mock_stub) do
Google::Longrunning::Operations::Stub.stub(:new, mock_stub) do
Google::Cloud::Speech::V1p1beta1::Credentials.stub(:default, mock_credentials) do
client = Google::Cloud::Speech.new(version: :v1p1beta1)

# Call method
response = client.get_operation(name)

# Verify the response
assert(response.error?)
assert_equal(operation_error, response.error)
end
end
end
end

it "invokes get_operation with error" do
# Create request parameters
name = "operation123"

# Mock Grpc layer
mock_method = proc do |request|
assert_instance_of(Google::Longrunning::GetOperationRequest, request)
assert_equal(name, request.name)
raise custom_error
end
mock_stub = MockGrpcClientStub_v1p1beta1.new(:get_operation, mock_method)

# Mock auth layer
mock_credentials = MockSpeechCredentials_v1p1beta1.new("get_operation")

Google::Cloud::Speech::V1p1beta1::Speech::Stub.stub(:new, mock_stub) do
Google::Longrunning::Operations::Stub.stub(:new, mock_stub) do
Google::Cloud::Speech::V1p1beta1::Credentials.stub(:default, mock_credentials) do
client = Google::Cloud::Speech.new(version: :v1p1beta1)

# Call method
err = assert_raises Google::Gax::GaxError do
response = client.get_operation(name)
end

# Verify the GaxError wrapped the custom error that was raised.
assert_match(custom_error.message, err.message)
end
end
end
end
end

0 comments on commit 2e313c9

Please sign in to comment.