Skip to content

Commit

Permalink
feat(language): add v2 service (#12376)
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 555681517
  • Loading branch information
devbww authored Aug 16, 2023
1 parent 4bbf136 commit 769450e
Show file tree
Hide file tree
Showing 43 changed files with 3,084 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ the APIs in these libraries are stable, and are ready for production use.
The Cloud IoT Core service has been [shutdown][cloud-iot-shutdown]. We removed
the corresponding C++ client library as it is no longer usable.

### [Natural Language AI](/google/cloud/language/README.md)

The library has been expanded to include the v2 service.

### Other Changes

**Bazel Testing**: from v2.15.0 we will only test with Bazel >= 6.0.0. We do not
Expand Down
Binary file modified ci/abi-dumps/google_cloud_cpp_language.expected.abi.dump.gz
Binary file not shown.
3 changes: 3 additions & 0 deletions ci/etc/expected_install_directories
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,9 @@
./include/google/cloud/language/v1
./include/google/cloud/language/v1/internal
./include/google/cloud/language/v1/mocks
./include/google/cloud/language/v2
./include/google/cloud/language/v2/internal
./include/google/cloud/language/v2/mocks
./include/google/cloud/logging
./include/google/cloud/logging/mocks
./include/google/cloud/logging/v2
Expand Down
1 change: 1 addition & 0 deletions external/googleapis/protolists/language.list
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
@com_google_googleapis//google/cloud/language/v1:language_service.proto
@com_google_googleapis//google/cloud/language/v2:language_service.proto
6 changes: 5 additions & 1 deletion external/googleapis/update_libraries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ declare -A -r LIBRARIES=(
"@com_google_googleapis//google/cloud/kms/v1:kms_cc_grpc" \
"@com_google_googleapis//google/cloud/kms/inventory/v1:inventory_cc_grpc"
)"
["language"]="@com_google_googleapis//google/cloud/language/v1:language_cc_grpc"
["language"]="$(
printf ",%s" \
"@com_google_googleapis//google/cloud/language/v1:language_cc_grpc" \
"@com_google_googleapis//google/cloud/language/v2:language_cc_grpc"
)"
["logging_type"]="@com_google_googleapis//google/logging/type:type_cc_grpc"
["logging"]="@com_google_googleapis//google/logging/v2:logging_cc_grpc"
["managedidentities"]="@com_google_googleapis//google/cloud/managedidentities/v1:managedidentities_cc_grpc"
Expand Down
7 changes: 7 additions & 0 deletions generator/generator_config.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -2302,6 +2302,13 @@ service {
retryable_status_codes: ["kUnavailable"]
}
service {
service_proto_path: "google/cloud/language/v2/language_service.proto"
product_path: "google/cloud/language/v2"
initial_copyright_year: "2023"
retryable_status_codes: ["kUnavailable"]
}
# Logging
service {
service_proto_path: "google/logging/v2/logging.proto"
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/language/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ licenses(["notice"]) # Apache 2.0
service_dirs = [
"",
"v1/",
"v2/",
]

src_dirs = service_dirs + [d + "internal/" for d in service_dirs]
Expand Down Expand Up @@ -47,6 +48,7 @@ cc_library(
"//:common",
"//:grpc_utils",
"@com_google_googleapis//google/cloud/language/v1:language_cc_grpc",
"@com_google_googleapis//google/cloud/language/v2:language_cc_grpc",
],
)

