Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions gcloud-java-translate/pom.xml
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>

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

<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>
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> {
}
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());
}
}
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> {
}
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);
}
}
Loading