-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from Kusitms-29th-MeetUp-H/feat/171-windyflo
[feat]: Main translate ai api 구현
- Loading branch information
Showing
8 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
Main/src/main/java/com/kusitms29/backendH/api/translate/controller/TranslateController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.kusitms29.backendH.api.translate.controller; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.kusitms29.backendH.api.translate.service.TranslateService; | ||
import com.kusitms29.backendH.api.translate.service.dto.TranslateReqDto; | ||
import com.kusitms29.backendH.api.translate.service.dto.TranslateResDto; | ||
import com.kusitms29.backendH.global.common.SuccessResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/translate") | ||
public class TranslateController { | ||
private final TranslateService translateService; | ||
@PostMapping | ||
public ResponseEntity<SuccessResponse<?>> translate(@RequestBody TranslateReqDto translateReqDto) throws JsonProcessingException { | ||
TranslateResDto translateResDto = translateService.translateMessage(translateReqDto); | ||
return SuccessResponse.ok(translateResDto); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Main/src/main/java/com/kusitms29/backendH/api/translate/service/TranslateService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.kusitms29.backendH.api.translate.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.kusitms29.backendH.api.translate.service.dto.TranslateReqDto; | ||
import com.kusitms29.backendH.api.translate.service.dto.TranslateResDto; | ||
import com.kusitms29.backendH.infra.external.windyflo.WindyfloClient; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class TranslateService { | ||
private final WindyfloClient windyfloClient; | ||
private final ObjectMapper objectMapper; | ||
|
||
public TranslateResDto translateMessage(TranslateReqDto translateReqDto) throws JsonProcessingException { | ||
JsonNode message = windyfloClient.translateMessage(translateReqDto); | ||
return extractReviewGitRes(message); | ||
} | ||
public TranslateResDto extractReviewGitRes(Object input) throws JsonProcessingException { | ||
JsonNode rootNode = objectMapper.readTree(input.toString()); | ||
JsonNode jsonNode = rootNode.get("json"); | ||
String message = jsonNode.get("message").asText(); | ||
return TranslateResDto.of(message); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
Main/src/main/java/com/kusitms29/backendH/api/translate/service/dto/TranslateReqDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.kusitms29.backendH.api.translate.service.dto; | ||
|
||
public record TranslateReqDto( | ||
String language, | ||
String type, | ||
String message | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
Main/src/main/java/com/kusitms29/backendH/api/translate/service/dto/TranslateResDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.kusitms29.backendH.api.translate.service.dto; | ||
|
||
public record TranslateResDto( | ||
String message | ||
) { | ||
public static TranslateResDto of(String message){ | ||
return new TranslateResDto(message); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Main/src/main/java/com/kusitms29/backendH/infra/config/WebClientConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.kusitms29.backendH.infra.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
@Configuration | ||
public class WebClientConfig { | ||
@Bean | ||
public WebClient webClient() { | ||
return WebClient.builder().build(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Main/src/main/java/com/kusitms29/backendH/infra/external/windyflo/QuestionRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.kusitms29.backendH.infra.external.windyflo; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class QuestionRequest { | ||
private String question; | ||
|
||
public QuestionRequest(String question) { | ||
this.question = question; | ||
} | ||
|
||
// getter and setter | ||
} |
32 changes: 32 additions & 0 deletions
32
Main/src/main/java/com/kusitms29/backendH/infra/external/windyflo/WindyfloClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.kusitms29.backendH.infra.external.windyflo; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.kusitms29.backendH.api.translate.service.dto.TranslateReqDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class WindyfloClient { | ||
private final String AI_TRANSLATE_URL ="https://windyflo.com/api/v1/prediction/46a6c57e-9971-4e59-9745-1de6777caba4"; | ||
private final WebClient webClient; | ||
private final ObjectMapper objectMapper; | ||
|
||
public JsonNode translateMessage(TranslateReqDto translateReqDto){ | ||
ObjectNode requestBody = objectMapper.createObjectNode(); | ||
requestBody.put("language", translateReqDto.language()); | ||
requestBody.put("type", translateReqDto.type()); | ||
requestBody.put("message", translateReqDto.message()); | ||
QuestionRequest questionRequest = new QuestionRequest(requestBody.toString()); | ||
|
||
return webClient.post() | ||
.uri(AI_TRANSLATE_URL) | ||
.bodyValue(questionRequest) | ||
.retrieve() | ||
.bodyToMono(JsonNode.class) | ||
.block(); | ||
} | ||
} |