Skip to content

Commit 36c66a5

Browse files
authored
Add SPI layer for Google Translate (#1143)
1 parent 64115e4 commit 36c66a5

11 files changed

Lines changed: 690 additions & 0 deletions

File tree

gcloud-java-translate/pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>gcloud-java-translate</artifactId>
5+
<packaging>jar</packaging>
6+
<name>GCloud Java translate</name>
7+
<url>https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-translate</url>
8+
<description>
9+
Java idiomatic client for Google Translate.
10+
</description>
11+
<parent>
12+
<groupId>com.google.cloud</groupId>
13+
<artifactId>gcloud-java-pom</artifactId>
14+
<version>0.2.7-SNAPSHOT</version>
15+
</parent>
16+
<properties>
17+
<site.installationModule>gcloud-java-translate</site.installationModule>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>${project.groupId}</groupId>
22+
<artifactId>gcloud-java-core</artifactId>
23+
<version>${project.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.google.apis</groupId>
27+
<artifactId>google-api-services-translate</artifactId>
28+
<version>v2-rev47-1.22.0</version>
29+
<scope>compile</scope>
30+
<exclusions>
31+
<exclusion>
32+
<groupId>com.google.guava</groupId>
33+
<artifactId>guava-jdk5</artifactId>
34+
</exclusion>
35+
<exclusion>
36+
<groupId>com.google.api-client</groupId>
37+
<artifactId>google-api-client</artifactId>
38+
</exclusion>
39+
</exclusions>
40+
</dependency>
41+
<dependency>
42+
<groupId>${project.groupId}</groupId>
43+
<artifactId>gcloud-java-core</artifactId>
44+
<version>${project.version}</version>
45+
<type>test-jar</type>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>junit</groupId>
50+
<artifactId>junit</artifactId>
51+
<version>4.12</version>
52+
<scope>test</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.easymock</groupId>
56+
<artifactId>easymock</artifactId>
57+
<version>3.4</version>
58+
<scope>test</scope>
59+
</dependency>
60+
</dependencies>
61+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.translate;
18+
19+
import com.google.cloud.Service;
20+
21+
/**
22+
* An interface for Google Translate.
23+
*
24+
* @see <a href="https://cloud.google.com/translate/docs">Google Translate</a>
25+
*/
26+
public interface Translate extends Service<TranslateOptions> {
27+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.translate;
18+
19+
import com.google.cloud.BaseServiceException;
20+
import com.google.cloud.RetryHelper.RetryHelperException;
21+
import com.google.cloud.RetryHelper.RetryInterruptedException;
22+
import com.google.common.collect.ImmutableSet;
23+
24+
import java.io.IOException;
25+
import java.util.Set;
26+
27+
/**
28+
* Google Translate service exception.
29+
*/
30+
public class TranslateException extends BaseServiceException {
31+
32+
private static final Set<Error> RETRYABLE_ERRORS = ImmutableSet.of(new Error(500, null));
33+
private static final long serialVersionUID = 4747004866996469418L;
34+
35+
TranslateException(int code, String message) {
36+
super(code, message, null, true, null);
37+
}
38+
39+
TranslateException(int code, String message, Throwable cause) {
40+
super(code, message, null, true, cause);
41+
}
42+
43+
public TranslateException(IOException exception) {
44+
super(exception, true);
45+
}
46+
47+
@Override
48+
protected Set<Error> retryableErrors() {
49+
return RETRYABLE_ERRORS;
50+
}
51+
52+
/**
53+
* Translate RetryHelperException to the TranslateException that caused the error. This method
54+
* will always throw an exception.
55+
*
56+
* @throws TranslateException when {@code ex} was caused by a {@code TranslateException}
57+
* @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException}
58+
*/
59+
static BaseServiceException translateAndThrow(RetryHelperException ex) {
60+
BaseServiceException.translateAndPropagateIfPossible(ex);
61+
throw new TranslateException(UNKNOWN_CODE, ex.getMessage(), ex.getCause());
62+
}
63+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.translate;
18+
19+
import com.google.cloud.ServiceFactory;
20+
21+
/**
22+
* An interface for Translates factories.
23+
*/
24+
public interface TranslateFactory extends ServiceFactory<Translate, TranslateOptions> {
25+
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.translate;
18+
19+
import static com.google.common.base.MoreObjects.firstNonNull;
20+
21+
import com.google.cloud.AuthCredentials;
22+
import com.google.cloud.HttpServiceOptions;
23+
import com.google.cloud.translate.spi.DefaultTranslateRpc;
24+
import com.google.cloud.translate.spi.TranslateRpc;
25+
import com.google.cloud.translate.spi.TranslateRpcFactory;
26+
import com.google.common.collect.ImmutableSet;
27+
28+
import java.util.Locale;
29+
import java.util.Set;
30+
31+
public class TranslateOptions extends
32+
HttpServiceOptions<Translate, TranslateRpc, TranslateOptions> {
33+
34+
private static final long serialVersionUID = 5997441123713672886L;
35+
private static final Set<String> SCOPES = ImmutableSet.of();
36+
37+
private final String apiKey;
38+
private final String targetLanguage;
39+
40+
public static class DefaultTranslateFactory implements TranslateFactory {
41+
42+
private static final TranslateFactory INSTANCE = new DefaultTranslateFactory();
43+
44+
@Override
45+
public Translate create(TranslateOptions options) {
46+
return null;
47+
// todo(mziccard) uncomment as soon as TranslateImpl is implemented
48+
// return new TranslateImpl(options);
49+
}
50+
}
51+
52+
public static class DefaultTranslateRpcFactory implements TranslateRpcFactory {
53+
54+
private static final TranslateRpcFactory INSTANCE = new DefaultTranslateRpcFactory();
55+
56+
@Override
57+
public TranslateRpc create(TranslateOptions options) {
58+
return new DefaultTranslateRpc(options);
59+
}
60+
}
61+
62+
public static class Builder extends
63+
HttpServiceOptions.Builder<Translate, TranslateRpc, TranslateOptions, Builder> {
64+
65+
private final String apiKey;
66+
private String targetLanguage;
67+
68+
private Builder(String apiKey) {
69+
this.apiKey = apiKey;
70+
}
71+
72+
private Builder(TranslateOptions options) {
73+
super(options);
74+
this.apiKey = options.apiKey;
75+
}
76+
77+
/**
78+
* Sets project id. Setting a project id has no impact on the {@link Translate} service.
79+
*
80+
* @return the builder
81+
*/
82+
@Override
83+
public Builder projectId(String projectId) {
84+
super.projectId(projectId);
85+
return self();
86+
}
87+
88+
/**
89+
* Sets the service authentication credentials. Setting credentials has no impact on the
90+
* {@link Translate} service.
91+
*
92+
* @return the builder
93+
*/
94+
public Builder authCredentials(AuthCredentials authCredentials) {
95+
super.authCredentials(authCredentials);
96+
return self();
97+
}
98+
99+
/**
100+
* Sets the code for the default target language. If not set, english ({@code en}) is used.
101+
*
102+
* @return the builder
103+
*/
104+
public Builder targetLanguage(String targetLanguage) {
105+
this.targetLanguage = targetLanguage;
106+
return self();
107+
}
108+
109+
@Override
110+
public TranslateOptions build() {
111+
return new TranslateOptions(this);
112+
}
113+
}
114+
115+
private TranslateOptions(Builder builder) {
116+
super(TranslateFactory.class, TranslateRpcFactory.class, builder);
117+
this.apiKey = builder.apiKey;
118+
this.targetLanguage = firstNonNull(builder.targetLanguage, Locale.ENGLISH.getLanguage());
119+
}
120+
121+
@Override
122+
protected TranslateFactory defaultServiceFactory() {
123+
return DefaultTranslateFactory.INSTANCE;
124+
}
125+
126+
@Override
127+
protected TranslateRpcFactory defaultRpcFactory() {
128+
return DefaultTranslateRpcFactory.INSTANCE;
129+
}
130+
131+
@Override
132+
protected boolean projectIdRequired() {
133+
return false;
134+
}
135+
136+
@Override
137+
protected Set<String> scopes() {
138+
return SCOPES;
139+
}
140+
141+
/**
142+
* Returns the API key, to be used used to send requests.
143+
*/
144+
public String apiKey() {
145+
return apiKey;
146+
}
147+
148+
/**
149+
* Returns the code for the default target language.
150+
*/
151+
public String targetLanguage() {
152+
return targetLanguage;
153+
}
154+
155+
/**
156+
* Returns a default {@code TranslateOptions} instance given an API key. For instructions on
157+
* how to get an API key see <a href="https://cloud.google.com/translate/v2/quickstart">Translate
158+
* quickstart</a>.
159+
*/
160+
public static TranslateOptions defaultInstance(String apiKey) {
161+
return builder(apiKey).build();
162+
}
163+
164+
@SuppressWarnings("unchecked")
165+
@Override
166+
public Builder toBuilder() {
167+
return new Builder(this);
168+
}
169+
170+
@Override
171+
public int hashCode() {
172+
return baseHashCode();
173+
}
174+
175+
@Override
176+
public boolean equals(Object obj) {
177+
return obj instanceof TranslateOptions && baseEquals((TranslateOptions) obj);
178+
}
179+
180+
/**
181+
* Creates a builder for {@code TranslateOptions} objects given an api key. For instructions on
182+
* how to get an API key see <a href="https://cloud.google.com/translate/v2/quickstart">Translate
183+
* quickstart</a>.
184+
*/
185+
public static Builder builder(String apiKey) {
186+
return new Builder(apiKey);
187+
}
188+
}

0 commit comments

Comments
 (0)