Skip to content

Commit 8a0d361

Browse files
Merge pull request #8 from volcengine/release/v1.1.2
Release/v1.1.2
2 parents 20368c5 + 56ab0b3 commit 8a0d361

5 files changed

Lines changed: 93 additions & 106 deletions

File tree

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,31 @@ public static void main(String[] args) throws Exception {
2121
<artifactId>fastjson</artifactId>
2222
<version>1.2.83</version>
2323
</dependency>
24+
<dependency>
25+
<groupId>com.squareup.okhttp3</groupId>
26+
<artifactId>okhttp</artifactId>
27+
<version>4.8.1</version>
28+
</dependency>
29+
```
30+
31+
或者在pom中直接添加依赖:
32+
```xml
33+
<dependency>
34+
<groupId>com.datarangers</groupId>
35+
<artifactId>sdk-openapi-java</artifactId>
36+
<version>{version}</version>
37+
</dependency>
38+
```
39+
{version} 为 `sdk-openapi-java`的版本。
40+
41+
maven 仓库地址为:
42+
```xml
43+
<repositories>
44+
<repository>
45+
<id>bytedance-volcengine</id>
46+
<name>bytedance Volcengine</name>
47+
<url>https://artifact.bytedance.com/repository/Volcengine/</url>
48+
</repository>
49+
</repositories>
50+
51+
```

pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,27 @@
66

77
<groupId>com.datarangers</groupId>
88
<artifactId>sdk-openapi-java</artifactId>
9-
<version>1.1.1</version>
9+
<version>1.1.2</version>
1010
<properties>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1212
<encoding>UTF-8</encoding>
1313
<java.version>1.8</java.version>
1414
<maven.compiler.source>1.8</maven.compiler.source>
1515
<maven.compiler.target>1.8</maven.compiler.target>
1616
</properties>
17+
<distributionManagement>
18+
</distributionManagement>
1719
<dependencies>
1820
<dependency>
1921
<groupId>com.alibaba</groupId>
2022
<artifactId>fastjson</artifactId>
2123
<version>1.2.83</version>
2224
</dependency>
25+
<dependency>
26+
<groupId>com.squareup.okhttp3</groupId>
27+
<artifactId>okhttp</artifactId>
28+
<version>4.8.1</version>
29+
</dependency>
2330
<dependency>
2431
<groupId>junit</groupId>
2532
<artifactId>junit</artifactId>

src/main/java/com/datarangers/sdk/client/Client.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public final String request(String method, String serviceUrl, Map<String, String
7272
if (!Constants.METHOD_ALLODED.contains(method)) {
7373
throw new Client.ClientNotSupportException(Constants.METHOD_NOT_SUPPORT + ":" + method);
7474
}
75-
if (params != null) {
75+
if (params != null && (!params.isEmpty())) {
7676
params = new LinkedHashMap<>(params);
7777
}
7878
String authorization = DslSign.sign(ak, sk, expiration, method, serviceUrl, params, body);
Lines changed: 55 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.datarangers.sdk.requests;
22

33
import com.datarangers.sdk.common.Constants;
4+
import okhttp3.*;
45

56
import java.io.*;
67
import java.net.HttpURLConnection;
@@ -15,99 +16,57 @@
1516
* 一些http请求方法
1617
*/
1718
public class Requests {
19+
private static final Object LOCK = new Object();
20+
private static volatile OkHttpClient okHttpClient;
21+
22+
public static void init(OkHttpClient okHttpClient) {
23+
if (Requests.okHttpClient != null) {
24+
throw new RuntimeException("ok httpclient has init");
25+
}
26+
Requests.okHttpClient = okHttpClient;
27+
}
28+
29+
private static OkHttpClient getHttpClient() {
30+
if (okHttpClient == null) {
31+
synchronized (LOCK) {
32+
if (okHttpClient == null) {
33+
okHttpClient = new OkHttpClient();
34+
}
35+
}
36+
}
37+
return okHttpClient;
38+
}
39+
1840
public static String post(String url, Map<String, String> headers, Map<String, String> params, String body) {
19-
HttpURLConnection connection = null;
2041
try {
2142
if (params != null) {
2243
url += formatParams(params);
2344
}
24-
URL urls = new URL(url);
25-
connection = (HttpURLConnection) urls.openConnection();
26-
//设置请求方式为POST
27-
connection.setRequestMethod("POST");
28-
for (Map.Entry<String, String> entry : headers.entrySet()) {
29-
connection.setRequestProperty(entry.getKey(), entry.getValue());
30-
}
31-
//允许写出
32-
connection.setDoOutput(true);
33-
//允许读入
34-
connection.setDoInput(true);
35-
//不使用缓存
36-
connection.setUseCaches(false);
37-
connection.connect();//连接
38-
if (body != null) {
39-
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
40-
writer.write(body);
41-
writer.close();
42-
}
43-
connection.getResponseCode();
44-
InputStream inputStream = connection.getInputStream();
45-
//将流转换为字符串
46-
String result = convertStreamToString(inputStream);
47-
return result;
48-
} catch (ProtocolException e) {
49-
e.printStackTrace();
50-
return e.toString();
51-
} catch (MalformedURLException e) {
52-
e.printStackTrace();
53-
return e.toString();
45+
return requestMethod("POST", url, body, headers);
5446
} catch (IOException e) {
5547
e.printStackTrace();
5648
return e.toString();
57-
} finally {
58-
if (connection != null) {
59-
connection.disconnect();
60-
}
6149
}
6250
}
6351

6452
public static String get(String url) {
65-
HttpURLConnection connection = null;
6653
try {
67-
URL urls = new URL(url);
68-
connection = (HttpURLConnection) urls.openConnection();
69-
connection.setRequestMethod("GET");
70-
connection.connect();
71-
connection.getResponseCode();
72-
InputStream inputStream = connection.getInputStream();
73-
//将流转换为字符串
74-
String result = convertStreamToString(inputStream);
75-
return result;
54+
return requestGet(url, new HashMap<>());
7655
} catch (Exception e) {
7756
e.printStackTrace();
7857
return e.toString();
79-
} finally {
80-
if (connection != null) {
81-
connection.disconnect();
82-
}
8358
}
8459
}
8560

8661
public static String get(String url, Map<String, String> headers, Map<String, String> params) {
87-
HttpURLConnection connection = null;
8862
try {
8963
if (params != null) {
9064
url += formatParams(params);
9165
}
92-
URL urls = new URL(url);
93-
connection = (HttpURLConnection) urls.openConnection();
94-
connection.setRequestMethod("GET");
95-
for (Map.Entry<String, String> entry : headers.entrySet()) {
96-
connection.setRequestProperty(entry.getKey(), entry.getValue());
97-
}
98-
connection.connect();
99-
connection.getResponseCode();
100-
InputStream inputStream = connection.getInputStream();
101-
//将流转换为字符串
102-
String result = convertStreamToString(inputStream);
103-
return result;
66+
return requestGet(url, headers);
10467
} catch (Exception e) {
10568
e.printStackTrace();
10669
return e.toString();
107-
} finally {
108-
if (connection != null) {
109-
connection.disconnect();
110-
}
11170
}
11271
}
11372

@@ -120,17 +79,6 @@ private static String formatParams(Map<String, String> params) {
12079
}
12180

12281

123-
public static String convertStreamToString(InputStream inputStream) throws IOException {
124-
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
125-
StringBuilder sb = new StringBuilder();
126-
String line;
127-
while ((line = reader.readLine()) != null) {
128-
sb.append(line + System.lineSeparator());
129-
}
130-
String res = sb.toString();
131-
return res;
132-
}
133-
13482
public static String requests(String method, String url, Map<String, String> headers, String body, Map<String, String> params) {
13583
if (Constants.POST.equals(method)) {
13684
return post(url, headers, params, body);
@@ -150,40 +98,44 @@ public static String requests(String method, String url, Map<String, String> hea
15098
* <LI>TRACE
15199
*/
152100
private static String call(String url, Map<String, String> headers, Map<String, String> params, String body, String method) {
153-
HttpURLConnection connection = null;
154101
try {
155102
if (params != null) {
156103
url += formatParams(params);
157104
}
158-
URL urls = new URL(url);
159-
connection = (HttpURLConnection) urls.openConnection();
160-
//设置请求方式为POST
161-
connection.setRequestMethod(method);
162-
headers.forEach(connection::setRequestProperty);
163-
//允许写出
164-
connection.setDoOutput(true);
165-
//允许读入
166-
connection.setDoInput(true);
167-
//不使用缓存
168-
connection.setUseCaches(false);
169-
//连接
170-
connection.connect();
171-
if (body != null) {
172-
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
173-
writer.write(body);
174-
writer.close();
175-
}
176-
connection.getResponseCode();
177-
InputStream inputStream = connection.getInputStream();
178-
//将流转换为字符串。
179-
return convertStreamToString(inputStream);
105+
return requestMethod(method, url, body, headers);
180106
} catch (Exception e) {
181107
e.printStackTrace();
182108
return e.toString();
183-
} finally {
184-
if (connection != null) {
185-
connection.disconnect();
109+
}
110+
}
111+
112+
private static String requestGet(String url, Map<String, String> headers) throws IOException {
113+
Request request = new Request.Builder()
114+
.url(url)
115+
.headers(Headers.of(headers))
116+
.build();
117+
return getResponse(request);
118+
}
119+
120+
private static String requestMethod(String method, String url, String bodyContent,
121+
Map<String, String> headers) throws IOException {
122+
RequestBody body = RequestBody.create(MediaType.parse("charset=utf-8"), bodyContent);
123+
Request request = new Request.Builder()
124+
.url(url)
125+
.headers(Headers.of(headers))
126+
.method(method, body)
127+
.build();
128+
return getResponse(request);
129+
}
130+
131+
private static String getResponse(Request request) throws IOException {
132+
OkHttpClient client = getHttpClient();
133+
try (Response response = client.newCall(request).execute()) {
134+
if (response == null) {
135+
return null;
186136
}
137+
return response.body() == null ? response.toString() : response.body().string();
187138
}
188139
}
140+
189141
}

src/main/java/com/datarangers/sdk/util/DslSign.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static String formatKeyValue(String key, String value) {
5252

5353
private static String canonicalParam(Map<String, String> params) {
5454
String res = "CanonicalQueryString:";
55-
if (params == null) {
55+
if (params == null || params.isEmpty()) {
5656
return res;
5757
}
5858
for (String key : params.keySet()) {

0 commit comments

Comments
 (0)