-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add SPI layer for Google Translate #1143
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?xml version="1.0"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <artifactId>gcloud-java-translate</artifactId> | ||
| <packaging>jar</packaging> | ||
| <name>GCloud Java translate</name> | ||
| <url>https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-translate</url> | ||
| <description> | ||
| Java idiomatic client for Google Translate. | ||
| </description> | ||
| <parent> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>gcloud-java-pom</artifactId> | ||
| <version>0.2.7-SNAPSHOT</version> | ||
| </parent> | ||
| <properties> | ||
| <site.installationModule>gcloud-java-translate</site.installationModule> | ||
| </properties> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>${project.groupId}</groupId> | ||
| <artifactId>gcloud-java-core</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.apis</groupId> | ||
| <artifactId>google-api-services-translate</artifactId> | ||
| <version>v2-rev47-1.22.0</version> | ||
| <scope>compile</scope> | ||
| <exclusions> | ||
| <exclusion> | ||
| <groupId>com.google.guava</groupId> | ||
| <artifactId>guava-jdk5</artifactId> | ||
| </exclusion> | ||
| <exclusion> | ||
| <groupId>com.google.api-client</groupId> | ||
| <artifactId>google-api-client</artifactId> | ||
| </exclusion> | ||
| </exclusions> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>${project.groupId}</groupId> | ||
| <artifactId>gcloud-java-core</artifactId> | ||
| <version>${project.version}</version> | ||
| <type>test-jar</type> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.12</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.easymock</groupId> | ||
| <artifactId>easymock</artifactId> | ||
| <version>3.4</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> | ||
27 changes: 27 additions & 0 deletions
27
gcloud-java-translate/src/main/java/com/google/cloud/translate/Translate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright 2016 Google Inc. All Rights Reserved. | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
|
|
||
| package com.google.cloud.translate; | ||
|
|
||
| import com.google.cloud.Service; | ||
|
|
||
| /** | ||
| * An interface for Google Translate. | ||
| * | ||
| * @see <a href="https://cloud.google.com/translate/docs">Google Translate</a> | ||
| */ | ||
| public interface Translate extends Service<TranslateOptions> { | ||
| } |
63 changes: 63 additions & 0 deletions
63
gcloud-java-translate/src/main/java/com/google/cloud/translate/TranslateException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * Copyright 2016 Google Inc. All Rights Reserved. | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
|
|
||
| package com.google.cloud.translate; | ||
|
|
||
| import com.google.cloud.BaseServiceException; | ||
| import com.google.cloud.RetryHelper.RetryHelperException; | ||
| import com.google.cloud.RetryHelper.RetryInterruptedException; | ||
| import com.google.common.collect.ImmutableSet; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Google Translate service exception. | ||
| */ | ||
| public class TranslateException extends BaseServiceException { | ||
|
|
||
| private static final Set<Error> RETRYABLE_ERRORS = ImmutableSet.of(new Error(500, null)); | ||
| private static final long serialVersionUID = 4747004866996469418L; | ||
|
|
||
| TranslateException(int code, String message) { | ||
| super(code, message, null, true, null); | ||
| } | ||
|
|
||
| TranslateException(int code, String message, Throwable cause) { | ||
| super(code, message, null, true, cause); | ||
| } | ||
|
|
||
| public TranslateException(IOException exception) { | ||
| super(exception, true); | ||
| } | ||
|
|
||
| @Override | ||
| protected Set<Error> retryableErrors() { | ||
| return RETRYABLE_ERRORS; | ||
| } | ||
|
|
||
| /** | ||
| * Translate RetryHelperException to the TranslateException that caused the error. This method | ||
| * will always throw an exception. | ||
| * | ||
| * @throws TranslateException when {@code ex} was caused by a {@code TranslateException} | ||
| * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} | ||
| */ | ||
| static BaseServiceException translateAndThrow(RetryHelperException ex) { | ||
| BaseServiceException.translateAndPropagateIfPossible(ex); | ||
| throw new TranslateException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
gcloud-java-translate/src/main/java/com/google/cloud/translate/TranslateFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Copyright 2016 Google Inc. All Rights Reserved. | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
|
|
||
| package com.google.cloud.translate; | ||
|
|
||
| import com.google.cloud.ServiceFactory; | ||
|
|
||
| /** | ||
| * An interface for Translates factories. | ||
| */ | ||
| public interface TranslateFactory extends ServiceFactory<Translate, TranslateOptions> { | ||
| } |
188 changes: 188 additions & 0 deletions
188
gcloud-java-translate/src/main/java/com/google/cloud/translate/TranslateOptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /* | ||
| * Copyright 2016 Google Inc. All Rights Reserved. | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
|
|
||
| package com.google.cloud.translate; | ||
|
|
||
| import static com.google.common.base.MoreObjects.firstNonNull; | ||
|
|
||
| import com.google.cloud.AuthCredentials; | ||
| import com.google.cloud.HttpServiceOptions; | ||
| import com.google.cloud.translate.spi.DefaultTranslateRpc; | ||
| import com.google.cloud.translate.spi.TranslateRpc; | ||
| import com.google.cloud.translate.spi.TranslateRpcFactory; | ||
| import com.google.common.collect.ImmutableSet; | ||
|
|
||
| import java.util.Locale; | ||
| import java.util.Set; | ||
|
|
||
| public class TranslateOptions extends | ||
| HttpServiceOptions<Translate, TranslateRpc, TranslateOptions> { | ||
|
|
||
| private static final long serialVersionUID = 5997441123713672886L; | ||
| private static final Set<String> SCOPES = ImmutableSet.of(); | ||
|
|
||
| private final String apiKey; | ||
| private final String targetLanguage; | ||
|
|
||
| public static class DefaultTranslateFactory implements TranslateFactory { | ||
|
|
||
| private static final TranslateFactory INSTANCE = new DefaultTranslateFactory(); | ||
|
|
||
| @Override | ||
| public Translate create(TranslateOptions options) { | ||
| return null; | ||
| // todo(mziccard) uncomment as soon as TranslateImpl is implemented | ||
| // return new TranslateImpl(options); | ||
| } | ||
| } | ||
|
|
||
| public static class DefaultTranslateRpcFactory implements TranslateRpcFactory { | ||
|
|
||
| private static final TranslateRpcFactory INSTANCE = new DefaultTranslateRpcFactory(); | ||
|
|
||
| @Override | ||
| public TranslateRpc create(TranslateOptions options) { | ||
| return new DefaultTranslateRpc(options); | ||
| } | ||
| } | ||
|
|
||
| public static class Builder extends | ||
| HttpServiceOptions.Builder<Translate, TranslateRpc, TranslateOptions, Builder> { | ||
|
|
||
| private final String apiKey; | ||
| private String targetLanguage; | ||
|
|
||
| private Builder(String apiKey) { | ||
| this.apiKey = apiKey; | ||
| } | ||
|
|
||
| private Builder(TranslateOptions options) { | ||
| super(options); | ||
| this.apiKey = options.apiKey; | ||
| } | ||
|
|
||
| /** | ||
| * Sets project id. Setting a project id has no impact on the {@link Translate} service. | ||
| * | ||
| * @return the builder | ||
| */ | ||
| @Override | ||
| public Builder projectId(String projectId) { | ||
| super.projectId(projectId); | ||
| return self(); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the service authentication credentials. Setting credentials has no impact on the | ||
| * {@link Translate} service. | ||
| * | ||
| * @return the builder | ||
| */ | ||
| public Builder authCredentials(AuthCredentials authCredentials) { | ||
| super.authCredentials(authCredentials); | ||
| return self(); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the code for the default target language. If not set, english ({@code en}) is used. | ||
| * | ||
| * @return the builder | ||
| */ | ||
| public Builder targetLanguage(String targetLanguage) { | ||
| this.targetLanguage = targetLanguage; | ||
| return self(); | ||
| } | ||
|
|
||
| @Override | ||
| public TranslateOptions build() { | ||
| return new TranslateOptions(this); | ||
| } | ||
| } | ||
|
|
||
| private TranslateOptions(Builder builder) { | ||
| super(TranslateFactory.class, TranslateRpcFactory.class, builder); | ||
| this.apiKey = builder.apiKey; | ||
| this.targetLanguage = firstNonNull(builder.targetLanguage, Locale.ENGLISH.getLanguage()); | ||
| } | ||
|
|
||
| @Override | ||
| protected TranslateFactory defaultServiceFactory() { | ||
| return DefaultTranslateFactory.INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| protected TranslateRpcFactory defaultRpcFactory() { | ||
| return DefaultTranslateRpcFactory.INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean projectIdRequired() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| protected Set<String> scopes() { | ||
| return SCOPES; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the API key, to be used used to send requests. | ||
| */ | ||
| public String apiKey() { | ||
| return apiKey; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the code for the default target language. | ||
| */ | ||
| public String targetLanguage() { | ||
| return targetLanguage; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a default {@code TranslateOptions} instance given an API key. For instructions on | ||
| * how to get an API key see <a href="https://cloud.google.com/translate/v2/quickstart">Translate | ||
| * quickstart</a>. | ||
| */ | ||
| public static TranslateOptions defaultInstance(String apiKey) { | ||
| return builder(apiKey).build(); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public Builder toBuilder() { | ||
| return new Builder(this); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return baseHashCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| return obj instanceof TranslateOptions && baseEquals((TranslateOptions) obj); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a builder for {@code TranslateOptions} objects given an api key. For instructions on | ||
| * how to get an API key see <a href="https://cloud.google.com/translate/v2/quickstart">Translate | ||
| * quickstart</a>. | ||
| */ | ||
| public static Builder builder(String apiKey) { | ||
| return new Builder(apiKey); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.