1
zyy
2 days ago 4fefff17528a878d345ff3311c297a66a671b8d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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);
        }
    }
 
}