Skip to content

Commit

Permalink
✨ Feat: Adapted to java.net.http built-in http client.
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbings committed Jan 7, 2023
1 parent 94743b8 commit 2cd3e4c
Showing 1 changed file with 90 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import io.hanbings.fluocean.common.interfaces.Serialization;
import lombok.AllArgsConstructor;

import java.io.IOException;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.util.ArrayList;
import java.net.http.HttpResponse;
import java.util.Map;

@AllArgsConstructor
Expand Down Expand Up @@ -58,25 +59,63 @@ public Response get(
Map<String, String> params,
Map<String, String> headers
) {
String[] strings = params.entrySet()
.stream()
.collect(
ArrayList<String>::new,
(l, s) -> {
l.add(s.getKey());
l.add(s.getValue());
},
ArrayList::addAll
)
.toArray(String[]::new);

HttpRequest request = HttpRequest.newBuilder()
// 处理 url 的 params
URI check;
URI target;

try {
// 从旧的 url 创建 URI 对象
check = new URI(url);

StringBuilder strings = new StringBuilder();

// 判断是否已经有 query 参数 如果没有还需要 & 带在 url 尾部
if (check.getQuery() != null) {
strings.append(check.getQuery()).append("&");
}
// 转换参数拼接到 url 尾部
params.forEach((k, v) -> strings.append(k).append("=").append(v).append("&"));

// 构造新 url
target = new URI(
check.getScheme(),
check.getAuthority(),
check.getPath(),
strings.toString(),
check.getFragment()
);
} catch (URISyntaxException e) {
return new OAuthResponse(true, e, 0, null, null);
}

// 为了掺入 headers 参数拆分 builder
HttpRequest.Builder builder = HttpRequest.newBuilder()
.GET()
.uri(URI.create(url))
.headers(strings)
.build();
.uri(target);
// 添加 headers
headers.forEach(builder::header);

// 发出请求
try {
HttpResponse<String> response = client.send(
builder.build(),
HttpResponse.BodyHandlers.ofString()
);

return null;
return new OAuthResponse(
false,
null,
response.statusCode(),
response.body(),
serialization.map(
String.class,
String.class,
response.body()
)
);
} catch (IOException | InterruptedException e) {
return new OAuthResponse(true, e, 0, null, null);
}
}

@Override
Expand All @@ -98,6 +137,38 @@ public Response post(
Map<String, String> form,
Map<String, String> headers
) {
return null;
// 构造表单
StringBuilder strings = new StringBuilder();
// 拼接数据
form.forEach((k, v) -> strings.append(k).append("=").append(v).append("&"));

// 为了掺入 headers 参数拆分 builder
HttpRequest.Builder builder = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(strings.toString()))
.uri(URI.create(url));
// 添加 headers
headers.forEach(builder::header);

// 发出请求
try {
HttpResponse<String> response = client.send(
builder.build(),
HttpResponse.BodyHandlers.ofString()
);

return new OAuthResponse(
false,
null,
response.statusCode(),
response.body(),
serialization.map(
String.class,
String.class,
response.body()
)
);
} catch (IOException | InterruptedException e) {
return new OAuthResponse(true, e, 0, null, null);
}
}
}

0 comments on commit 2cd3e4c

Please sign in to comment.