Skip to content

Commit

Permalink
[FIX/#95] 네이버 OCR 응답 추출 메서드로 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
seokbeom00 committed Jul 15, 2024
1 parent ed56260 commit 5d8a446
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
import org.sopt.seonyakServer.global.common.external.naver.dto.OcrBusinessResponse;
Expand All @@ -27,7 +26,6 @@

@Service
@RequiredArgsConstructor
@Slf4j
public class OcrService {
private final OcrConfig ocrConfig;

Expand All @@ -37,8 +35,7 @@ public OcrUnivResponse ocrUniv(MultipartFile file) throws IOException {
String apiUrl = ocrConfig.getUnivUrl();
String apiKey = ocrConfig.getUnivUrlKey();

// 대학교 OCR 응답 문자열로 받아옴
String response = requestNaverOcr(apiUrl, apiKey, file);
String response = getOcrResponse(apiUrl, apiKey, file);

// 네이버 OCR 실패 응답 처리
String responseResult = extractInferResult(response);
Expand All @@ -50,16 +47,12 @@ public OcrUnivResponse ocrUniv(MultipartFile file) throws IOException {

// 명함 OCR
public OcrBusinessResponse ocrBusiness(MultipartFile file) throws IOException {

// OCR 설정파일로부터 URL, Secret Key 가져옴
String apiUrl = ocrConfig.getBusinessUrl();
String apiKey = ocrConfig.getBusinessKey();

String response = requestNaverOcr(apiUrl, apiKey, file);
// 네이버 OCR 실패 응답 처리
String responseResult = extractInferResult(response);
if (responseResult.equals("FAILURE")) {
throw new CustomException(ErrorType.NOT_VALID_OCR_IMAGE);
}
String response = getOcrResponse(apiUrl, apiKey, file);

//회사명, 휴대전화번호 JSON 응답에서 파싱
String company = extractTextByKey(response, "company");
Expand Down Expand Up @@ -209,4 +202,16 @@ private String extractInferResult(String jsonResponse) {
.map(image -> image.getString("inferResult"))
.collect(Collectors.joining(","));
}

private String getOcrResponse(String apiUrl, String apiKey, MultipartFile file) throws IOException {

String response = requestNaverOcr(apiUrl, apiKey, file);
// 네이버 OCR 실패 응답 처리
String responseResult = extractInferResult(response);
if (responseResult.equals("FAILURE")) {
throw new CustomException(ErrorType.NOT_VALID_OCR_IMAGE);
}

return response;
}
}

0 comments on commit 5d8a446

Please sign in to comment.