@Test public void 取得でacquireに翻訳される() { Codic codic = new Codic(); List<Translation> actual = codic.translate("取得"); assertThat(actual).hasSize(1); assertThat(actual.get(0).getTranslatedText()) .isEqualTo("acquisition"); }
public List<Translation> translate(String text) { URL url = new HttpUrl.Builder() .scheme("https") .host("api.codic.jp") .addPathSegment("v1") .addPathSegment("engine") .addPathSegment("translate.json") .addQueryParameter("text", text) .build() .url(); Request request = new Builder() .url(url) .addHeader(CODIC_HTTP_AUTH_HEADER, "Bearer " + accessToken) .get() .build(); try { Response response = new OkHttpClient().newCall(request).execute(); int code = response.code(); if (code != 200) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "] with status " + code + "."); } Genson genson = new GensonBuilder().useMethods(true).create(); String string = response.body().string(); return genson.deserialize(string, new GenericType<List<Translation>>() {}); } catch (IOException e) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "].", e); } }
Codic { public List<Translation> translate(String text) { URL url = new HttpUrl.Builder() .scheme("https") .host("api.codic.jp") .addPathSegment("v1") .addPathSegment("engine") .addPathSegment("translate.json") .addQueryParameter("text", text) .build() .url(); Request request = new Builder() .url(url) .addHeader(CODIC_HTTP_AUTH_HEADER, "Bearer " + accessToken) .get() .build(); try { Response response = new OkHttpClient().newCall(request).execute(); int code = response.code(); if (code != 200) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "] with status " + code + "."); } Genson genson = new GensonBuilder().useMethods(true).create(); String string = response.body().string(); return genson.deserialize(string, new GenericType<List<Translation>>() {}); } catch (IOException e) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "].", e); } } }
Codic { public List<Translation> translate(String text) { URL url = new HttpUrl.Builder() .scheme("https") .host("api.codic.jp") .addPathSegment("v1") .addPathSegment("engine") .addPathSegment("translate.json") .addQueryParameter("text", text) .build() .url(); Request request = new Builder() .url(url) .addHeader(CODIC_HTTP_AUTH_HEADER, "Bearer " + accessToken) .get() .build(); try { Response response = new OkHttpClient().newCall(request).execute(); int code = response.code(); if (code != 200) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "] with status " + code + "."); } Genson genson = new GensonBuilder().useMethods(true).create(); String string = response.body().string(); return genson.deserialize(string, new GenericType<List<Translation>>() {}); } catch (IOException e) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "].", e); } } Codic();  Codic(String accessToken); }
Codic { public List<Translation> translate(String text) { URL url = new HttpUrl.Builder() .scheme("https") .host("api.codic.jp") .addPathSegment("v1") .addPathSegment("engine") .addPathSegment("translate.json") .addQueryParameter("text", text) .build() .url(); Request request = new Builder() .url(url) .addHeader(CODIC_HTTP_AUTH_HEADER, "Bearer " + accessToken) .get() .build(); try { Response response = new OkHttpClient().newCall(request).execute(); int code = response.code(); if (code != 200) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "] with status " + code + "."); } Genson genson = new GensonBuilder().useMethods(true).create(); String string = response.body().string(); return genson.deserialize(string, new GenericType<List<Translation>>() {}); } catch (IOException e) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "].", e); } } Codic();  Codic(String accessToken); List<Translation> translate(String text); }
Codic { public List<Translation> translate(String text) { URL url = new HttpUrl.Builder() .scheme("https") .host("api.codic.jp") .addPathSegment("v1") .addPathSegment("engine") .addPathSegment("translate.json") .addQueryParameter("text", text) .build() .url(); Request request = new Builder() .url(url) .addHeader(CODIC_HTTP_AUTH_HEADER, "Bearer " + accessToken) .get() .build(); try { Response response = new OkHttpClient().newCall(request).execute(); int code = response.code(); if (code != 200) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "] with status " + code + "."); } Genson genson = new GensonBuilder().useMethods(true).create(); String string = response.body().string(); return genson.deserialize(string, new GenericType<List<Translation>>() {}); } catch (IOException e) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "].", e); } } Codic();  Codic(String accessToken); List<Translation> translate(String text);  }