Skip to content

Commit

Permalink
Merge pull request #172 from Kusitms-29th-MeetUp-H/feat/171-windyflo
Browse files Browse the repository at this point in the history
[feat]: Main translate ai api 구현
  • Loading branch information
qogustj authored Jul 29, 2024
2 parents 6a9d930 + 9f2fb8b commit 2a381b8
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Main/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ dependencies {
//mongoDB
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'

//webclient
implementation 'org.springframework.boot:spring-boot-starter-webflux'

// Test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.0'
Expand Down
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);
}
}
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);
}
}
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
) {
}
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);
}
}
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();
}
}
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
}
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();
}
}

0 comments on commit 2a381b8

Please sign in to comment.