This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
/
HttpTranslateRpc.java
129 lines (118 loc) · 4.9 KB
/
HttpTranslateRpc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright 2016 Google LLC
*
* 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.spi.v2;
import static com.google.common.base.MoreObjects.firstNonNull;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.translate.Translate;
import com.google.api.services.translate.model.DetectionsResourceItems;
import com.google.api.services.translate.model.LanguagesResource;
import com.google.api.services.translate.model.TranslationsResource;
import com.google.cloud.http.HttpTransportOptions;
import com.google.cloud.translate.TranslateException;
import com.google.cloud.translate.TranslateOptions;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class HttpTranslateRpc implements TranslateRpc {
private final TranslateOptions options;
private final Translate translate;
public HttpTranslateRpc(TranslateOptions options) {
HttpTransportOptions transportOptions = (HttpTransportOptions) options.getTransportOptions();
HttpTransport transport = transportOptions.getHttpTransportFactory().create();
HttpRequestInitializer initializer = transportOptions.getHttpRequestInitializer(options);
this.options = options;
translate =
new Translate.Builder(transport, new JacksonFactory(), initializer)
.setRootUrl(options.getHost())
.setApplicationName(options.getApplicationName())
.build();
}
private static TranslateException translate(IOException exception) {
return new TranslateException(exception);
}
private GenericUrl buildTargetUrl(String path) {
GenericUrl genericUrl = new GenericUrl(translate.getBaseUrl() + "v2/" + path);
if (options.getApiKey() != null) {
genericUrl.put("key", options.getApiKey());
}
return genericUrl;
}
@Override
public List<List<DetectionsResourceItems>> detect(List<String> texts) {
try {
List<List<DetectionsResourceItems>> detections =
translate.detections().list(texts).setKey(options.getApiKey()).execute().getDetections();
return detections != null ? detections : ImmutableList.<List<DetectionsResourceItems>>of();
} catch (IOException ex) {
throw translate(ex);
}
}
@Override
public List<LanguagesResource> listSupportedLanguages(Map<Option, ?> optionMap) {
try {
List<LanguagesResource> languages =
translate
.languages()
.list()
.setKey(options.getApiKey())
.setTarget(
firstNonNull(
Option.TARGET_LANGUAGE.getString(optionMap), options.getTargetLanguage()))
.execute()
.getLanguages();
return languages != null ? languages : ImmutableList.<LanguagesResource>of();
} catch (IOException ex) {
throw translate(ex);
}
}
@Override
public List<TranslationsResource> translate(List<String> texts, Map<Option, ?> optionMap) {
try {
String targetLanguage =
firstNonNull(Option.TARGET_LANGUAGE.getString(optionMap), options.getTargetLanguage());
final String sourceLanguage = Option.SOURCE_LANGUAGE.getString(optionMap);
List<TranslationsResource> translations =
translate
.translations()
.list(texts, targetLanguage)
.setSource(sourceLanguage)
.setKey(options.getApiKey())
.setModel(Option.MODEL.getString(optionMap))
.setFormat(Option.FORMAT.getString(optionMap))
.execute()
.getTranslations();
return Lists.transform(
translations != null ? translations : ImmutableList.<TranslationsResource>of(),
new Function<TranslationsResource, TranslationsResource>() {
@Override
public TranslationsResource apply(TranslationsResource translationsResource) {
if (translationsResource.getDetectedSourceLanguage() == null) {
translationsResource.setDetectedSourceLanguage(sourceLanguage);
}
return translationsResource;
}
});
} catch (IOException ex) {
throw translate(ex);
}
}
}