-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Dev-backend' into feat/#105-debate-complete
- Loading branch information
Showing
9 changed files
with
208 additions
and
1 deletion.
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
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
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
18 changes: 18 additions & 0 deletions
18
...c/main/java/com/rollthedice/backend/domain/debate/dto/response/DebateSummaryResponse.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,18 @@ | ||
package com.rollthedice.backend.domain.debate.dto.response; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class DebateSummaryResponse { | ||
Long roomId; | ||
|
||
@Schema(description = "4문장의 요약된 토론 내용") | ||
String summary; | ||
} |
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
119 changes: 119 additions & 0 deletions
119
backend/core/src/main/java/com/rollthedice/backend/domain/debate/service/ClovaSummary.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,119 @@ | ||
package com.rollthedice.backend.domain.debate.service; | ||
|
||
import com.rollthedice.backend.global.error.ErrorCode; | ||
import com.rollthedice.backend.global.error.exception.ExternalApiException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.minidev.json.JSONObject; | ||
import org.apache.tomcat.util.json.JSONParser; | ||
import org.apache.tomcat.util.json.ParseException; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.LinkedHashMap; | ||
|
||
@Slf4j | ||
@Service | ||
public class ClovaSummary { | ||
private static final String KOREAN = "ko"; | ||
private static final int POLITE_TONE = 2; | ||
private static final int SUMMARY_COUNT = 4; | ||
|
||
private static final String API_URL = "https://naveropenapi.apigw.ntruss.com/text-summary/v1/summarize"; | ||
|
||
|
||
@Value("${clova.secret-key}") | ||
private String SECRET; | ||
|
||
@Value("${clova.client-id}") | ||
private String CLIENT_ID; | ||
|
||
public String summaryDebate(String messages) { | ||
log.info("요약할 메세지: {}" ,messages); | ||
try { | ||
URL url = new URL(API_URL); | ||
HttpURLConnection connection = createRequestHeader(url); | ||
createRequestBody(connection, messages); | ||
|
||
log.info("정상1"); | ||
StringBuilder response = getResponse(connection); | ||
log.info("정상2"); | ||
return parseResponse(response); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
throw new ExternalApiException(ErrorCode.CLOVA_API_ERROR); | ||
} | ||
} | ||
|
||
private HttpURLConnection createRequestHeader(URL url) throws IOException { | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setDoInput(true); | ||
connection.setDoOutput(true); | ||
connection.setRequestMethod("POST"); | ||
connection.setRequestProperty("Content-Type", "application/json;"); | ||
connection.setRequestProperty("X-NCP-APIGW-API-KEY-ID", CLIENT_ID); | ||
connection.setRequestProperty("X-NCP-APIGW-API-KEY", SECRET); | ||
return connection; | ||
} | ||
|
||
private void createRequestBody(HttpURLConnection connection, String content) throws IOException { | ||
JSONObject document = new JSONObject(); | ||
document.put("content", content); | ||
|
||
JSONObject option = new JSONObject(); | ||
option.put("language", KOREAN); | ||
option.put("tone", POLITE_TONE); | ||
option.put("summaryCount", SUMMARY_COUNT); | ||
|
||
JSONObject requestObject = new JSONObject(); | ||
requestObject.put("document", document); | ||
requestObject.put("option", option); | ||
|
||
connection.connect(); | ||
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); | ||
outputStream.write(requestObject.toString().getBytes(StandardCharsets.UTF_8)); | ||
outputStream.flush(); | ||
outputStream.close(); | ||
} | ||
|
||
|
||
private StringBuilder getResponse(HttpURLConnection connection) throws IOException { | ||
BufferedReader reader = checkResponse(connection); | ||
String line; | ||
StringBuilder response = new StringBuilder(); | ||
while ((line = reader.readLine()) != null) { | ||
response.append(line); | ||
} | ||
reader.close(); | ||
return response; | ||
} | ||
|
||
private BufferedReader checkResponse(HttpURLConnection connection) throws IOException { | ||
int responseCode = connection.getResponseCode(); | ||
|
||
return getResponseResult(connection, responseCode); | ||
} | ||
|
||
private BufferedReader getResponseResult(HttpURLConnection connection, int responseCode) throws IOException { | ||
if (HttpStatusCode.valueOf(responseCode).is2xxSuccessful()) { | ||
return new BufferedReader(new InputStreamReader(connection.getInputStream())); | ||
} | ||
log.error("Clova Api error response code: {}", responseCode); | ||
return new BufferedReader(new InputStreamReader(connection.getErrorStream())); | ||
} | ||
|
||
|
||
private String parseResponse(StringBuilder response) throws ParseException { | ||
JSONParser parser = new JSONParser(response.toString()); | ||
LinkedHashMap<String, String> hashMap = (LinkedHashMap<String, String>) parser.parse(); | ||
JSONObject parsed = new JSONObject(hashMap); | ||
return parsed.get("summary").toString(); | ||
} | ||
} |
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
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
Submodule scoop-core-config
updated
from 50728d to 51a4b2