package com.yami.trading.huobi.rose;
|
|
import lombok.extern.slf4j.Slf4j;
|
import org.junit.Test;
|
|
import java.util.List;
|
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* @Author: TG:哪吒出海
|
* @Description: 东方财富API测试类
|
*/
|
@Slf4j
|
public class EastmoneyApiTest {
|
|
// 代理配置
|
private static final String PROXY_HOST = "127.0.0.1";
|
private static final int PROXY_PORT = 7890;
|
|
/**
|
* 创建带代理的配置
|
*/
|
private EastmoneyApi.QuoteConfig createProxyConfig(String market, int pageSize, int pageNumber) {
|
return EastmoneyApi.QuoteConfig.builder()
|
.market(market)
|
.pageSize(pageSize)
|
.pageNumber(pageNumber)
|
.useProxy(true)
|
.proxyHost(PROXY_HOST)
|
.proxyPort(PROXY_PORT)
|
.timeout(5000)
|
.build();
|
}
|
|
/**
|
* 创建普通配置
|
*/
|
private EastmoneyApi.QuoteConfig createConfig(String market, int pageSize, int pageNumber) {
|
return EastmoneyApi.QuoteConfig.builder()
|
.market(market)
|
.pageSize(pageSize)
|
.pageNumber(pageNumber)
|
.timeout(5000)
|
.build();
|
}
|
|
/**
|
* 测试获取ETF行情
|
*/
|
@Test
|
public void testETFQuotes() {
|
QuoteResponse response = EastmoneyApi.quotes(createConfig("ETF", 2000, 1));
|
log.info("ETF行情总数: {}", response.getTotal());
|
for (QuoteResponse.QuoteItem item : response.getItems()) {
|
log.info("ETF行情: {}", item);
|
}
|
}
|
|
/**
|
* 测试获取港股行情
|
*/
|
@Test
|
public void testHKQuotes() {
|
QuoteResponse response = EastmoneyApi.quotes(createConfig("HK", 20, 1));
|
log.info("港股行情总数: {}", response.getTotal());
|
for (QuoteResponse.QuoteItem item : response.getItems()) {
|
log.info("港股行情: {}", item);
|
}
|
}
|
|
/**
|
* 测试获取美股行情
|
*/
|
@Test
|
public void testUSQuotes() {
|
QuoteResponse response = EastmoneyApi.quotes(createProxyConfig("US", 20, 1));
|
log.info("美股行情总数: {}", response.getTotal());
|
for (QuoteResponse.QuoteItem item : response.getItems()) {
|
log.info("美股行情: {}", item);
|
}
|
}
|
|
/**
|
* 测试使用代理获取行情
|
*/
|
@Test
|
public void testQuotesWithProxy() {
|
QuoteResponse response = EastmoneyApi.quotes(createProxyConfig("US", 20, 1));
|
log.info("使用代理获取美股行情总数: {}", response.getTotal());
|
for (QuoteResponse.QuoteItem item : response.getItems()) {
|
log.info("使用代理获取美股行情: {}", item);
|
}
|
}
|
|
/**
|
* 测试异步获取多个市场行情
|
*/
|
@Test
|
public void testAsyncQuotes() throws InterruptedException {
|
CountDownLatch latch = new CountDownLatch(3);
|
|
// 异步获取ETF行情
|
CompletableFuture<QuoteResponse> etfFuture = EastmoneyApi.quotesAsync(createConfig("ETF", 20, 1));
|
|
// 异步获取港股行情
|
CompletableFuture<QuoteResponse> hkFuture = EastmoneyApi.quotesAsync(createProxyConfig("HK", 20, 1));
|
|
// 异步获取美股行情
|
CompletableFuture<QuoteResponse> usFuture = EastmoneyApi.quotesAsync(createProxyConfig("US", 20, 1));
|
|
// 等待所有异步任务完成
|
latch.await(10, TimeUnit.SECONDS);
|
}
|
|
/**
|
* 测试分页获取行情
|
*/
|
@Test
|
public void testPaginationQuotes() {
|
QuoteResponse response = EastmoneyApi.quotes(createConfig("ETF", 10, 2));
|
log.info("ETF行情第二页总数: {}", response.getTotal());
|
for (QuoteResponse.QuoteItem item : response.getItems()) {
|
log.info("ETF行情第二页数据: {}", item);
|
}
|
}
|
|
|
/**
|
* 测试获取所有ETF行情数据
|
*/
|
@Test
|
public void testGetAllETFQuotes() {
|
// 先获取第一页,以获取总数
|
QuoteResponse firstPage = EastmoneyApi.quotes(createConfig("ETF", 100, 1));
|
int total = firstPage.getTotal();
|
int pageSize = 100; // 每页100条
|
int totalPages = (total + pageSize - 1) / pageSize; // 向上取整计算总页数
|
|
log.info("ETF行情总数: {}, 总页数: {}", total, totalPages);
|
|
// 处理第一页数据
|
List<QuoteResponse.QuoteItem> allItems = firstPage.getItems();
|
log.info("获取第1页,数据条数: {}", allItems.size());
|
|
// 获取剩余页数据
|
for (int page = 2; page <= totalPages; page++) {
|
QuoteResponse response = EastmoneyApi.quotes(createConfig("ETF", pageSize, page));
|
allItems.addAll(response.getItems());
|
log.info("获取第{}页,累计数据条数: {}", page, allItems.size());
|
}
|
|
log.info("获取完成,总共获取{}条ETF行情数据", allItems.size());
|
|
// 可以在这里处理所有数据
|
for (QuoteResponse.QuoteItem item : allItems) {
|
log.info("ETF行情: {}", item);
|
}
|
}
|
|
}
|