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

feat: [language] Added client library support for ModerateText in the Natural Language API (V1) #9463

Merged
merged 4 commits into from
May 26, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions java-language/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.12.0</version>
<version>26.15.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -227,7 +227,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java11.html
[stability-image]: https://img.shields.io/badge/stability-stable-green
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-language.svg
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-language/2.15.0
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-language/2.18.0
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,84 @@ public final UnaryCallable<ClassifyTextRequest, ClassifyTextResponse> classifyTe
return stub.classifyTextCallable();
}

// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Moderates a document for harmful and sensitive categories.
*
* <p>Sample code:
*
* <pre>{@code
* // This snippet has been automatically generated and should be regarded as a code template only.
* // It will require modifications to work:
* // - It may require correct/in-range values for request initialization.
* // - It may require specifying regional endpoints when creating the service client as shown in
* // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
* try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
* Document document = Document.newBuilder().build();
* ModerateTextResponse response = languageServiceClient.moderateText(document);
* }
* }</pre>
*
* @param document Required. Input document.
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
*/
public final ModerateTextResponse moderateText(Document document) {
ModerateTextRequest request = ModerateTextRequest.newBuilder().setDocument(document).build();
return moderateText(request);
}

// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Moderates a document for harmful and sensitive categories.
*
* <p>Sample code:
*
* <pre>{@code
* // This snippet has been automatically generated and should be regarded as a code template only.
* // It will require modifications to work:
* // - It may require correct/in-range values for request initialization.
* // - It may require specifying regional endpoints when creating the service client as shown in
* // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
* try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
* ModerateTextRequest request =
* ModerateTextRequest.newBuilder().setDocument(Document.newBuilder().build()).build();
* ModerateTextResponse response = languageServiceClient.moderateText(request);
* }
* }</pre>
*
* @param request The request object containing all of the parameters for the API call.
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
*/
public final ModerateTextResponse moderateText(ModerateTextRequest request) {
return moderateTextCallable().call(request);
}

// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Moderates a document for harmful and sensitive categories.
*
* <p>Sample code:
*
* <pre>{@code
* // This snippet has been automatically generated and should be regarded as a code template only.
* // It will require modifications to work:
* // - It may require correct/in-range values for request initialization.
* // - It may require specifying regional endpoints when creating the service client as shown in
* // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
* try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
* ModerateTextRequest request =
* ModerateTextRequest.newBuilder().setDocument(Document.newBuilder().build()).build();
* ApiFuture<ModerateTextResponse> future =
* languageServiceClient.moderateTextCallable().futureCall(request);
* // Do something.
* ModerateTextResponse response = future.get();
* }
* }</pre>
*/
public final UnaryCallable<ModerateTextRequest, ModerateTextResponse> moderateTextCallable() {
return stub.moderateTextCallable();
}

// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* A convenience method that provides all the features that analyzeSentiment, analyzeEntities, and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public UnaryCallSettings<ClassifyTextRequest, ClassifyTextResponse> classifyText
return ((LanguageServiceStubSettings) getStubSettings()).classifyTextSettings();
}

/** Returns the object with the settings used for calls to moderateText. */
public UnaryCallSettings<ModerateTextRequest, ModerateTextResponse> moderateTextSettings() {
return ((LanguageServiceStubSettings) getStubSettings()).moderateTextSettings();
}

