k line接口调整

This commit is contained in:
Achilles
2024-01-08 21:04:59 +08:00
parent 90c5eb9644
commit bc852cbf7c

View File

@@ -23,11 +23,13 @@ import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
@@ -186,6 +188,8 @@ public class MoneyApiController {
json1.put("VOL",data.getString("VOL"));
json1.put("dataSourceType","3");
json1.put("symbol",data.getString("symbol"));
json1.put("BSEID",data.getString("BSEID"));
json1.put("NSEID",data.getString("NSEID"));
json1.put("LTH",data.getString("LTH"));
json1.put("LTL",data.getString("LTL"));
json1.put("OPN",data.getString("OPN"));
@@ -609,7 +613,7 @@ public class MoneyApiController {
@GetMapping("/api/market/money/history/kLine")
@ApiOperation(value = "获取kline的money数据源", notes = "获取kline的money数据源",response = StockHistoryResponse.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "symbol", value = "Stock symbol 对应的是scId", required = true, dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "symbol", value = "Stock symbol 对应的是NSEID 或者是BSEID", required = true, dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "resolution", value = "单位:60 1D 1W 1D 对应H,D,W,Y", required = true, dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "from", value = "Start timestamp", required = true, dataType = "long", paramType = "query"),
@ApiImplicitParam(name = "to", value = "End timestamp", required = true, dataType = "long", paramType = "query"),
@@ -618,29 +622,78 @@ public class MoneyApiController {
})
@ResponseBody
public ResponseEntity<StockHistoryResponse> getStockHistory( @RequestParam String symbol,
@RequestParam String resolution,
@RequestParam long from,
@RequestParam long to,
@RequestParam int countback,
@RequestParam String currencyCode) {
// 创建一个RestTemplate实例
RestTemplate restTemplate = new RestTemplate();
@RequestParam String resolution
) {
// 向外部API发起请求并获取响应
StockHistoryRequest request = new StockHistoryRequest();
request.setSymbol(symbol);
request.setResolution(resolution);
Long to = null;
Long from = null;
int countback = 5;
if(StringUtils.equals("H",resolution)){
to = (long) (System.currentTimeMillis() / 1000);
from = to - (5 * 24 * 60 * 60 );
countback = 10;
request.setResolution("60");
}else if(StringUtils.equals("D",resolution)){
to = (long) (System.currentTimeMillis() / 1000);
from = to - (10 * 24 * 60 * 60 );
countback = 5;
request.setResolution("1D");
}else if(StringUtils.equals("W",resolution)){
to = (long) (System.currentTimeMillis() / 1000);
from = to - (15 * 24 * 60 * 60 );
countback = 5;
request.setResolution("1W");
}else if(StringUtils.equals("M",resolution)){
to = (long) (System.currentTimeMillis() / 1000);
from = to - (365 * 24 * 60 * 60 );
countback = 10;
request.setResolution("1D");
}
request.setFrom(from);
request.setTo(to);
request.setCountback(countback);
request.setCurrencyCode(currencyCode);
request.setCurrencyCode("INR");
String apiUrl = buildApiUrl(request);
StockHistoryResponse response = restTemplate.getForObject(apiUrl, StockHistoryResponse.class);
log.info("request url:"+apiUrl);
StockHistoryResponse response = null;
int maxRetries = 3;
int retryCount = 0;
while (response == null && retryCount < maxRetries) {
try {
response = restTemplate.getForObject(apiUrl, StockHistoryResponse.class);
} catch (RestClientException e) {
// Log the exception or perform any other error handling
log.error("Error while making API request. Retrying... (Retry count: {})", retryCount + 1);
// Increment the retry count
retryCount++;
// Add some delay before the next retry (you can adjust this as needed)
try {
Thread.sleep(300); // 1 second delay
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
if (response != null) {
// API request successful, return the response
return ResponseEntity.ok(response);
} else {
// All retries failed, return an error response
log.error("Failed to get a successful response after {} retries.", maxRetries);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
// 返回响应
return ResponseEntity.ok(response);
}
private String buildApiUrl(StockHistoryRequest request) {
// 构建外部API的URL
return String.format("%s?symbol=%s&resolution=%s&from=%d&to=%d&countback=%d&currencyCode=%s",