Expand Down
5 changes: 3 additions & 2 deletions google/cloud/language/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ set(DOXYGEN_EXAMPLE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/quickstart")

unset(mocks_globs)
unset(source_globs)
set(service_dirs "" "v1/")
set(service_dirs "" "v1/" "v2/")
foreach (dir IN LISTS service_dirs)
string(REPLACE "/" "_" ns "${dir}")
list(APPEND source_globs "${dir}*.h" "${dir}*.cc" "${dir}internal/*")
Expand Down Expand Up @@ -112,7 +112,8 @@ target_include_directories(
target_compile_options(google_cloud_cpp_language_mocks
INTERFACE ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG})

if (BUILD_TESTING AND GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
include(CTest)
if (BUILD_TESTING)
add_executable(language_quickstart "quickstart/quickstart.cc")
target_link_libraries(language_quickstart
PRIVATE google-cloud-cpp::language)
Expand Down
15 changes: 6 additions & 9 deletions google/cloud/language/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ to get started using this client library in a larger project. The following
"Hello World" program is used in this quickstart, and should give you a taste of
this library.

For detailed instructions on how to build and install this library, see the
top-level [README](/README.md#building-and-installing).

<!-- inject-quickstart-start -->

```cc
#include "google/cloud/language/v1/language_client.h"
#include "google/cloud/language/v2/language_client.h"
#include <iostream>

auto constexpr kText = R"""(
Expand All @@ -35,20 +32,20 @@ int main(int argc, char* argv[]) try {
return 1;
}

namespace language = ::google::cloud::language_v1;
namespace language = ::google::cloud::language_v2;
auto client = language::LanguageServiceClient(
language::MakeLanguageServiceConnection());

google::cloud::language::v1::Document document;
document.set_type(google::cloud::language::v1::Document::PLAIN_TEXT);
google::cloud::language::v2::Document document;
document.set_type(google::cloud::language::v2::Document::PLAIN_TEXT);
document.set_content(kText);
document.set_language("en-US");
document.set_language_code("en-US");

auto response = client.AnalyzeEntities(document);
if (!response) throw std::move(response).status();

for (auto const& entity : response->entities()) {
if (entity.type() != google::cloud::language::v1::Entity::NUMBER) continue;
if (entity.type() != google::cloud::language::v2::Entity::NUMBER) continue;
std::cout << entity.DebugString() << "\n";
}

Expand Down
22 changes: 15 additions & 7 deletions google/cloud/language/doc/main.dox
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ a service that provides natural language understanding technologies, such as
sentiment analysis, entity recognition, entity sentiment analysis, and other
text annotations.

While this library is **GA**, please note Google Cloud C++ client libraries do **not** follow [Semantic Versioning](https://semver.org/).
While this library is **GA**, please note that the Google Cloud C++ client libraries do **not** follow
[Semantic Versioning](https://semver.org/).

@tableofcontents{HTML:2}

Expand All @@ -22,12 +23,18 @@ which should give you a taste of the Cloud Natural Language API C++ client libra
## Main classes

<!-- inject-client-list-start -->
The main class in this library is
[`language_v1::LanguageServiceClient`](@ref google::cloud::language_v1::LanguageServiceClient). All RPCs are exposed
as member functions of this class. Other classes provide helpers, configuration
parameters, and infrastructure to mock
[`language_v1::LanguageServiceClient`](@ref google::cloud::language_v1::LanguageServiceClient) when testing your
This library offers multiple `*Client` classes, which are listed below. Each one
of these classes exposes all the RPCs for a service as member functions of the
class. This library groups multiple services because they are part of the same
product or are often used together. A typical example may be the administrative
and data plane operations for a single product.

The library also has other classes that provide helpers, configuration
parameters, and infrastructure to mock the `*Client` classes when testing your
application.

- [\c language_v1::LanguageServiceClient](@ref google::cloud::language_v1::LanguageServiceClient)
- [\c language_v2::LanguageServiceClient](@ref google::cloud::language_v2::LanguageServiceClient)
<!-- inject-client-list-end -->

## More Information
Expand All @@ -39,7 +46,8 @@ application.
authentication credentials used by the library.
- @ref language-override-retry - describes how to change the default retry
policies.
- @ref language-env - describes environment variables that can configure the behavior of the library.
- @ref language-env - describes environment variables that can configure the
behavior of the library.

[cloud-service-docs]: https://cloud.google.com/natural-language

Expand Down
11 changes: 11 additions & 0 deletions google/cloud/language/doc/override-authentication.dox
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ The following example shows how to explicitly load a service account key file:
<!-- inject-service-account-snippet-start -->
@snippet language_client_samples.cc with-service-account

Follow these links to find examples for other \c *Client classes:

- [\c language_v1::LanguageServiceClient](@ref language_v1::LanguageServiceClient-service-account-snippet)
- [\c language_v2::LanguageServiceClient](@ref language_v2::LanguageServiceClient-service-account-snippet)

<!-- inject-service-account-snippet-end -->

Keep in mind that we chose this as an example because it is relatively easy to
Expand All @@ -31,5 +36,11 @@ guide for more details.

@snippet google/cloud/language/v1/samples/language_client_samples.cc with-service-account

*/

/*! @page language_v2::LanguageServiceClient-service-account-snippet Override language_v2::LanguageServiceClient Authentication Defaults

@snippet google/cloud/language/v2/samples/language_client_samples.cc with-service-account

*/
// <!-- inject-authentication-pages-end -->
11 changes: 11 additions & 0 deletions google/cloud/language/doc/override-endpoint.dox
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ For example, this will override the default endpoint for `language_v1::LanguageS

@snippet language_client_samples.cc set-client-endpoint

Follow these links to find examples for other \c *Client classes:

- [\c language_v1::LanguageServiceClient](@ref language_v1::LanguageServiceClient-endpoint-snippet)
- [\c language_v2::LanguageServiceClient](@ref language_v2::LanguageServiceClient-endpoint-snippet)

<!-- inject-endpoint-snippet-end -->

*/
Expand All @@ -21,5 +26,11 @@ For example, this will override the default endpoint for `language_v1::LanguageS

@snippet google/cloud/language/v1/samples/language_client_samples.cc set-client-endpoint

*/

/*! @page language_v2::LanguageServiceClient-endpoint-snippet Override language_v2::LanguageServiceClient Endpoint Configuration

@snippet google/cloud/language/v2/samples/language_client_samples.cc set-client-endpoint

*/
// <!-- inject-endpoint-pages-end -->
18 changes: 17 additions & 1 deletion google/cloud/language/doc/override-retry-policies.dox
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*!

@page language-override-retry Override Retry, Backoff, and Idempotency Policies

When it is safe to do so, the library automatically retries requests that fail
Expand Down Expand Up @@ -77,6 +76,11 @@ This assumes you have created a custom idempotency policy. Such as:
@snippet language_client_samples.cc custom-idempotency-policy


Follow these links to find examples for other \c *Client classes:

- [\c language_v1::LanguageServiceClient](@ref language_v1::LanguageServiceClient-retry-snippet)
- [\c language_v2::LanguageServiceClient](@ref language_v2::LanguageServiceClient-retry-snippet)

<!-- inject-retry-snippet-end -->

@section language-override-retry-more-information More Information
Expand All @@ -101,5 +105,17 @@ Assuming you have created a custom idempotency policy. Such as:

@snippet google/cloud/language/v1/samples/language_client_samples.cc custom-idempotency-policy

*/

/*! @page language_v2::LanguageServiceClient-retry-snippet Override language_v2::LanguageServiceClient Retry Policies

This shows how to override the retry policies for language_v2::LanguageServiceClient:

@snippet google/cloud/language/v2/samples/language_client_samples.cc set-retry-policy

Assuming you have created a custom idempotency policy. Such as:

@snippet google/cloud/language/v2/samples/language_client_samples.cc custom-idempotency-policy

*/
// <!-- inject-retry-pages-end -->
12 changes: 6 additions & 6 deletions google/cloud/language/quickstart/quickstart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

//! [all]
#include "google/cloud/language/v1/language_client.h"
#include "google/cloud/language/v2/language_client.h"
#include <iostream>

auto constexpr kText = R"""(
Expand All @@ -27,20 +27,20 @@ int main(int argc, char* argv[]) try {
return 1;
}

namespace language = ::google::cloud::language_v1;
namespace language = ::google::cloud::language_v2;
auto client = language::LanguageServiceClient(
language::MakeLanguageServiceConnection());

google::cloud::language::v1::Document document;
document.set_type(google::cloud::language::v1::Document::PLAIN_TEXT);
google::cloud::language::v2::Document document;
document.set_type(google::cloud::language::v2::Document::PLAIN_TEXT);
document.set_content(kText);
document.set_language("en-US");
document.set_language_code("en-US");

auto response = client.AnalyzeEntities(document);
if (!response) throw std::move(response).status();

for (auto const& entity : response->entities()) {
if (entity.type() != google::cloud::language::v1::Entity::NUMBER) continue;
if (entity.type() != google::cloud::language::v2::Entity::NUMBER) continue;
std::cout << entity.DebugString() << "\n";
}

Expand Down
14 changes: 14 additions & 0 deletions google/cloud/language/v2/.repo-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"api_id": "language.googleapis.com",
"api_shortname": "language",
"client_documentation": "https://cloud.google.com/cpp/docs/reference/language/latest",
"distribution_name": "google-cloud-cpp",
"issue_tracker": "https://issuetracker.google.com/issues?q=componentid:187079%20status=open",
"language": "cpp",
"library_type": "GAPIC_AUTO",
"name_pretty": "Cloud Natural Language API",
"product_documentation": "https://cloud.google.com/natural-language/docs",
"release_level": "stable",
"repo": "googleapis/google-cloud-cpp",
"requires_billing": true
}
81 changes: 81 additions & 0 deletions google/cloud/language/v2/internal/language_auth_decorator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2023 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.

// Generated by the Codegen C++ plugin.
// If you make any local changes, they will be lost.
// source: google/cloud/language/v2/language_service.proto

#include "google/cloud/language/v2/internal/language_auth_decorator.h"
#include <google/cloud/language/v2/language_service.grpc.pb.h>
#include <memory>

namespace google {
namespace cloud {
namespace language_v2_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

LanguageServiceAuth::LanguageServiceAuth(
std::shared_ptr<google::cloud::internal::GrpcAuthenticationStrategy> auth,
std::shared_ptr<LanguageServiceStub> child)
: auth_(std::move(auth)), child_(std::move(child)) {}

StatusOr<google::cloud::language::v2::AnalyzeSentimentResponse>
LanguageServiceAuth::AnalyzeSentiment(
grpc::ClientContext& context,
google::cloud::language::v2::AnalyzeSentimentRequest const& request) {
auto status = auth_->ConfigureContext(context);
if (!status.ok()) return status;
return child_->AnalyzeSentiment(context, request);
}

StatusOr<google::cloud::language::v2::AnalyzeEntitiesResponse>
LanguageServiceAuth::AnalyzeEntities(
grpc::ClientContext& context,
google::cloud::language::v2::AnalyzeEntitiesRequest const& request) {
auto status = auth_->ConfigureContext(context);
if (!status.ok()) return status;
return child_->AnalyzeEntities(context, request);
}

StatusOr<google::cloud::language::v2::ClassifyTextResponse>
LanguageServiceAuth::ClassifyText(
grpc::ClientContext& context,
google::cloud::language::v2::ClassifyTextRequest const& request) {
auto status = auth_->ConfigureContext(context);
if (!status.ok()) return status;
return child_->ClassifyText(context, request);
}

StatusOr<google::cloud::language::v2::ModerateTextResponse>
LanguageServiceAuth::ModerateText(
grpc::ClientContext& context,
google::cloud::language::v2::ModerateTextRequest const& request) {
auto status = auth_->ConfigureContext(context);
if (!status.ok()) return status;
return child_->ModerateText(context, request);
}

StatusOr<google::cloud::language::v2::AnnotateTextResponse>
LanguageServiceAuth::AnnotateText(
grpc::ClientContext& context,
google::cloud::language::v2::AnnotateTextRequest const& request) {
auto status = auth_->ConfigureContext(context);
if (!status.ok()) return status;
return child_->AnnotateText(context, request);
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace language_v2_internal
} // namespace cloud
} // namespace google
Loading

0 comments on commit 769450e

Please sign in to comment.