Skip to content

Commit

Permalink
#33 [feat] : 에러 처리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbang105 committed Jun 17, 2024
1 parent 5000085 commit 790168d
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions backend/src/main/java/org/dgu/backend/service/UpbitApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

import com.nimbusds.jose.shaded.gson.Gson;
import lombok.RequiredArgsConstructor;
import org.dgu.backend.domain.UpbitKey;
import org.dgu.backend.domain.User;
import org.dgu.backend.dto.UpbitDto;
import org.dgu.backend.exception.UpbitErrorResult;
import org.dgu.backend.exception.UpbitException;
import org.dgu.backend.exception.UserErrorResult;
import org.dgu.backend.exception.UserException;
import org.dgu.backend.repository.UpbitKeyRepository;
import org.dgu.backend.util.JwtUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
Expand All @@ -17,10 +24,14 @@
@Component
@RequiredArgsConstructor
public class UpbitApiClient {
@Value("${upbit.url.account}")
private String UPBIT_URL_ACCOUNT;
private final RestTemplate restTemplate;
private final JwtUtil jwtUtil;
private final UpbitKeyRepository upbitKeyRepository;

// HTTP GET 요청을 보내고 결과를 처리하는 메서드
private <T> T sendHttpGetRequest(String url, Class<T> responseType, Optional<String> token) {
public <T> T sendHttpGetRequest(String url, Class<T> responseType, Optional<String> token) {
HttpHeaders headers = new HttpHeaders();
headers.set("accept", MediaType.APPLICATION_JSON_VALUE);
token.ifPresent(t -> headers.add("Authorization", "Bearer " + t));
Expand All @@ -32,16 +43,16 @@ private <T> T sendHttpGetRequest(String url, Class<T> responseType, Optional<Str
new HttpEntity<>(headers),
responseType
);

// HTTP 요청이 성공적으로 처리되었지만, 응답 본문이 null인 경우 예외 처리
if (Objects.isNull(responseEntity.getBody())) {
throw new UpbitException(UpbitErrorResult.FAIL_GET_RESPONSE);
}

return responseEntity.getBody();
} catch (HttpClientErrorException e) {
if (e.getStatusCode() == HttpStatus.UNAUTHORIZED && e.getResponseBodyAsString().contains("no_authorization_ip")) {
throw new UpbitException(UpbitErrorResult.UNAUTHORIZED_IP);
} else {
throw new UpbitException(UpbitErrorResult.UNAUTHORIZED_KEY);
}
UpbitErrorResult errorResult = mapToUpbitErrorResult((HttpStatus) e.getStatusCode(), e.getResponseBodyAsString());
throw new UpbitException(errorResult);
}
}

Expand All @@ -63,12 +74,20 @@ private <T> T sendHttpPostRequest(String url, Class<T> responseType, String toke
}
return responseEntity.getBody();
} catch (HttpClientErrorException e) {
if (e.getStatusCode() == HttpStatus.UNAUTHORIZED && e.getResponseBodyAsString().contains("no_authorization_ip")) {
throw new UpbitException(UpbitErrorResult.UNAUTHORIZED_IP);
} else {
throw new UpbitException(UpbitErrorResult.UNAUTHORIZED_KEY);
}
UpbitErrorResult errorResult = mapToUpbitErrorResult((HttpStatus) e.getStatusCode(), e.getResponseBodyAsString());
throw new UpbitException(errorResult);
}
}

// 유저 업비트 계좌 정보를 조회하는 메서드
public UpbitDto.Account[] getUpbitAccounts(User user) {
UpbitKey upbitKey = upbitKeyRepository.findByUser(user);
if (Objects.isNull(upbitKey)) {
throw new UserException(UserErrorResult.NOT_FOUND_KEY);
}

String token = jwtUtil.generateUpbitToken(upbitKey);
return getUserAccountsAtUpbit(UPBIT_URL_ACCOUNT, token);
}

// 캔들 차트 조회 업비트 API와 통신하는 메서드
Expand All @@ -92,7 +111,19 @@ public UpbitDto.Ticker[] getTickerPriceAtUpbit(String url) {
}

// 주문 생성 업비트 API와 통신하는 메서드
public UpbitDto.OrderResponse[] createNewOrder(String url, String token, Map<String, String> params) {
return sendHttpPostRequest(url, UpbitDto.OrderResponse[].class, token, params);
public UpbitDto.OrderResponse createNewOrder(String url, String token, Map<String, String> params) {
return sendHttpPostRequest(url, UpbitDto.OrderResponse.class, token, params);
}
// HTTP 상태 코드에 따라 UpbitErrorResult를 반환하거나 에러 코드를 출력하는 메서드
private UpbitErrorResult mapToUpbitErrorResult(HttpStatus statusCode, String responseBody) {
for (UpbitErrorResult errorResult : UpbitErrorResult.values()) {
if (errorResult.getHttpStatus() == statusCode && responseBody.contains(errorResult.getCode())) {
return errorResult;
}
}

// 그 외의 경우, 에러 코드만 출력
System.out.println("Upbit error code: " + statusCode.toString() + " " + responseBody);
return null;
}
}

0 comments on commit 790168d

Please sign in to comment.