Skip to content
This repository has been archived by the owner on Oct 14, 2023. It is now read-only.

fix: use rest api for detect and listSupportedLanguages #243

Merged
merged 3 commits into from Jul 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -19,23 +19,18 @@
import static com.google.common.base.MoreObjects.firstNonNull;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.json.JsonHttpContent;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.translate.Translate;
import com.google.api.services.translate.model.DetectionsListResponse;
import com.google.api.services.translate.model.DetectionsResourceItems;
import com.google.api.services.translate.model.LanguagesListResponse;
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.ImmutableMap;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -73,20 +68,8 @@ private GenericUrl buildTargetUrl(String path) {
@Override
public List<List<DetectionsResourceItems>> detect(List<String> texts) {
try {
Map<String, ?> content = ImmutableMap.of("q", texts);
HttpRequest httpRequest =
translate
.getRequestFactory()
.buildPostRequest(
buildTargetUrl("detect"),
new JsonHttpContent(translate.getJsonFactory(), content))
.setParser(translate.getObjectParser());
List<List<DetectionsResourceItems>> detections =
httpRequest.execute().parseAs(DetectionsListResponse.class).getDetections();
// TODO use REST apiary as soon as it supports POST
// List<List<DetectionsResourceItems>> detections =
//
// translate.detections().list(texts).setKey(options.getApiKey()).execute().getDetections();
translate.detections().list(texts).setKey(options.getApiKey()).execute().getDetections();
return detections != null ? detections : ImmutableList.<List<DetectionsResourceItems>>of();
} catch (IOException ex) {
throw translate(ex);
Expand All @@ -96,27 +79,16 @@ public List<List<DetectionsResourceItems>> detect(List<String> texts) {
@Override
public List<LanguagesResource> listSupportedLanguages(Map<Option, ?> optionMap) {
try {
Map<String, ?> content =
ImmutableMap.of(
"target",
firstNonNull(
Option.TARGET_LANGUAGE.getString(optionMap), options.getTargetLanguage()));
HttpRequest httpRequest =
translate
.getRequestFactory()
.buildPostRequest(
buildTargetUrl("languages"),
new JsonHttpContent(translate.getJsonFactory(), content))
.setParser(translate.getObjectParser());
List<LanguagesResource> languages =
httpRequest.execute().parseAs(LanguagesListResponse.class).getLanguages();
// TODO use REST apiary as soon as it supports POST
// List<LanguagesResource> languages = translate.languages()
// .list()
// .setKey(options.getApiKey())
// .setTarget(
// firstNonNull(TARGET_LANGUAGE.getString(optionMap), options.getTargetLanguage()))
// .execute().getLanguages();
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);
Expand Down