/** Returns the object with the settings used for calls to annotateText. */
public UnaryCallSettings<AnnotateTextRequest, AnnotateTextResponse> annotateTextSettings() {
return ((LanguageServiceStubSettings) getStubSettings()).annotateTextSettings();
Expand Down Expand Up @@ -250,6 +255,12 @@ public Builder applyToAllUnaryMethods(
return getStubSettingsBuilder().classifyTextSettings();
}

/** Returns the builder for the settings used for calls to moderateText. */
public UnaryCallSettings.Builder<ModerateTextRequest, ModerateTextResponse>
moderateTextSettings() {
return getStubSettingsBuilder().moderateTextSettings();
}

/** Returns the builder for the settings used for calls to annotateText. */
public UnaryCallSettings.Builder<AnnotateTextRequest, AnnotateTextResponse>
annotateTextSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
},
"ClassifyText": {
"methods": ["classifyText", "classifyText", "classifyTextCallable"]
},
"ModerateText": {
"methods": ["moderateText", "moderateText", "moderateTextCallable"]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import com.google.cloud.language.v1.AnnotateTextResponse;
import com.google.cloud.language.v1.ClassifyTextRequest;
import com.google.cloud.language.v1.ClassifyTextResponse;
import com.google.cloud.language.v1.ModerateTextRequest;
import com.google.cloud.language.v1.ModerateTextResponse;
import com.google.longrunning.stub.GrpcOperationsStub;
import io.grpc.MethodDescriptor;
import io.grpc.protobuf.ProtoUtils;
Expand Down Expand Up @@ -105,6 +107,16 @@ public class GrpcLanguageServiceStub extends LanguageServiceStub {
ProtoUtils.marshaller(ClassifyTextResponse.getDefaultInstance()))
.build();

private static final MethodDescriptor<ModerateTextRequest, ModerateTextResponse>
moderateTextMethodDescriptor =
MethodDescriptor.<ModerateTextRequest, ModerateTextResponse>newBuilder()
.setType(MethodDescriptor.MethodType.UNARY)
.setFullMethodName("google.cloud.language.v1.LanguageService/ModerateText")
.setRequestMarshaller(ProtoUtils.marshaller(ModerateTextRequest.getDefaultInstance()))
.setResponseMarshaller(
ProtoUtils.marshaller(ModerateTextResponse.getDefaultInstance()))
.build();

private static final MethodDescriptor<AnnotateTextRequest, AnnotateTextResponse>
annotateTextMethodDescriptor =
MethodDescriptor.<AnnotateTextRequest, AnnotateTextResponse>newBuilder()
Expand All @@ -123,6 +135,7 @@ public class GrpcLanguageServiceStub extends LanguageServiceStub {
analyzeEntitySentimentCallable;
private final UnaryCallable<AnalyzeSyntaxRequest, AnalyzeSyntaxResponse> analyzeSyntaxCallable;
private final UnaryCallable<ClassifyTextRequest, ClassifyTextResponse> classifyTextCallable;
private final UnaryCallable<ModerateTextRequest, ModerateTextResponse> moderateTextCallable;
private final UnaryCallable<AnnotateTextRequest, AnnotateTextResponse> annotateTextCallable;

private final BackgroundResource backgroundResources;
Expand Down Expand Up @@ -193,6 +206,10 @@ protected GrpcLanguageServiceStub(
GrpcCallSettings.<ClassifyTextRequest, ClassifyTextResponse>newBuilder()
.setMethodDescriptor(classifyTextMethodDescriptor)
.build();
GrpcCallSettings<ModerateTextRequest, ModerateTextResponse> moderateTextTransportSettings =
GrpcCallSettings.<ModerateTextRequest, ModerateTextResponse>newBuilder()
.setMethodDescriptor(moderateTextMethodDescriptor)
.build();
GrpcCallSettings<AnnotateTextRequest, AnnotateTextResponse> annotateTextTransportSettings =
GrpcCallSettings.<AnnotateTextRequest, AnnotateTextResponse>newBuilder()
.setMethodDescriptor(annotateTextMethodDescriptor)
Expand All @@ -215,6 +232,9 @@ protected GrpcLanguageServiceStub(
this.classifyTextCallable =
callableFactory.createUnaryCallable(
classifyTextTransportSettings, settings.classifyTextSettings(), clientContext);
this.moderateTextCallable =
callableFactory.createUnaryCallable(
moderateTextTransportSettings, settings.moderateTextSettings(), clientContext);
this.annotateTextCallable =
callableFactory.createUnaryCallable(
annotateTextTransportSettings, settings.annotateTextSettings(), clientContext);
Expand Down Expand Up @@ -254,6 +274,11 @@ public UnaryCallable<ClassifyTextRequest, ClassifyTextResponse> classifyTextCall
return classifyTextCallable;
}

@Override
public UnaryCallable<ModerateTextRequest, ModerateTextResponse> moderateTextCallable() {
return moderateTextCallable;
}

@Override
public UnaryCallable<AnnotateTextRequest, AnnotateTextResponse> annotateTextCallable() {
return annotateTextCallable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import com.google.cloud.language.v1.AnnotateTextResponse;
import com.google.cloud.language.v1.ClassifyTextRequest;
import com.google.cloud.language.v1.ClassifyTextResponse;
import com.google.cloud.language.v1.ModerateTextRequest;
import com.google.cloud.language.v1.ModerateTextResponse;
import com.google.protobuf.TypeRegistry;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -242,6 +244,42 @@ public class HttpJsonLanguageServiceStub extends LanguageServiceStub {
.build())
.build();

private static final ApiMethodDescriptor<ModerateTextRequest, ModerateTextResponse>
moderateTextMethodDescriptor =
ApiMethodDescriptor.<ModerateTextRequest, ModerateTextResponse>newBuilder()
.setFullMethodName("google.cloud.language.v1.LanguageService/ModerateText")
.setHttpMethod("POST")
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<ModerateTextRequest>newBuilder()
.setPath(
"/v1/documents:moderateText",
request -> {
Map<String, String> fields = new HashMap<>();
ProtoRestSerializer<ModerateTextRequest> serializer =
ProtoRestSerializer.create();
return fields;
})
.setQueryParamsExtractor(
request -> {
Map<String, List<String>> fields = new HashMap<>();
ProtoRestSerializer<ModerateTextRequest> serializer =
ProtoRestSerializer.create();
serializer.putQueryParam(fields, "$alt", "json;enum-encoding=int");
return fields;
})
.setRequestBodyExtractor(
request ->
ProtoRestSerializer.create()
.toBody("*", request.toBuilder().build(), true))
.build())
.setResponseParser(
ProtoMessageResponseParser.<ModerateTextResponse>newBuilder()
.setDefaultInstance(ModerateTextResponse.getDefaultInstance())
.setDefaultTypeRegistry(typeRegistry)
.build())
.build();

private static final ApiMethodDescriptor<AnnotateTextRequest, AnnotateTextResponse>
annotateTextMethodDescriptor =
ApiMethodDescriptor.<AnnotateTextRequest, AnnotateTextResponse>newBuilder()
Expand Down Expand Up @@ -286,6 +324,7 @@ public class HttpJsonLanguageServiceStub extends LanguageServiceStub {
analyzeEntitySentimentCallable;
private final UnaryCallable<AnalyzeSyntaxRequest, AnalyzeSyntaxResponse> analyzeSyntaxCallable;
private final UnaryCallable<ClassifyTextRequest, ClassifyTextResponse> classifyTextCallable;
private final UnaryCallable<ModerateTextRequest, ModerateTextResponse> moderateTextCallable;
private final UnaryCallable<AnnotateTextRequest, AnnotateTextResponse> annotateTextCallable;

private final BackgroundResource backgroundResources;
Expand Down Expand Up @@ -360,6 +399,11 @@ protected HttpJsonLanguageServiceStub(
.setMethodDescriptor(classifyTextMethodDescriptor)
.setTypeRegistry(typeRegistry)
.build();
HttpJsonCallSettings<ModerateTextRequest, ModerateTextResponse> moderateTextTransportSettings =
HttpJsonCallSettings.<ModerateTextRequest, ModerateTextResponse>newBuilder()
.setMethodDescriptor(moderateTextMethodDescriptor)
.setTypeRegistry(typeRegistry)
.build();
HttpJsonCallSettings<AnnotateTextRequest, AnnotateTextResponse> annotateTextTransportSettings =
HttpJsonCallSettings.<AnnotateTextRequest, AnnotateTextResponse>newBuilder()
.setMethodDescriptor(annotateTextMethodDescriptor)
Expand All @@ -383,6 +427,9 @@ protected HttpJsonLanguageServiceStub(
this.classifyTextCallable =
callableFactory.createUnaryCallable(
classifyTextTransportSettings, settings.classifyTextSettings(), clientContext);
this.moderateTextCallable =
callableFactory.createUnaryCallable(
moderateTextTransportSettings, settings.moderateTextSettings(), clientContext);
this.annotateTextCallable =
callableFactory.createUnaryCallable(
annotateTextTransportSettings, settings.annotateTextSettings(), clientContext);
Expand All @@ -399,6 +446,7 @@ public static List<ApiMethodDescriptor> getMethodDescriptors() {
methodDescriptors.add(analyzeEntitySentimentMethodDescriptor);
methodDescriptors.add(analyzeSyntaxMethodDescriptor);
methodDescriptors.add(classifyTextMethodDescriptor);
methodDescriptors.add(moderateTextMethodDescriptor);
methodDescriptors.add(annotateTextMethodDescriptor);
return methodDescriptors;
}
Expand Down Expand Up @@ -430,6 +478,11 @@ public UnaryCallable<ClassifyTextRequest, ClassifyTextResponse> classifyTextCall
return classifyTextCallable;
}

@Override
public UnaryCallable<ModerateTextRequest, ModerateTextResponse> moderateTextCallable() {
return moderateTextCallable;
}

@Override
public UnaryCallable<AnnotateTextRequest, AnnotateTextResponse> annotateTextCallable() {
return annotateTextCallable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import com.google.cloud.language.v1.AnnotateTextResponse;
import com.google.cloud.language.v1.ClassifyTextRequest;
import com.google.cloud.language.v1.ClassifyTextResponse;
import com.google.cloud.language.v1.ModerateTextRequest;
import com.google.cloud.language.v1.ModerateTextResponse;
import javax.annotation.Generated;

// AUTO-GENERATED DOCUMENTATION AND CLASS.
Expand Down Expand Up @@ -63,6 +65,10 @@ public UnaryCallable<ClassifyTextRequest, ClassifyTextResponse> classifyTextCall
throw new UnsupportedOperationException("Not implemented: classifyTextCallable()");
}

public UnaryCallable<ModerateTextRequest, ModerateTextResponse> moderateTextCallable() {
throw new UnsupportedOperationException("Not implemented: moderateTextCallable()");
}

public UnaryCallable<AnnotateTextRequest, AnnotateTextResponse> annotateTextCallable() {
throw new UnsupportedOperationException("Not implemented: annotateTextCallable()");
}
Expand Down