Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f073b8d7ad | ||
|
|
a1b6bf62df | ||
|
|
21c19af696 | ||
|
|
fc04d50d0f | ||
|
|
515387dd32 | ||
|
|
9fb406427f | ||
|
|
386fded0a3 | ||
|
|
88fdc79c82 | ||
|
|
571ed377a8 | ||
|
|
6b84af1f16 | ||
|
|
4302084b95 | ||
|
|
4f8054dc94 | ||
|
|
5c1e1ee52c | ||
|
|
01074e1ac2 | ||
|
|
1b60210c06 | ||
|
|
2290ac5ecf | ||
|
|
6101e8d462 | ||
|
|
aaea3a218b |
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.imyeyu.network</groupId>
|
||||
<artifactId>timi-network</artifactId>
|
||||
<version>0.0.14</version>
|
||||
<version>0.0.13</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
package com.imyeyu.network;
|
||||
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.CallbackArg;
|
||||
import org.apache.hc.client5.http.fluent.ContentResponseHandler;
|
||||
import org.apache.hc.client5.http.fluent.Request;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
|
||||
import org.apache.hc.core5.util.Timeout;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -20,9 +16,6 @@ public class CommonRequest {
|
||||
|
||||
protected final Request request;
|
||||
|
||||
/// 响应回调
|
||||
protected CallbackArg<HttpResponse> responseCallback;
|
||||
|
||||
protected CommonRequest(Request request) {
|
||||
this.request = request;
|
||||
}
|
||||
@@ -61,22 +54,13 @@ public class CommonRequest {
|
||||
return header("Accept-Language", langHeader);
|
||||
}
|
||||
|
||||
/// 设置响应回调,回调在响应体读取前执行
|
||||
///
|
||||
/// @param callback 响应回调,传入 null 表示移除回调
|
||||
/// @return 当前请求
|
||||
public CommonRequest responseCallback(CallbackArg<HttpResponse> callback) {
|
||||
this.responseCallback = callback;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommonRequest bodyEntity(HttpEntity entity) {
|
||||
request.body(entity);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String asString() throws IOException {
|
||||
return handleResponse(response -> new ContentResponseHandler().handleResponse(response).asString());
|
||||
return request.execute().returnContent().asString();
|
||||
}
|
||||
|
||||
public Number asNumber() throws IOException {
|
||||
@@ -84,29 +68,14 @@ public class CommonRequest {
|
||||
}
|
||||
|
||||
public byte[] asBytes() throws IOException {
|
||||
return handleResponse(response -> new ContentResponseHandler().handleResponse(response).asBytes());
|
||||
return request.execute().returnContent().asBytes();
|
||||
}
|
||||
|
||||
public InputStream asStream() throws IOException {
|
||||
return handleResponse(response -> new ContentResponseHandler().handleResponse(response).asStream());
|
||||
return request.execute().returnContent().asStream();
|
||||
}
|
||||
|
||||
public void execute() throws IOException {
|
||||
handleResponse(response -> null);
|
||||
}
|
||||
|
||||
/// 使用统一入口处理响应,子类的自定义响应处理必须调用此方法
|
||||
///
|
||||
/// @param handler 响应处理器
|
||||
/// @param <T> 返回值类型
|
||||
/// @return 响应处理结果
|
||||
/// @throws IOException 请求或响应处理失败
|
||||
protected <T> T handleResponse(HttpClientResponseHandler<T> handler) throws IOException {
|
||||
return request.execute().handleResponse(response -> {
|
||||
if (responseCallback != null) {
|
||||
responseCallback.handler(response);
|
||||
}
|
||||
return handler.handleResponse(response);
|
||||
});
|
||||
request.execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.imyeyu.network;
|
||||
|
||||
import com.imyeyu.io.IO;
|
||||
import com.imyeyu.java.bean.CallbackArg;
|
||||
import org.apache.hc.client5.http.fluent.Request;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
@@ -60,12 +59,6 @@ public class FileRequest extends JacksonRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileRequest responseCallback(CallbackArg<HttpResponse> callback) {
|
||||
super.responseCallback(callback);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileRequest body(Object object) {
|
||||
super.body(object);
|
||||
@@ -91,7 +84,7 @@ public class FileRequest extends JacksonRequest {
|
||||
}
|
||||
|
||||
public long length() throws IOException, ProtocolException {
|
||||
return handleResponse(response -> {
|
||||
HttpResponse response = request.execute().returnResponse();
|
||||
final int status = response.getCode();
|
||||
if (status < 200 || 300 <= status) {
|
||||
throw new IOException("HTTP error status: " + status);
|
||||
@@ -101,6 +94,5 @@ public class FileRequest extends JacksonRequest {
|
||||
throw new IOException("Content-Length header missing");
|
||||
}
|
||||
return Long.parseLong(contentLengthHeader.getValue());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,11 +8,9 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.imyeyu.java.bean.CallbackArg;
|
||||
import org.apache.hc.client5.http.fluent.Request;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
@@ -117,12 +115,6 @@ public class JacksonRequest extends CommonRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JacksonRequest responseCallback(CallbackArg<HttpResponse> callback) {
|
||||
super.responseCallback(callback);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 JSON 请求体
|
||||
*
|
||||
|
||||
@@ -2,10 +2,9 @@ package com.imyeyu.network;
|
||||
|
||||
import com.imyeyu.io.IO;
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.CallbackArg;
|
||||
import org.apache.hc.client5.http.fluent.Request;
|
||||
import org.apache.hc.client5.http.fluent.Response;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
|
||||
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||
|
||||
@@ -67,12 +66,6 @@ public class ProgressiveRequest extends FileRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProgressiveRequest responseCallback(CallbackArg<HttpResponse> callback) {
|
||||
super.responseCallback(callback);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProgressiveRequest body(Object object) {
|
||||
super.body(object);
|
||||
@@ -87,12 +80,12 @@ public class ProgressiveRequest extends FileRequest {
|
||||
|
||||
@Override
|
||||
public void toFile(Path outputPath) throws IOException, NoPermissionException {
|
||||
processResponse(IO.getOutputStream(outputPath.toFile()));
|
||||
processResponse(request.execute(), IO.getOutputStream(outputPath.toFile()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toFile(File file) throws IOException, NoPermissionException {
|
||||
processResponse(IO.getOutputStream(file));
|
||||
processResponse(request.execute(), IO.getOutputStream(file));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -103,17 +96,17 @@ public class ProgressiveRequest extends FileRequest {
|
||||
@Override
|
||||
public byte[] asBytes() throws IOException {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
processResponse(os);
|
||||
processResponse(request.execute(), os);
|
||||
return os.toByteArray();
|
||||
}
|
||||
|
||||
private void processResponse(OutputStream os) throws IOException {
|
||||
handleResponse((HttpClientResponseHandler<Void>) response -> {
|
||||
HttpEntity entity = response.getEntity();
|
||||
private void processResponse(Response response, OutputStream os) throws IOException {
|
||||
response.handleResponse((HttpClientResponseHandler<Void>) resp -> {
|
||||
HttpEntity entity = resp.getEntity();
|
||||
if (entity == null) {
|
||||
throw new IOException("not found response entity");
|
||||
}
|
||||
int code = response.getCode();
|
||||
int code = resp.getCode();
|
||||
if (400 <= code) {
|
||||
throw new IOException("response error: %s".formatted(code));
|
||||
}
|
||||
|
||||
@@ -2,11 +2,9 @@ package com.imyeyu.network;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.imyeyu.java.bean.CallbackArg;
|
||||
import com.imyeyu.java.bean.timi.TimiResponse;
|
||||
import org.apache.hc.client5.http.fluent.Request;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
@@ -57,12 +55,6 @@ public class TimiRequest extends JacksonRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiRequest responseCallback(CallbackArg<HttpResponse> callback) {
|
||||
super.responseCallback(callback);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiRequest body(Object object) {
|
||||
super.body(object);
|
||||
|
||||
Reference in New Issue
Block a user