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

fix(speech): enable location specific connections #13757

Merged
merged 4 commits into from
Mar 11, 2024
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
1 change: 1 addition & 0 deletions generator/generator_config.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -3149,6 +3149,7 @@ service {
product_path: "google/cloud/speech/v2"
initial_copyright_year: "2022"
retryable_status_codes: ["kUnavailable"]
endpoint_location_style: LOCATION_DEPENDENT_COMPAT
}

# Sql
Expand Down
4 changes: 4 additions & 0 deletions google/cloud/speech/doc/environment-variables.dox
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ environment variables are convenient when troubleshooting problems.
`EndpointOption` (which defaults to "speech.googleapis.com")
used by `MakeAdaptationConnection()`.

- `GOOGLE_CLOUD_CPP_SPEECH_ENDPOINT=...` overrides the
`EndpointOption` (which defaults to "<location>-speech.googleapis.com")
used by `MakeSpeechConnection()`.

- `GOOGLE_CLOUD_CPP_SPEECH_ENDPOINT=...` overrides the
`EndpointOption` (which defaults to "speech.googleapis.com")
used by `MakeSpeechConnection()`.
Comment on lines +17 to 23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should both of these exist?

Expand Down
7 changes: 5 additions & 2 deletions google/cloud/speech/v2/internal/speech_option_defaults.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "google/cloud/speech/v2/internal/speech_option_defaults.h"
#include "google/cloud/speech/v2/speech_connection.h"
#include "google/cloud/speech/v2/speech_options.h"
#include "google/cloud/internal/absl_str_cat_quiet.h"
#include "google/cloud/internal/populate_common_options.h"
#include "google/cloud/internal/populate_grpc_options.h"
#include <memory>
Expand All @@ -33,10 +34,12 @@ namespace {
auto constexpr kBackoffScaling = 2.0;
} // namespace

Options SpeechDefaultOptions(Options options) {
Options SpeechDefaultOptions(std::string const& location, Options options) {
options = internal::PopulateCommonOptions(
std::move(options), "GOOGLE_CLOUD_CPP_SPEECH_ENDPOINT", "",
"GOOGLE_CLOUD_CPP_SPEECH_AUTHORITY", "speech.googleapis.com");
"GOOGLE_CLOUD_CPP_SPEECH_AUTHORITY",
absl::StrCat(location, location.empty() ? "" : "-",
"speech.googleapis.com"));
options = internal::PopulateGrpcOptions(std::move(options));
if (!options.has<speech_v2::SpeechRetryPolicyOption>()) {
options.set<speech_v2::SpeechRetryPolicyOption>(
Expand Down
3 changes: 2 additions & 1 deletion google/cloud/speech/v2/internal/speech_option_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@

#include "google/cloud/options.h"
#include "google/cloud/version.h"
#include <string>

namespace google {
namespace cloud {
namespace speech_v2_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

Options SpeechDefaultOptions(Options options);
Options SpeechDefaultOptions(std::string const& location, Options options);

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace speech_v2_internal
Expand Down
10 changes: 8 additions & 2 deletions google/cloud/speech/v2/speech_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,13 @@ SpeechConnection::UndeletePhraseSet(
Status(StatusCode::kUnimplemented, "not implemented"));
}

std::shared_ptr<SpeechConnection> MakeSpeechConnection(Options options) {
std::shared_ptr<SpeechConnection> MakeSpeechConnection(
std::string const& location, Options options) {
internal::CheckExpectedOptions<CommonOptionList, GrpcOptionList,
UnifiedCredentialsOptionList,
SpeechPolicyOptionList>(options, __func__);
options = speech_v2_internal::SpeechDefaultOptions(std::move(options));
options =
speech_v2_internal::SpeechDefaultOptions(location, std::move(options));
auto background = internal::MakeBackgroundThreadsFactory(options)();
auto auth = internal::CreateAuthenticationStrategy(background->cq(), options);
auto stub =
Expand All @@ -223,6 +225,10 @@ std::shared_ptr<SpeechConnection> MakeSpeechConnection(Options options) {
std::move(background), std::move(stub), std::move(options)));
}

std::shared_ptr<SpeechConnection> MakeSpeechConnection(Options options) {
return MakeSpeechConnection(std::string{}, std::move(options));
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace speech_v2
} // namespace cloud
Expand Down
12 changes: 12 additions & 0 deletions google/cloud/speech/v2/speech_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <google/cloud/speech/v2/cloud_speech.pb.h>
#include <google/longrunning/operations.grpc.pb.h>
#include <memory>
#include <string>

namespace google {
namespace cloud {
Expand Down Expand Up @@ -285,9 +286,20 @@ class SpeechConnection {
* @note Unexpected options will be ignored. To log unexpected options instead,
* set `GOOGLE_CLOUD_CPP_ENABLE_CLOG=yes` in the environment.
*
* @param location Sets the prefix for the default `EndpointOption` value.
* @param options (optional) Configure the `SpeechConnection` created by
* this function.
*/
std::shared_ptr<SpeechConnection> MakeSpeechConnection(
std::string const& location, Options options = {});

/**
* A backwards-compatible version of the previous factory function. Unless
* the service also offers a global endpoint, the default value of the
* `EndpointOption` may be useless, in which case it must be overridden.
*
* @deprecated Please use the `location` overload instead.
*/
std::shared_ptr<SpeechConnection> MakeSpeechConnection(Options options = {});

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Expand Down