Skip to content

Commit

Permalink
🔨 refactor: googleTranslateRestTemplate connection timeout 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
choihuk committed Mar 31, 2024
1 parent b8475d2 commit f8e24da
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package tipitapi.drawmytoday.common.config;

import java.time.Duration;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -35,4 +37,12 @@ public RestTemplate karloRestTemplate() {
});
return restTemplate;
}

@Bean
public RestTemplate googleTranslateRestTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
.setConnectTimeout(Duration.ofMillis(5000))
.setReadTimeout(Duration.ofMillis(5000))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
@RequiredArgsConstructor
public class GoogleTranslatorService implements TranslateTextService {

private final RestTemplate restTemplate = new RestTemplate();
private final RestTemplate googleTranslateRestTemplate;
private final ObjectMapper objectMapper;
private static final String GOOGLE_TRANSLATE_URL = "https://translate.googleapis.com/translate_a/single";

@Override
public String translateAutoToEnglish(String text) {
String translatedText = null;
try {
String jsonResult = restTemplate.getForObject(
String jsonResult = googleTranslateRestTemplate.getForObject(
GOOGLE_TRANSLATE_URL + "?client=gtx&sl=auto&tl=en&dt=t&dt=bd&dj=1&source=icon&q="
+ text, String.class);
JsonNode rootNode = objectMapper.readTree(jsonResult);
Expand Down

0 comments on commit f8e24da

Please sign in to comment.