Compare commits
16
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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.11</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
@@ -18,20 +18,6 @@
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.13.0</version>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.46</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.imyeyu.network;
|
||||
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.utils.Encoder;
|
||||
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.hc.client5.http.fluent.Form;
|
||||
@@ -30,7 +29,7 @@ public class ArgMap<K, V> {
|
||||
private final List<Map.Entry<K, V>> entries = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 追加一个参数
|
||||
* 追加一个参数。
|
||||
*
|
||||
* @param key 参数名
|
||||
* @param value 参数值
|
||||
@@ -42,7 +41,7 @@ public class ArgMap<K, V> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 按插入顺序遍历全部参数
|
||||
* 按插入顺序遍历全部参数。
|
||||
*
|
||||
* @param consumer 处理逻辑
|
||||
*/
|
||||
@@ -51,29 +50,29 @@ public class ArgMap<K, V> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 按插入顺序遍历全部非空参数
|
||||
* 按插入顺序遍历全部非空参数。
|
||||
*
|
||||
* @param consumer 处理逻辑
|
||||
*/
|
||||
private void forEachNonNull(BiConsumer<? super K, ? super V> consumer) {
|
||||
entries.forEach(entry -> {
|
||||
if (TimiJava.isNotEmpty(entry.getValue())) {
|
||||
if (entry.getValue() != null) {
|
||||
consumer.accept(entry.getKey(), entry.getValue());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否存在可输出的非空参数
|
||||
* 是否存在可输出的非空参数。
|
||||
*
|
||||
* @return true 为存在非空参数
|
||||
* @return 是否存在非空参数
|
||||
*/
|
||||
private boolean hasNonNullValue() {
|
||||
return entries.stream().anyMatch(entry -> TimiJava.isNotEmpty(entry.getValue()));
|
||||
return entries.stream().anyMatch(entry -> entry.getValue() != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数数量
|
||||
* 返回参数数量。
|
||||
*
|
||||
* @return 参数数量
|
||||
*/
|
||||
@@ -82,21 +81,23 @@ public class ArgMap<K, V> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断参数列表是否为空
|
||||
* 判断参数列表是否为空。
|
||||
*
|
||||
* @return true 为空
|
||||
* @return 是否为空
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return entries.isEmpty();
|
||||
}
|
||||
|
||||
/** 清空全部参数 */
|
||||
/**
|
||||
* 清空全部参数。
|
||||
*/
|
||||
public void clear() {
|
||||
entries.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回只读参数视图
|
||||
* 返回只读参数视图。
|
||||
*
|
||||
* @return 参数条目列表
|
||||
*/
|
||||
@@ -105,7 +106,7 @@ public class ArgMap<K, V> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为 multipart 实体
|
||||
* 转换为 multipart 实体。
|
||||
*
|
||||
* @return multipart 请求体
|
||||
*/
|
||||
@@ -124,7 +125,7 @@ public class ArgMap<K, V> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为表单参数列表
|
||||
* 转换为表单参数列表。
|
||||
*
|
||||
* @return 表单参数列表
|
||||
*/
|
||||
@@ -135,9 +136,9 @@ public class ArgMap<K, V> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为 URL 查询参数
|
||||
* 转换为 query string。
|
||||
*
|
||||
* @return URL 查询参数
|
||||
* @return query string
|
||||
*/
|
||||
public String toURL() {
|
||||
if (!hasNonNullValue()) {
|
||||
@@ -150,7 +151,7 @@ public class ArgMap<K, V> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 将参数拼接到指定 URL
|
||||
* 将参数拼接到指定 URL。
|
||||
*
|
||||
* @param url 原始 URL
|
||||
* @return 拼接后的 URL
|
||||
@@ -172,7 +173,7 @@ public class ArgMap<K, V> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用一组参数创建对象
|
||||
* 使用一组参数创建对象。
|
||||
*
|
||||
* @param key 参数名
|
||||
* @param value 参数值
|
||||
@@ -185,7 +186,7 @@ public class ArgMap<K, V> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用两组参数创建对象
|
||||
* 使用两组参数创建对象。
|
||||
*
|
||||
* @param key1 参数名 1
|
||||
* @param value1 参数值 1
|
||||
@@ -200,7 +201,7 @@ public class ArgMap<K, V> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用三组参数创建对象
|
||||
* 使用三组参数创建对象。
|
||||
*
|
||||
* @param key1 参数名 1
|
||||
* @param value1 参数值 1
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.imyeyu.network;
|
||||
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.CallbackArg;
|
||||
import org.apache.hc.client5.http.fluent.ContentResponseHandler;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
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;
|
||||
@@ -16,17 +13,11 @@ import java.io.InputStream;
|
||||
* @author 夜雨
|
||||
* @since 2025-07-15 14:27
|
||||
*/
|
||||
@AllArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class CommonRequest {
|
||||
|
||||
protected final Request request;
|
||||
|
||||
/// 响应回调
|
||||
protected CallbackArg<HttpResponse> responseCallback;
|
||||
|
||||
protected CommonRequest(Request request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public static CommonRequest wrap(Request request) {
|
||||
return new CommonRequest(request);
|
||||
}
|
||||
@@ -40,33 +31,22 @@ public class CommonRequest {
|
||||
}
|
||||
|
||||
public CommonRequest timeout(long ms) {
|
||||
if (0 < ms) {
|
||||
request.connectTimeout(Timeout.ofMilliseconds(ms)).responseTimeout(Timeout.ofMilliseconds(ms));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommonRequest header(String key, String value) {
|
||||
if (TimiJava.isNotEmpty(value)) {
|
||||
request.addHeader(key, value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommonRequest token(String token) {
|
||||
return header("Token", token);
|
||||
request.addHeader("Token", token);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommonRequest language(String langHeader) {
|
||||
return header("Accept-Language", langHeader);
|
||||
}
|
||||
|
||||
/// 设置响应回调,回调在响应体读取前执行
|
||||
///
|
||||
/// @param callback 响应回调,传入 null 表示移除回调
|
||||
/// @return 当前请求
|
||||
public CommonRequest responseCallback(CallbackArg<HttpResponse> callback) {
|
||||
this.responseCallback = callback;
|
||||
request.addHeader("Accept-Language", langHeader);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -76,7 +56,7 @@ public class CommonRequest {
|
||||
}
|
||||
|
||||
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 +64,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;
|
||||
@@ -44,25 +43,19 @@ public class FileRequest extends JacksonRequest {
|
||||
|
||||
@Override
|
||||
public FileRequest header(String key, String value) {
|
||||
super.header(key, value);
|
||||
request.addHeader(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileRequest token(String token) {
|
||||
super.token(token);
|
||||
request.addHeader("Token", token);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileRequest language(String langHeader) {
|
||||
super.language(langHeader);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileRequest responseCallback(CallbackArg<HttpResponse> callback) {
|
||||
super.responseCallback(callback);
|
||||
request.addHeader("Accept-Language", langHeader);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -101,30 +99,24 @@ public class JacksonRequest extends CommonRequest {
|
||||
|
||||
@Override
|
||||
public JacksonRequest header(String key, String value) {
|
||||
super.header(key, value);
|
||||
request.addHeader(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JacksonRequest token(String token) {
|
||||
super.token(token);
|
||||
request.addHeader("Token", token);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JacksonRequest language(String langHeader) {
|
||||
super.language(langHeader);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JacksonRequest responseCallback(CallbackArg<HttpResponse> callback) {
|
||||
super.responseCallback(callback);
|
||||
request.addHeader("Accept-Language", langHeader);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 JSON 请求体
|
||||
* 设置 JSON 请求体。
|
||||
*
|
||||
* @param object 要序列化的对象
|
||||
* @return 当前请求
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -51,25 +50,19 @@ public class ProgressiveRequest extends FileRequest {
|
||||
|
||||
@Override
|
||||
public ProgressiveRequest header(String key, String value) {
|
||||
super.header(key, value);
|
||||
request.addHeader(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProgressiveRequest token(String token) {
|
||||
super.token(token);
|
||||
request.addHeader("Token", token);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProgressiveRequest language(String langHeader) {
|
||||
super.language(langHeader);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProgressiveRequest responseCallback(CallbackArg<HttpResponse> callback) {
|
||||
super.responseCallback(callback);
|
||||
request.addHeader("Accept-Language", langHeader);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -41,25 +39,19 @@ public class TimiRequest extends JacksonRequest {
|
||||
|
||||
@Override
|
||||
public TimiRequest header(String key, String value) {
|
||||
super.header(key, value);
|
||||
request.addHeader(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiRequest token(String token) {
|
||||
super.token(token);
|
||||
request.addHeader("Token", token);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiRequest language(String langHeader) {
|
||||
super.language(langHeader);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiRequest responseCallback(CallbackArg<HttpResponse> callback) {
|
||||
super.responseCallback(callback);
|
||||
request.addHeader("Accept-Language", langHeader);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user