-
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.
Browse files
Browse the repository at this point in the history
[feat] : 업비트 자동매매 기능 초기 구현
- Loading branch information
Showing
8 changed files
with
224 additions
and
119 deletions.
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
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
97 changes: 97 additions & 0 deletions
97
backend/src/main/java/org/dgu/backend/service/UpbitApiClient.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,97 @@ | ||
package org.dgu.backend.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.dgu.backend.dto.UpbitDto; | ||
import org.dgu.backend.exception.UpbitErrorResult; | ||
import org.dgu.backend.exception.UpbitException; | ||
import org.springframework.http.*; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class UpbitApiClient { | ||
private final RestTemplate restTemplate; | ||
|
||
// HTTP GET 요청을 보내고 결과를 처리하는 메서드 | ||
private <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)); | ||
|
||
try { | ||
ResponseEntity<T> responseEntity = restTemplate.exchange( | ||
url, | ||
HttpMethod.GET, | ||
new HttpEntity<>(headers), | ||
responseType | ||
); | ||
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); | ||
} | ||
} | ||
} | ||
|
||
// HTTP POST 주문 요청을 보내고 결과를 처리하는 메서드 | ||
private <T> T sendHttpPostRequest(String url, Class<T> responseType, String token, Object requestBody) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("accept", MediaType.APPLICATION_JSON_VALUE); | ||
headers.add("Authorization", "Bearer " + token); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
|
||
try { | ||
ResponseEntity<T> responseEntity = restTemplate.exchange( | ||
url, | ||
HttpMethod.POST, | ||
new HttpEntity<>(requestBody, headers), | ||
responseType | ||
); | ||
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); | ||
} | ||
} | ||
} | ||
|
||
// 캔들 차트 조회 업비트 API와 통신하는 메서드 | ||
public UpbitDto.CandleInfoResponse[] getCandleInfoAtUpbit(String url) { | ||
return sendHttpGetRequest(url, UpbitDto.CandleInfoResponse[].class, Optional.empty()); | ||
} | ||
|
||
// 가상화폐 조회 업비트 API와 통신하는 메서드 | ||
public UpbitDto.MarketResponse[] getAllMarketsAtUpbit(String url) { | ||
return sendHttpGetRequest(url, UpbitDto.MarketResponse[].class, Optional.empty()); | ||
} | ||
|
||
// 전체 계좌 조회 업비트 API와 통신하는 메서드 | ||
public UpbitDto.Account[] getUserAccountsAtUpbit(String url, String token) { | ||
return sendHttpGetRequest(url, UpbitDto.Account[].class, Optional.ofNullable(token)); | ||
} | ||
|
||
// 시세 현재가 조회 업비트 API와 통신하는 메서드 | ||
public UpbitDto.Ticker[] getTickerPriceAtUpbit(String url) { | ||
return sendHttpGetRequest(url, UpbitDto.Ticker[].class, Optional.empty()); | ||
} | ||
|
||
// 주문 생성 업비트 API와 통신하는 메서드 | ||
public UpbitDto.OrderResponse[] createNewOrder(String url, String token, UpbitDto.OrderRequest request) { | ||
return sendHttpPostRequest(url, UpbitDto.OrderResponse[].class, token, request); | ||
} | ||
} |
Oops, something went wrong.