Skip to content

Commit b567fc7

Browse files
committed
Fix base URL normalization and README examples
1 parent bfa56b7 commit b567fc7

24 files changed

Lines changed: 27 additions & 6 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import java.util.Map;
2222

2323
public class Demo {
2424
public static void main(String[] args) throws Exception {
25-
var client = new Client("https://uapis.cn/api/v1", "");
25+
var client = new Client("https://uapis.cn", "YOUR_API_KEY");
2626
var info = client.social().getSocialQqUserinfo(Map.of("qq", "10001"));
2727
System.out.println(info);
2828
}
@@ -54,7 +54,7 @@ import uapi.Client;
5454
import uapi.UapiException;
5555
import java.util.Map;
5656

57-
var client = new Client("https://uapis.cn/api/v1", "");
57+
var client = new Client("https://uapis.cn", "YOUR_API_KEY");
5858

5959
// 成功路径
6060
var result = client.social().getSocialQqUserinfo(Map.of("qq", "10001"));

pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,20 @@
1919
<artifactId>gson</artifactId>
2020
<version>2.11.0</version>
2121
</dependency>
22+
<dependency>
23+
<groupId>org.junit.jupiter</groupId>
24+
<artifactId>junit-jupiter</artifactId>
25+
<version>5.10.2</version>
26+
<scope>test</scope>
27+
</dependency>
2228
</dependencies>
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-surefire-plugin</artifactId>
34+
<version>3.2.5</version>
35+
</plugin>
36+
</plugins>
37+
</build>
2338
</project>

src/main/java/uapi/Client.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ public class Client {
1313
private final OkHttpClient http = new OkHttpClient();
1414
private volatile UapiException.ResponseMeta lastResponseMeta;
1515

16-
public Client(String baseUrl, String token) { this.baseUrl = baseUrl; this.token = token; }
16+
public Client(String baseUrl, String token) { this.baseUrl = normalizeBaseUrl(baseUrl); this.token = token; }
1717

1818
public UapiException.ResponseMeta getLastResponseMeta() { return lastResponseMeta; }
1919

20+
private static String normalizeBaseUrl(String baseUrl) {
21+
var normalized = baseUrl.replaceAll("/+$", "");
22+
return normalized.endsWith("/api/v1") ? normalized.substring(0, normalized.length() - "/api/v1".length()) : normalized;
23+
}
24+
2025
private Object request(String method, String path, Map<String, Object> params, Object body) throws UapiException, IOException {
2126
HttpUrl.Builder url = HttpUrl.parse(baseUrl + path).newBuilder();
2227
if (params != null) for (var e: params.entrySet()) url.addQueryParameter(e.getKey(), String.valueOf(e.getValue()));
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)