19
19
import static com .google .common .base .MoreObjects .firstNonNull ;
20
20
21
21
import com .google .api .client .http .GenericUrl ;
22
- import com .google .api .client .http .HttpRequest ;
23
22
import com .google .api .client .http .HttpRequestInitializer ;
24
23
import com .google .api .client .http .HttpTransport ;
25
- import com .google .api .client .http .json .JsonHttpContent ;
26
24
import com .google .api .client .json .jackson2 .JacksonFactory ;
27
25
import com .google .api .services .translate .Translate ;
28
- import com .google .api .services .translate .model .DetectionsListResponse ;
29
26
import com .google .api .services .translate .model .DetectionsResourceItems ;
30
- import com .google .api .services .translate .model .LanguagesListResponse ;
31
27
import com .google .api .services .translate .model .LanguagesResource ;
32
28
import com .google .api .services .translate .model .TranslationsResource ;
33
29
import com .google .cloud .http .HttpTransportOptions ;
34
30
import com .google .cloud .translate .TranslateException ;
35
31
import com .google .cloud .translate .TranslateOptions ;
36
32
import com .google .common .base .Function ;
37
33
import com .google .common .collect .ImmutableList ;
38
- import com .google .common .collect .ImmutableMap ;
39
34
import com .google .common .collect .Lists ;
40
35
import java .io .IOException ;
41
36
import java .util .List ;
@@ -73,20 +68,8 @@ private GenericUrl buildTargetUrl(String path) {
73
68
@ Override
74
69
public List <List <DetectionsResourceItems >> detect (List <String > texts ) {
75
70
try {
76
- Map <String , ?> content = ImmutableMap .of ("q" , texts );
77
- HttpRequest httpRequest =
78
- translate
79
- .getRequestFactory ()
80
- .buildPostRequest (
81
- buildTargetUrl ("detect" ),
82
- new JsonHttpContent (translate .getJsonFactory (), content ))
83
- .setParser (translate .getObjectParser ());
84
71
List <List <DetectionsResourceItems >> detections =
85
- httpRequest .execute ().parseAs (DetectionsListResponse .class ).getDetections ();
86
- // TODO use REST apiary as soon as it supports POST
87
- // List<List<DetectionsResourceItems>> detections =
88
- //
89
- // translate.detections().list(texts).setKey(options.getApiKey()).execute().getDetections();
72
+ translate .detections ().list (texts ).setKey (options .getApiKey ()).execute ().getDetections ();
90
73
return detections != null ? detections : ImmutableList .<List <DetectionsResourceItems >>of ();
91
74
} catch (IOException ex ) {
92
75
throw translate (ex );
@@ -96,27 +79,16 @@ public List<List<DetectionsResourceItems>> detect(List<String> texts) {
96
79
@ Override
97
80
public List <LanguagesResource > listSupportedLanguages (Map <Option , ?> optionMap ) {
98
81
try {
99
- Map <String , ?> content =
100
- ImmutableMap .of (
101
- "target" ,
102
- firstNonNull (
103
- Option .TARGET_LANGUAGE .getString (optionMap ), options .getTargetLanguage ()));
104
- HttpRequest httpRequest =
105
- translate
106
- .getRequestFactory ()
107
- .buildPostRequest (
108
- buildTargetUrl ("languages" ),
109
- new JsonHttpContent (translate .getJsonFactory (), content ))
110
- .setParser (translate .getObjectParser ());
111
82
List <LanguagesResource > languages =
112
- httpRequest .execute ().parseAs (LanguagesListResponse .class ).getLanguages ();
113
- // TODO use REST apiary as soon as it supports POST
114
- // List<LanguagesResource> languages = translate.languages()
115
- // .list()
116
- // .setKey(options.getApiKey())
117
- // .setTarget(
118
- // firstNonNull(TARGET_LANGUAGE.getString(optionMap), options.getTargetLanguage()))
119
- // .execute().getLanguages();
83
+ translate
84
+ .languages ()
85
+ .list ()
86
+ .setKey (options .getApiKey ())
87
+ .setTarget (
88
+ firstNonNull (
89
+ Option .TARGET_LANGUAGE .getString (optionMap ), options .getTargetLanguage ()))
90
+ .execute ()
91
+ .getLanguages ();
120
92
return languages != null ? languages : ImmutableList .<LanguagesResource >of ();
121
93
} catch (IOException ex ) {
122
94
throw translate (ex );
0 commit comments