728x90
package com.example.investment.home.fluctuation.controller.dto.response;
public record FluctuationDTO(
String stockName, //HTS 한글 종목명
int rank, //데이터(등락률) 순위
int currentPrice, //현재가
int prevChangePrice, //전일 대비
String prevSign, //전일 대비 부호
Double prevChangeRate //전일 대비율
) {
}
package com.example.investment.home.fluctuation.controller;
import com.example.investment.home.fluctuation.controller.dto.response.FluctuationDTO;
import com.example.investment.home.fluctuation.service.FluctuationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("home/fluctuation")
public class FluctuationController {
private final FluctuationService fluctuationService;
public FluctuationController(FluctuationService fluctuationService) {
this.fluctuationService = fluctuationService;
}
@GetMapping
public List<FluctuationDTO> getFluctuation() throws IOException {
return fluctuationService.getFluctuation();
}
}
package com.example.investment.home.fluctuation.infrastructor;
import com.example.investment.home.fluctuation.controller.dto.response.FluctuationDTO;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Component
public class FluctuationParser {
private final ObjectMapper objectMapper;
public FluctuationParser(final ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
public List<FluctuationDTO> getFluctuation(String responseBody) throws IOException {
JsonNode rootNode = objectMapper.readTree(responseBody);
JsonNode items = rootNode.path("output");
List<FluctuationDTO> fluctuationDTOList = new ArrayList<>();
Iterator<JsonNode> elements = items.elements();
int count = 0;
while (elements.hasNext() && count < 5) {
JsonNode fluctuationItem = elements.next();
String stockName = fluctuationItem.path("hts_kor_isnm").asText();
int rank = Integer.parseInt(fluctuationItem.path("data_rank").asText());
int currentPrice = Integer.parseInt(fluctuationItem.path("stck_prpr").asText());
int prevChangePrice = Integer.parseInt(fluctuationItem.path("prdy_vrss").asText());
String prevSign = fluctuationItem.path("prdy_vrss_sign").asText();
Double prevChangeRate = Double.valueOf(fluctuationItem.path("prdy_ctrt").asText());
fluctuationDTOList.add(new FluctuationDTO(stockName, rank, currentPrice, prevChangePrice, prevSign, prevChangeRate));
count++;
}
return fluctuationDTOList;
}
}
package com.example.investment.home.fluctuation.service.client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class FluctuationDataFetcher {
private RestTemplate restTemplate;
@Value("${api.app_key}")
private String appKey;
@Value("${api.app_secret}")
private String appSecret;
@Value("${fluctuation.tr_id}")
private String trId;
@Value("${api.access_token}")
private String accessToken;
public FluctuationDataFetcher(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> fluctuationData() {
String url = "https://openapi.koreainvestment.com:9443/uapi/domestic-stock/v1/ranking/fluctuation?"
+ "fid_cond_mrkt_div_code=J&"
+ "fid_cond_scr_div_code=20170&"
+ "fid_input_iscd=0000&"
+ "fid_rank_sort_cls_code=0&"
+ "fid_input_cnt_1=0&"
+ "fid_prc_cls_code=0&"
+ "fid_input_price_1=&"
+ "fid_input_price_2=&"
+ "fid_vol_cnt=&"
+ "fid_trgt_cls_code=0&"
+ "fid_trgt_exls_cls_code=0&"
+ "fid_div_cls_code=0&"
+ "fid_rsfl_rate1=&"
+ "fid_rsfl_rate2=";
HttpHeaders headers = new HttpHeaders();
headers.set("tr_id", trId);
headers.set("appsecret", appSecret);
headers.set("appkey", appKey);
headers.set("Authorization", "Bearer " + accessToken);
headers.set("Content-Type", "application/json");
HttpEntity<String> entity = new HttpEntity<>(headers);
return restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
}
}
package com.example.investment.home.fluctuation.service;
import com.example.investment.home.fluctuation.controller.dto.response.FluctuationDTO;
import com.example.investment.home.fluctuation.infrastructor.FluctuationParser;
import com.example.investment.home.fluctuation.service.client.FluctuationDataFetcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.util.List;
@Service
@Transactional
public class FluctuationService {
private final FluctuationDataFetcher fluctuationDataFetcher;
private final FluctuationParser fluctuationParser;
@Autowired
public FluctuationService(FluctuationDataFetcher fluctuationDataFetcher, FluctuationParser fluctuationParser) {
this.fluctuationDataFetcher = fluctuationDataFetcher;
this.fluctuationParser=fluctuationParser;
}
public List<FluctuationDTO> getFluctuation() throws IOException {
ResponseEntity<String> response = fluctuationDataFetcher.fluctuationData();
return fluctuationParser.getFluctuation(response.getBody());
}
}
728x90
'프로젝트 > 프로젝트' 카테고리의 다른 글
| 검색화면API (2) | 2024.09.14 |
|---|---|
| 홈화면 API (0) | 2024.09.14 |
| 증권 플랫폼 API를 가공해서 DTO를 작성한다. (1) | 2024.09.12 |
| 요구사항정의서 (0) | 2024.07.21 |
| 프로젝트를 위한 CRUD 공부 (0) | 2024.07.03 |