Skip to content

Commit bb3d01e

Browse files
committed
Add Translate SPI layer
1 parent 64115e4 commit bb3d01e

File tree

11 files changed

+689
-0
lines changed

11 files changed

+689
-0
lines changed

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: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
//return new TranslateImpl(options);
48+
}
49+
}
50+
51+
public static class DefaultTranslateRpcFactory implements TranslateRpcFactory {
52+
53+
private static final TranslateRpcFactory INSTANCE = new DefaultTranslateRpcFactory();
54+
55+
@Override
56+
public TranslateRpc create(TranslateOptions options) {
57+
return new DefaultTranslateRpc(options);
58+
}
59+
}
60+
61+
public static class Builder extends
62+
HttpServiceOptions.Builder<Translate, TranslateRpc, TranslateOptions, Builder> {
63+
64+
private final String apiKey;
65+
private String targetLanguage;
66+
67+
private Builder(String apiKey) {
68+
this.apiKey = apiKey;
69+
}
70+
71+
private Builder(TranslateOptions options) {
72+
super(options);
73+
this.apiKey = options.apiKey;
74+
}
75+
76+
/**
77+
* Sets project id. Setting a project id has no impact on the {@link Translate} service.
78+
*
79+
* @return the builder
80+
*/
81+
@Override
82+
public Builder projectId(String projectId) {
83+
super.projectId(projectId);
84+
return self();
85+
}
86+
87+
/**
88+
* Sets the service authentication credentials. Setting credentials has no impact on the
89+
* {@link Translate} service.
90+
*
91+
* @return the builder
92+
*/
93+
public Builder authCredentials(AuthCredentials authCredentials) {
94+
super.authCredentials(authCredentials);
95+
return self();
96+
}
97+
98+
/**
99+
* Sets the code for the default target language. If not set, english ({@code en}) is used.
100+
*
101+
* @return the builder
102+
*/
103+
public Builder targetLanguage(String targetLanguage) {
104+
this.targetLanguage = targetLanguage;
105+
return self();
106+
}
107+
108+
@Override
109+
public TranslateOptions build() {
110+
return new TranslateOptions(this);
111+
}
112+
}
113+
114+
private TranslateOptions(Builder builder) {
115+
super(TranslateFactory.class, TranslateRpcFactory.class, builder);
116+
this.apiKey = builder.apiKey;
117+
this.targetLanguage = firstNonNull(builder.targetLanguage, Locale.ENGLISH.getLanguage());
118+
}
119+
120+
@Override
121+
protected TranslateFactory defaultServiceFactory() {
122+
return DefaultTranslateFactory.INSTANCE;
123+
}
124+
125+
@Override
126+
protected TranslateRpcFactory defaultRpcFactory() {
127+
return DefaultTranslateRpcFactory.INSTANCE;
128+
}
129+
130+
@Override
131+
protected boolean projectIdRequired() {
132+
return false;
133+
}
134+
135+
@Override
136+
protected Set<String> scopes() {
137+
return SCOPES;
138+
}
139+
140+
/**
141+
* Returns the API key, to be used used to send requests.
142+
*/
143+
public String apiKey() {
144+
return apiKey;
145+
}
146+
147+
/**
148+
* Returns the code for the default target language.
149+
*/
150+
public String targetLanguage() {
151+
return targetLanguage;
152+
}
153+
154+
/**
155+
* Returns a default {@code TranslateOptions} instance given an API key. For instructions on
156+
* how to get an API key see <a href="https://cloud.google.com/translate/v2/quickstart">Translate
157+
* quickstart</a>.
158+
*/
159+
public static TranslateOptions defaultInstance(String apiKey) {
160+
return builder(apiKey).build();
161+
}
162+
163+
@SuppressWarnings("unchecked")
164+
@Override
165+
public Builder toBuilder() {
166+
return new Builder(this);
167+
}
168+
169+
@Override
170+
public int hashCode() {
171+
return baseHashCode();
172+
}
173+
174+
@Override
175+
public boolean equals(Object obj) {
176+
return obj instanceof TranslateOptions && baseEquals((TranslateOptions) obj);
177+
}
178+
179+
/**
180+
* Creates a builder for {@code TranslateOptions} objects given an api key. For instructions on
181+
* how to get an API key see <a href="https://cloud.google.com/translate/v2/quickstart">Translate
182+
* quickstart</a>.
183+
*/
184+
public static Builder builder(String apiKey) {
185+
return new Builder(apiKey);
186+
}
187+
}

0 commit comments

Comments
 (0)