Skip to content

Commit

Permalink
[FEATURE] naver clova <-> server 감정분석 응답 처리
Browse files Browse the repository at this point in the history
[FEATURE] naver clova <-> server 감정분석 응답 처리
  • Loading branch information
SunYerim authored Jul 2, 2024
2 parents 6289c23 + 4647738 commit c4e21f7
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 23 deletions.
Binary file not shown.
Binary file not shown.
Binary file modified build/libs/sentimentAnalysis-0.0.1-SNAPSHOT-plain.jar
Binary file not shown.
Binary file modified build/libs/sentimentAnalysis-0.0.1-SNAPSHOT.jar
Binary file not shown.
Binary file not shown.
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ public ResponseEntity<AnalysisResponse> findAnalysis(@PathVariable(value = "anal
return ResponseEntity.ok(analysis);
}

@GetMapping
@GetMapping("/list")
public ResponseEntity<List<AnalysisHistory>> findHistoriesByUserId(@RequestHeader("Authorization") String token) {
log.info("token값입니다: " + token);
// userId 조회
UserInfoResponse userInfoResponse = userClient.getUserInfo("Bearer" + token);
UserInfoResponse userInfoResponse = userClient.getUserInfo(token);
UserDTO user = userInfoResponse.getResult();

final List<AnalysisHistory> histories = analysisService.findHistoriesByUserId(user.getId());
log.info("idididid------" +user.getId());

final List<AnalysisHistory> histories = analysisService.findHistoriesByUserId(token);

return ResponseEntity.ok(histories);
}
Expand All @@ -40,8 +43,10 @@ public ResponseEntity<List<AnalysisHistory>> findHistoriesByUserId(@RequestHeade
public ResponseEntity<AnalysisCreatedResponse> createAnalysisSource(@RequestHeader("Authorization") String token,
@RequestBody AnalysisRequest sourceRequest) {


log.info("sources----------" + token);
// userId 조회
UserInfoResponse userInfoResponse = userClient.getUserInfo("Bearer" + token);
UserInfoResponse userInfoResponse = userClient.getUserInfo(token);
UserDTO user = userInfoResponse.getResult();

final AnalysisResponse analysisResponse = analysisService.postAnalysis(user.getUserNo(), sourceRequest, token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "analysis_result")
public class Analysis {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import org.json.JSONObject;
import com.sunjoo.sentimentAnalysis.dto.AnalysisRequest;
import com.sunjoo.sentimentAnalysis.dto.AnalysisResult;
Expand All @@ -18,6 +19,7 @@
import java.io.IOException;

@Component
@Log4j2
public class AnalysisProviderImpl implements AnalysisProvider{
// 텍스트 분석
private static final String TEXT_API_URL = "https://naveropenapi.apigw.ntruss.com/sentiment-analysis/v1/analyze";
Expand Down Expand Up @@ -47,9 +49,14 @@ private AnalysisResult getTextResult(final String text) {
final JSONObject body = new JSONObject();
body.put("content", text);

log.info("Request Body: {}", body.toString());
log.info("Headers: {}", headers);

HttpEntity<String> httpEntity = new HttpEntity<>(body.toString(), headers);
final String result = restTemplate.postForObject(TEXT_API_URL, httpEntity, String.class);

log.info("Response Body: {}", result);

final JSONObject jsonObject = new JSONObject(result);
final JSONObject confidence = jsonObject.getJSONObject("document").getJSONObject("confidence");
final double positive = confidence.getDouble("positive");
Expand All @@ -67,12 +74,9 @@ private HttpHeaders makeHeaders(final MediaType mediaType) {
headers.add(CLIENT_SECRET_HEADER, CLIENT_SECRET);
headers.setContentType(mediaType);

log.info("makeeeeee Headers: {}", headers);
return headers;
}






}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.sunjoo.sentimentAnalysis.entity.Sentiment;
import com.sunjoo.sentimentAnalysis.repository.AnalysisRepository;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -20,6 +21,7 @@
import java.util.stream.Collectors;

@Service
@Log4j2
@AllArgsConstructor
public class AnalysisService {

Expand All @@ -30,7 +32,7 @@ public class AnalysisService {

public List<AnalysisHistory> findHistoriesByUserId(String token) {
// token을 사용하여 userInfo 조회
UserInfoResponse userInfoResponse = userClient.getUserInfo("Bearer " + token);
UserInfoResponse userInfoResponse = userClient.getUserInfo(token);

if (userInfoResponse == null || !"SUCCESS".equals(userInfoResponse.getResultCode())) {
throw new IllegalArgumentException("Invalid token: " + token);
Expand All @@ -55,22 +57,25 @@ public AnalysisResponse findAnalysisById(long id, String token) {
@Transactional
public AnalysisResponse postAnalysis(final Long userId, final AnalysisRequest request, String token) {
// token을 사용하여 userInfo 조회
UserInfoResponse userInfoResponse = userClient.getUserInfo("Bearer " + token);
log.info("post token----- " + token);
UserInfoResponse userInfoResponse = userClient.getUserInfo(token);

if (userInfoResponse == null || !"SUCCESS".equals(userInfoResponse.getResultCode())) {
throw new IllegalArgumentException("Invalid token: " + token);
}

UserDTO user = userInfoResponse.getResult();
// UserDTO user = userInfoResponse.getResult();

try {
final Analysis analysis = analyze(request, token);
log.info("drink token-------- "+ token);
Analysis persistedAnalysis = analysisRepository.save(analysis);

DrinkResponse drinkResponse = drinkClient.getDrinkById(persistedAnalysis.getDrinkId(), "Bearer " + token);
DrinkResponse drinkResponse = drinkClient.getDrinkById(persistedAnalysis.getDrinkId(), token);

return AnalysisResponse.from(persistedAnalysis, drinkResponse);
} catch (final IOException e) {
log.error("IOException during analysis: " + e.getMessage());
throw new IllegalArgumentException("분석 요청 중 문제가 발생했습니다.");
}
}
Expand All @@ -79,7 +84,7 @@ private Analysis analyze(AnalysisRequest request, String token) throws IOExcepti
final Sentiment sentiment = analysisProvider.analyze(request);

// token을 사용하여 userInfo 조회
UserInfoResponse userInfoResponse = userClient.getUserInfo("Bearer " + token);
UserInfoResponse userInfoResponse = userClient.getUserInfo(token);

if (userInfoResponse == null || !"SUCCESS".equals(userInfoResponse.getResultCode())) {
throw new IllegalArgumentException("Invalid token: " + token);
Expand All @@ -89,7 +94,7 @@ private Analysis analyze(AnalysisRequest request, String token) throws IOExcepti

// api요청 보내기
String sentimentValue = sentiment.name();
DrinkResponse drinkResponse = drinkClient.getRecommendedDrink(sentimentValue, "Bearer " + token);
DrinkResponse drinkResponse = drinkClient.getRecommendedDrink(sentimentValue, token);

return Analysis.builder()
.sentiment(sentiment)
Expand Down
10 changes: 1 addition & 9 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,13 @@ spring:
name: sentimentAnalysis
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://sunjoo-sentimentanalysis.clau8wyu484y.ap-northeast-2.rds.amazonaws.com:3306/juryu?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true
url: jdbc:mysql://sunjoo-sentimentanalysis.clau8wyu484y.ap-northeast-2.rds.amazonaws.com:3306/sunjoo?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true
username: root
password: sunjoo-sentimentanalysis0628
jpa:
hibernate:
ddl-auto: validate

drink-datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://sunjoo-drinktionary.clau8wyu484y.ap-northeast-2.rds.amazonaws.com:3306/sunjoo?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true
username: root
password: sunjoo-drinktionary0628
jpa:
hibernate:
ddl-auto: validate

eureka:
instance:
Expand Down

0 comments on commit c4e21f7

Please sign in to comment.