1919import static com .google .common .base .MoreObjects .firstNonNull ;
2020
2121import com .google .api .client .http .GenericUrl ;
22- import com .google .api .client .http .HttpRequest ;
2322import com .google .api .client .http .HttpRequestInitializer ;
2423import com .google .api .client .http .HttpTransport ;
25- import com .google .api .client .http .json .JsonHttpContent ;
2624import com .google .api .client .json .jackson2 .JacksonFactory ;
2725import com .google .api .services .translate .Translate ;
28- import com .google .api .services .translate .model .DetectionsListResponse ;
2926import com .google .api .services .translate .model .DetectionsResourceItems ;
30- import com .google .api .services .translate .model .LanguagesListResponse ;
3127import com .google .api .services .translate .model .LanguagesResource ;
3228import com .google .api .services .translate .model .TranslationsResource ;
3329import com .google .cloud .http .HttpTransportOptions ;
3430import com .google .cloud .translate .TranslateException ;
3531import com .google .cloud .translate .TranslateOptions ;
3632import com .google .common .base .Function ;
3733import com .google .common .collect .ImmutableList ;
38- import com .google .common .collect .ImmutableMap ;
3934import com .google .common .collect .Lists ;
4035import java .io .IOException ;
4136import java .util .List ;
@@ -73,20 +68,8 @@ private GenericUrl buildTargetUrl(String path) {
7368 @ Override
7469 public List <List <DetectionsResourceItems >> detect (List <String > texts ) {
7570 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 ());
8471 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 ();
9073 return detections != null ? detections : ImmutableList .<List <DetectionsResourceItems >>of ();
9174 } catch (IOException ex ) {
9275 throw translate (ex );
@@ -96,27 +79,16 @@ public List<List<DetectionsResourceItems>> detect(List<String> texts) {
9679 @ Override
9780 public List <LanguagesResource > listSupportedLanguages (Map <Option , ?> optionMap ) {
9881 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 ());
11182 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 ();
12092 return languages != null ? languages : ImmutableList .<LanguagesResource >of ();
12193 } catch (IOException ex ) {
12294 throw translate (ex );
0 commit comments