Skip to content

Commit c3d9bc1

Browse files
committed
fix RestClient toUrl bug
1 parent 200c8e8 commit c3d9bc1

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/RestClient.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ private String toUrl(String api) {
4848
}
4949

5050
public String get(String api, HttpHeaders headers, MultiValueMap<String, String> requestParams) {
51-
Request request = getGetRequest(toUrl(api), headers, requestParams);
51+
String url = toUrl(api);
52+
Request request = getGetRequest(url, headers, requestParams);
5253
for (int i = 0; i < retryCount; i++) {
5354
try {
5455
return request.execute();
@@ -77,11 +78,11 @@ public String get(String api, HttpHeaders headers) {
7778
}
7879

7980
public Request getGetRequest(String api, HttpHeaders headers, MultiValueMap<String, String> requestParams) {
80-
return httpRequest.getGetRequest(toUrl(api), headers, requestParams);
81+
return httpRequest.getGetRequest(api, headers, requestParams);
8182
}
8283

8384
public Request getPostRequest(String api, HttpHeaders headers, JSON requestBody) {
84-
return httpRequest.getPostRequest(toUrl(api), headers, requestBody);
85+
return httpRequest.getPostRequest(api, headers, requestBody);
8586
}
8687

8788
public String post(String api, JSON requestBody) {
@@ -93,7 +94,8 @@ public String post(String api, RestParamBuilder paramBuilder) {
9394
}
9495

9596
public String post(String api, HttpHeaders headers, JSON requestBody) {
96-
Request request = getPostRequest(api, headers, requestBody);
97+
String url = toUrl(api);
98+
Request request = getPostRequest(url, headers, requestBody);
9799
for (int i = 0; i < retryCount; i++) {
98100
try {
99101
return request.execute();

springboot-starter/src/test/java/com/codingapi/springboot/framework/rest/RestClientTest.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,33 @@
22

33
import com.alibaba.fastjson.JSONObject;
44
import com.codingapi.springboot.framework.rest.param.RestParamBuilder;
5+
import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
56
import lombok.extern.slf4j.Slf4j;
67
import org.junit.jupiter.api.Test;
78

9+
import java.net.Proxy;
10+
811
import static org.junit.jupiter.api.Assertions.assertEquals;
9-
import static org.junit.jupiter.api.Assertions.assertTrue;
1012

1113
@Slf4j
1214
class RestClientTest {
1315

1416
@Test
15-
void baikeTest() {
16-
String baseUrl = "http://baike.baidu.com/";
17-
RestClient restClient = new RestClient(baseUrl);
18-
String response = restClient.get("/api/openapi/BaikeLemmaCardApi", RestParamBuilder.create()
19-
.add("scope","103").add("format","json")
20-
.add("appid","379020").add("bk_key","关键字")
17+
void okxTest() {
18+
String baseUrl = "https://www.okx.com/";
19+
HttpProxyProperties proxyProperties = new HttpProxyProperties();
20+
proxyProperties.setEnableProxy(true);
21+
proxyProperties.setProxyType(Proxy.Type.HTTP);
22+
proxyProperties.setProxyHost("127.0.0.1");
23+
proxyProperties.setProxyPort(7890);
24+
RestClient restClient = new RestClient(proxyProperties,baseUrl,5,"{}",null,null);
25+
String response = restClient.get("api/v5/market/candles", RestParamBuilder.create()
26+
.add("instId","BTC-USDT")
27+
.add("bar","1m")
28+
.add("limit","300")
2129
);
2230
log.info("response:{}",response);
2331
JSONObject jsonObject = JSONObject.parseObject(response);
24-
log.info("desc:{}",jsonObject.getString("desc"));
25-
assertEquals(jsonObject.getString("key"),"关键字");
26-
assertTrue(response.contains("id"));
32+
assertEquals(jsonObject.getJSONArray("data").size(),300);
2733
}
2834
}

0 commit comments

Comments
 (0)