16 Commits
Author SHA1 Message Date
timi 21c19af696 Merge pull request 'v0.0.11' (#16) from dev into master
Reviewed-on: #16
2026-06-04 10:35:40 +00:00
timi fc04d50d0f Merge pull request 'v0.0.10' (#15) from dev into master
Reviewed-on: #15
2026-05-07 07:06:53 +00:00
timi 515387dd32 Merge pull request 'v0.0.9' (#14) from dev into master
Reviewed-on: #14
2026-05-06 09:27:58 +00:00
timi 9fb406427f Merge pull request 'v0.0.8' (#13) from dev into master
Reviewed-on: #13
2026-03-25 07:41:06 +00:00
timi 386fded0a3 Merge pull request 'v0.0.8' (#12) from dev into master
Reviewed-on: #12
2026-03-25 07:32:57 +00:00
timi 88fdc79c82 Merge pull request 'v0.0.8' (#11) from dev into master
Reviewed-on: #11
2026-03-25 07:25:09 +00:00
timi 571ed377a8 Merge pull request 'v0.0.8' (#10) from dev into master
Reviewed-on: #10
2026-03-25 07:22:40 +00:00
timi 6b84af1f16 Merge pull request 'v0.0.8' (#9) from dev into master
Reviewed-on: #9
2026-03-25 07:04:17 +00:00
timi 4302084b95 Merge pull request 'v0.0.8' (#8) from dev into master
Reviewed-on: #8
2026-03-25 06:48:12 +00:00
timi 4f8054dc94 Merge pull request 'v0.0.7' (#7) from dev into master
Reviewed-on: #7
2026-03-17 08:40:14 +00:00
timi 5c1e1ee52c Merge pull request 'v0.0.6' (#6) from dev into master
Reviewed-on: #6
2026-03-17 03:02:07 +00:00
timi 01074e1ac2 Merge pull request 'v0.0.5' (#5) from dev into master
Reviewed-on: #5
2026-03-16 09:13:40 +00:00
timi 1b60210c06 Merge pull request 'v0.0.4' (#4) from dev into master
Reviewed-on: #4
2026-03-09 03:14:26 +00:00
timi 2290ac5ecf Merge pull request 'v0.0.3' (#3) from dev into master
Reviewed-on: #3
2026-02-10 07:31:09 +00:00
timi 6101e8d462 Merge pull request 'v0.0.3' (#2) from dev into master
Reviewed-on: #2
2026-02-10 07:22:43 +00:00
timi aaea3a218b Merge pull request 'v0.0.2' (#1) from dev into master
Reviewed-on: #1
2026-01-19 10:00:36 +00:00
9 changed files with 79 additions and 374 deletions
-11
View File
@@ -1,14 +1,3 @@
# timi-network # timi-network
Java 网络操作工具 Java 网络操作工具
静态工厂方法按 HTTP 方法命名,支持 `get`、`post`、`put`、`patch`、`delete`、`head` 和 `options`,分别用于读取、创建、替换、部分更新、删除资源、获取响应头和查询支持的请求方法
`CommonRequest`、`JacksonRequest`、`FileRequest` 和 `TimiRequest` 的工厂方法均返回对应类型,`ProgressiveRequest` 的工厂方法额外接收进度回调
```java
JacksonRequest.put("https://example.com/users/1").body(user).execute();
JacksonRequest.patch("https://example.com/users/1").body(changes).execute();
CommonRequest.delete("https://example.com/users/1").execute();
long length = FileRequest.head("https://example.com/files/1").length();
```
+2 -16
View File
@@ -6,7 +6,7 @@
<groupId>com.imyeyu.network</groupId> <groupId>com.imyeyu.network</groupId>
<artifactId>timi-network</artifactId> <artifactId>timi-network</artifactId>
<version>0.2.0</version> <version>0.0.11</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
@@ -18,20 +18,6 @@
<build> <build>
<plugins> <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> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId> <artifactId>maven-deploy-plugin</artifactId>
@@ -106,7 +92,7 @@
<dependency> <dependency>
<groupId>com.imyeyu.io</groupId> <groupId>com.imyeyu.io</groupId>
<artifactId>timi-io</artifactId> <artifactId>timi-io</artifactId>
<version>0.1.0</version> <version>0.0.5</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents.client5</groupId> <groupId>org.apache.httpcomponents.client5</groupId>
+22 -21
View File
@@ -1,6 +1,5 @@
package com.imyeyu.network; package com.imyeyu.network;
import com.imyeyu.java.TimiJava;
import com.imyeyu.utils.Encoder; import com.imyeyu.utils.Encoder;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder; import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
import org.apache.hc.client5.http.fluent.Form; 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<>(); private final List<Map.Entry<K, V>> entries = new ArrayList<>();
/** /**
* 追加一个参数 * 追加一个参数。
* *
* @param key 参数名 * @param key 参数名
* @param value 参数值 * @param value 参数值
@@ -42,7 +41,7 @@ public class ArgMap<K, V> {
} }
/** /**
* 按插入顺序遍历全部参数 * 按插入顺序遍历全部参数。
* *
* @param consumer 处理逻辑 * @param consumer 处理逻辑
*/ */
@@ -51,29 +50,29 @@ public class ArgMap<K, V> {
} }
/** /**
* 按插入顺序遍历全部非空参数 * 按插入顺序遍历全部非空参数。
* *
* @param consumer 处理逻辑 * @param consumer 处理逻辑
*/ */
private void forEachNonNull(BiConsumer<? super K, ? super V> consumer) { private void forEachNonNull(BiConsumer<? super K, ? super V> consumer) {
entries.forEach(entry -> { entries.forEach(entry -> {
if (TimiJava.isNotEmpty(entry.getValue())) { if (entry.getValue() != null) {
consumer.accept(entry.getKey(), entry.getValue()); consumer.accept(entry.getKey(), entry.getValue());
} }
}); });
} }
/** /**
* 是否存在可输出的非空参数 * 是否存在可输出的非空参数。
* *
* @return true 为存在非空参数 * @return 是否存在非空参数
*/ */
private boolean hasNonNullValue() { private boolean hasNonNullValue() {
return entries.stream().anyMatch(entry -> TimiJava.isNotEmpty(entry.getValue())); return entries.stream().anyMatch(entry -> entry.getValue() != null);
} }
/** /**
* 参数数量 * 返回参数数量。
* *
* @return 参数数量 * @return 参数数量
*/ */
@@ -82,21 +81,23 @@ public class ArgMap<K, V> {
} }
/** /**
* 判断参数列表是否为空 * 判断参数列表是否为空。
* *
* @return true 为空 * @return 是否为空
*/ */
public boolean isEmpty() { public boolean isEmpty() {
return entries.isEmpty(); return entries.isEmpty();
} }
/** 清空全部参数 */ /**
* 清空全部参数。
*/
public void clear() { public void clear() {
entries.clear(); entries.clear();
} }
/** /**
* 返回只读参数视图 * 返回只读参数视图。
* *
* @return 参数条目列表 * @return 参数条目列表
*/ */
@@ -105,7 +106,7 @@ public class ArgMap<K, V> {
} }
/** /**
* 转换为 multipart 实体 * 转换为 multipart 实体。
* *
* @return multipart 请求体 * @return multipart 请求体
*/ */
@@ -124,7 +125,7 @@ public class ArgMap<K, V> {
} }
/** /**
* 转换为表单参数列表 * 转换为表单参数列表。
* *
* @return 表单参数列表 * @return 表单参数列表
*/ */
@@ -135,9 +136,9 @@ public class ArgMap<K, V> {
} }
/** /**
* 转换为 URL 查询参数 * 转换为 query string。
* *
* @return URL 查询参数 * @return query string
*/ */
public String toURL() { public String toURL() {
if (!hasNonNullValue()) { if (!hasNonNullValue()) {
@@ -150,7 +151,7 @@ public class ArgMap<K, V> {
} }
/** /**
* 将参数拼接到指定 URL * 将参数拼接到指定 URL。
* *
* @param url 原始 URL * @param url 原始 URL
* @return 拼接后的 URL * @return 拼接后的 URL
@@ -172,7 +173,7 @@ public class ArgMap<K, V> {
} }
/** /**
* 使用一组参数创建对象 * 使用一组参数创建对象。
* *
* @param key 参数名 * @param key 参数名
* @param value 参数值 * @param value 参数值
@@ -185,7 +186,7 @@ public class ArgMap<K, V> {
} }
/** /**
* 使用两组参数创建对象 * 使用两组参数创建对象。
* *
* @param key1 参数名 1 * @param key1 参数名 1
* @param value1 参数值 1 * @param value1 参数值 1
@@ -200,7 +201,7 @@ public class ArgMap<K, V> {
} }
/** /**
* 使用三组参数创建对象 * 使用三组参数创建对象。
* *
* @param key1 参数名 1 * @param key1 参数名 1
* @param value1 参数值 1 * @param value1 参数值 1
@@ -1,12 +1,9 @@
package com.imyeyu.network; package com.imyeyu.network;
import com.imyeyu.java.TimiJava; import lombok.AccessLevel;
import com.imyeyu.java.bean.CallbackArg; import lombok.AllArgsConstructor;
import org.apache.hc.client5.http.fluent.ContentResponseHandler;
import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.HttpEntity; 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 org.apache.hc.core5.util.Timeout;
import java.io.IOException; import java.io.IOException;
@@ -16,17 +13,11 @@ import java.io.InputStream;
* @author 夜雨 * @author 夜雨
* @since 2025-07-15 14:27 * @since 2025-07-15 14:27
*/ */
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class CommonRequest { public class CommonRequest {
protected final Request request; protected final Request request;
/// 响应回调
protected CallbackArg<HttpResponse> responseCallback;
protected CommonRequest(Request request) {
this.request = request;
}
public static CommonRequest wrap(Request request) { public static CommonRequest wrap(Request request) {
return new CommonRequest(request); return new CommonRequest(request);
} }
@@ -39,74 +30,23 @@ public class CommonRequest {
return new CommonRequest(Request.post(url)); return new CommonRequest(Request.post(url));
} }
/// 创建 PUT 请求,用于替换资源
///
/// @param url 请求地址
/// @return CommonRequest 请求
public static CommonRequest put(String url) {
return new CommonRequest(Request.put(url));
}
/// 创建 PATCH 请求,用于部分更新资源
///
/// @param url 请求地址
/// @return CommonRequest 请求
public static CommonRequest patch(String url) {
return new CommonRequest(Request.patch(url));
}
/// 创建 DELETE 请求,用于删除资源
///
/// @param url 请求地址
/// @return CommonRequest 请求
public static CommonRequest delete(String url) {
return new CommonRequest(Request.delete(url));
}
/// 创建 HEAD 请求,用于获取响应头
///
/// @param url 请求地址
/// @return CommonRequest 请求
public static CommonRequest head(String url) {
return new CommonRequest(Request.head(url));
}
/// 创建 OPTIONS 请求,用于查询支持的请求方法
///
/// @param url 请求地址
/// @return CommonRequest 请求
public static CommonRequest options(String url) {
return new CommonRequest(Request.options(url));
}
public CommonRequest timeout(long ms) { public CommonRequest timeout(long ms) {
if (0 < ms) { request.connectTimeout(Timeout.ofMilliseconds(ms)).responseTimeout(Timeout.ofMilliseconds(ms));
request.connectTimeout(Timeout.ofMilliseconds(ms)).responseTimeout(Timeout.ofMilliseconds(ms));
}
return this; return this;
} }
public CommonRequest header(String key, String value) { public CommonRequest header(String key, String value) {
if (TimiJava.isNotEmpty(value)) { request.addHeader(key, value);
request.addHeader(key, value);
}
return this; return this;
} }
public CommonRequest token(String token) { public CommonRequest token(String token) {
return header("Token", token); request.addHeader("Token", token);
return this;
} }
public CommonRequest language(String langHeader) { public CommonRequest language(String langHeader) {
return header("Accept-Language", langHeader); request.addHeader("Accept-Language", langHeader);
}
/// 设置响应回调,回调在响应体读取前执行
///
/// @param callback 响应回调,传入 null 表示移除回调
/// @return 当前请求
public CommonRequest responseCallback(CallbackArg<HttpResponse> callback) {
this.responseCallback = callback;
return this; return this;
} }
@@ -116,7 +56,7 @@ public class CommonRequest {
} }
public String asString() throws IOException { public String asString() throws IOException {
return handleResponse(response -> new ContentResponseHandler().handleResponse(response).asString()); return request.execute().returnContent().asString();
} }
public Number asNumber() throws IOException { public Number asNumber() throws IOException {
@@ -124,29 +64,14 @@ public class CommonRequest {
} }
public byte[] asBytes() throws IOException { public byte[] asBytes() throws IOException {
return handleResponse(response -> new ContentResponseHandler().handleResponse(response).asBytes()); return request.execute().returnContent().asBytes();
} }
public InputStream asStream() throws IOException { public InputStream asStream() throws IOException {
return handleResponse(response -> new ContentResponseHandler().handleResponse(response).asStream()); return request.execute().returnContent().asStream();
} }
public void execute() throws IOException { public void execute() throws IOException {
handleResponse(response -> null); request.execute();
}
/// 使用统一入口处理响应,子类的自定义响应处理必须调用此方法
///
/// @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);
});
} }
} }
@@ -1,7 +1,6 @@
package com.imyeyu.network; package com.imyeyu.network;
import com.imyeyu.io.IO; import com.imyeyu.io.IO;
import com.imyeyu.java.bean.CallbackArg;
import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpEntity;
@@ -36,46 +35,6 @@ public class FileRequest extends JacksonRequest {
return new FileRequest(Request.post(url)); return new FileRequest(Request.post(url));
} }
/// 创建 PUT 请求,用于替换资源
///
/// @param url 请求地址
/// @return FileRequest 请求
public static FileRequest put(String url) {
return new FileRequest(Request.put(url));
}
/// 创建 PATCH 请求,用于部分更新资源
///
/// @param url 请求地址
/// @return FileRequest 请求
public static FileRequest patch(String url) {
return new FileRequest(Request.patch(url));
}
/// 创建 DELETE 请求,用于删除资源
///
/// @param url 请求地址
/// @return FileRequest 请求
public static FileRequest delete(String url) {
return new FileRequest(Request.delete(url));
}
/// 创建 HEAD 请求,用于获取响应头
///
/// @param url 请求地址
/// @return FileRequest 请求
public static FileRequest head(String url) {
return new FileRequest(Request.head(url));
}
/// 创建 OPTIONS 请求,用于查询支持的请求方法
///
/// @param url 请求地址
/// @return FileRequest 请求
public static FileRequest options(String url) {
return new FileRequest(Request.options(url));
}
@Override @Override
public FileRequest timeout(long ms) { public FileRequest timeout(long ms) {
super.timeout(ms); super.timeout(ms);
@@ -84,25 +43,19 @@ public class FileRequest extends JacksonRequest {
@Override @Override
public FileRequest header(String key, String value) { public FileRequest header(String key, String value) {
super.header(key, value); request.addHeader(key, value);
return this; return this;
} }
@Override @Override
public FileRequest token(String token) { public FileRequest token(String token) {
super.token(token); request.addHeader("Token", token);
return this; return this;
} }
@Override @Override
public FileRequest language(String langHeader) { public FileRequest language(String langHeader) {
super.language(langHeader); request.addHeader("Accept-Language", langHeader);
return this;
}
@Override
public FileRequest responseCallback(CallbackArg<HttpResponse> callback) {
super.responseCallback(callback);
return this; return this;
} }
@@ -131,16 +84,15 @@ public class FileRequest extends JacksonRequest {
} }
public long length() throws IOException, ProtocolException { public long length() throws IOException, ProtocolException {
return handleResponse(response -> { HttpResponse response = request.execute().returnResponse();
final int status = response.getCode(); final int status = response.getCode();
if (status < 200 || 300 <= status) { if (status < 200 || 300 <= status) {
throw new IOException("HTTP error status: " + status); throw new IOException("HTTP error status: " + status);
} }
final Header contentLengthHeader = response.getHeader("Content-Length"); final Header contentLengthHeader = response.getHeader("Content-Length");
if (contentLengthHeader == null) { if (contentLengthHeader == null) {
throw new IOException("Content-Length header missing"); throw new IOException("Content-Length header missing");
} }
return Long.parseLong(contentLengthHeader.getValue()); 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.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode; 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.client5.http.fluent.Request;
import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpResponse;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@@ -31,7 +29,7 @@ public class JacksonRequest extends CommonRequest {
static { static {
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
OBJECT_MAPPER.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL); OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
} }
private ObjectMapper objectMapper = null; private ObjectMapper objectMapper = null;
@@ -82,46 +80,6 @@ public class JacksonRequest extends CommonRequest {
return new JacksonRequest(Request.post(url)); return new JacksonRequest(Request.post(url));
} }
/// 创建 PUT 请求,用于替换资源
///
/// @param url 请求地址
/// @return JacksonRequest 请求
public static JacksonRequest put(String url) {
return new JacksonRequest(Request.put(url));
}
/// 创建 PATCH 请求,用于部分更新资源
///
/// @param url 请求地址
/// @return JacksonRequest 请求
public static JacksonRequest patch(String url) {
return new JacksonRequest(Request.patch(url));
}
/// 创建 DELETE 请求,用于删除资源
///
/// @param url 请求地址
/// @return JacksonRequest 请求
public static JacksonRequest delete(String url) {
return new JacksonRequest(Request.delete(url));
}
/// 创建 HEAD 请求,用于获取响应头
///
/// @param url 请求地址
/// @return JacksonRequest 请求
public static JacksonRequest head(String url) {
return new JacksonRequest(Request.head(url));
}
/// 创建 OPTIONS 请求,用于查询支持的请求方法
///
/// @param url 请求地址
/// @return JacksonRequest 请求
public static JacksonRequest options(String url) {
return new JacksonRequest(Request.options(url));
}
/** /**
* 指定对象映射器。 * 指定对象映射器。
* *
@@ -141,30 +99,24 @@ public class JacksonRequest extends CommonRequest {
@Override @Override
public JacksonRequest header(String key, String value) { public JacksonRequest header(String key, String value) {
super.header(key, value); request.addHeader(key, value);
return this; return this;
} }
@Override @Override
public JacksonRequest token(String token) { public JacksonRequest token(String token) {
super.token(token); request.addHeader("Token", token);
return this; return this;
} }
@Override @Override
public JacksonRequest language(String langHeader) { public JacksonRequest language(String langHeader) {
super.language(langHeader); request.addHeader("Accept-Language", langHeader);
return this;
}
@Override
public JacksonRequest responseCallback(CallbackArg<HttpResponse> callback) {
super.responseCallback(callback);
return this; return this;
} }
/** /**
* 设置 JSON 请求体 * 设置 JSON 请求体。
* *
* @param object 要序列化的对象 * @param object 要序列化的对象
* @return 当前请求 * @return 当前请求
@@ -2,10 +2,9 @@ package com.imyeyu.network;
import com.imyeyu.io.IO; import com.imyeyu.io.IO;
import com.imyeyu.java.TimiJava; 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.Request;
import org.apache.hc.client5.http.fluent.Response;
import org.apache.hc.core5.http.HttpEntity; 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.HttpClientResponseHandler;
import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.http.io.entity.EntityUtils;
@@ -43,51 +42,6 @@ public class ProgressiveRequest extends FileRequest {
return new ProgressiveRequest(Request.post(url), callback); return new ProgressiveRequest(Request.post(url), callback);
} }
/// 创建 PUT 请求,用于替换资源
///
/// @param url 请求地址
/// @param callback 进度回调
/// @return ProgressiveRequest 请求
public static ProgressiveRequest put(String url, ProgressiveCallback callback) {
return new ProgressiveRequest(Request.put(url), callback);
}
/// 创建 PATCH 请求,用于部分更新资源
///
/// @param url 请求地址
/// @param callback 进度回调
/// @return ProgressiveRequest 请求
public static ProgressiveRequest patch(String url, ProgressiveCallback callback) {
return new ProgressiveRequest(Request.patch(url), callback);
}
/// 创建 DELETE 请求,用于删除资源
///
/// @param url 请求地址
/// @param callback 进度回调
/// @return ProgressiveRequest 请求
public static ProgressiveRequest delete(String url, ProgressiveCallback callback) {
return new ProgressiveRequest(Request.delete(url), callback);
}
/// 创建 HEAD 请求,用于获取响应头
///
/// @param url 请求地址
/// @param callback 进度回调
/// @return ProgressiveRequest 请求
public static ProgressiveRequest head(String url, ProgressiveCallback callback) {
return new ProgressiveRequest(Request.head(url), callback);
}
/// 创建 OPTIONS 请求,用于查询支持的请求方法
///
/// @param url 请求地址
/// @param callback 进度回调
/// @return ProgressiveRequest 请求
public static ProgressiveRequest options(String url, ProgressiveCallback callback) {
return new ProgressiveRequest(Request.options(url), callback);
}
@Override @Override
public ProgressiveRequest timeout(long ms) { public ProgressiveRequest timeout(long ms) {
super.timeout(ms); super.timeout(ms);
@@ -96,25 +50,19 @@ public class ProgressiveRequest extends FileRequest {
@Override @Override
public ProgressiveRequest header(String key, String value) { public ProgressiveRequest header(String key, String value) {
super.header(key, value); request.addHeader(key, value);
return this; return this;
} }
@Override @Override
public ProgressiveRequest token(String token) { public ProgressiveRequest token(String token) {
super.token(token); request.addHeader("Token", token);
return this; return this;
} }
@Override @Override
public ProgressiveRequest language(String langHeader) { public ProgressiveRequest language(String langHeader) {
super.language(langHeader); request.addHeader("Accept-Language", langHeader);
return this;
}
@Override
public ProgressiveRequest responseCallback(CallbackArg<HttpResponse> callback) {
super.responseCallback(callback);
return this; return this;
} }
@@ -132,12 +80,12 @@ public class ProgressiveRequest extends FileRequest {
@Override @Override
public void toFile(Path outputPath) throws IOException, NoPermissionException { public void toFile(Path outputPath) throws IOException, NoPermissionException {
processResponse(IO.getOutputStream(outputPath.toFile())); processResponse(request.execute(), IO.getOutputStream(outputPath.toFile()));
} }
@Override @Override
public void toFile(File file) throws IOException, NoPermissionException { public void toFile(File file) throws IOException, NoPermissionException {
processResponse(IO.getOutputStream(file)); processResponse(request.execute(), IO.getOutputStream(file));
} }
@Override @Override
@@ -148,17 +96,17 @@ public class ProgressiveRequest extends FileRequest {
@Override @Override
public byte[] asBytes() throws IOException { public byte[] asBytes() throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
processResponse(os); processResponse(request.execute(), os);
return os.toByteArray(); return os.toByteArray();
} }
private void processResponse(OutputStream os) throws IOException { private void processResponse(Response response, OutputStream os) throws IOException {
handleResponse((HttpClientResponseHandler<Void>) response -> { response.handleResponse((HttpClientResponseHandler<Void>) resp -> {
HttpEntity entity = response.getEntity(); HttpEntity entity = resp.getEntity();
if (entity == null) { if (entity == null) {
throw new IOException("not found response entity"); throw new IOException("not found response entity");
} }
int code = response.getCode(); int code = resp.getCode();
if (400 <= code) { if (400 <= code) {
throw new IOException("response error: %s".formatted(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.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
import com.imyeyu.java.bean.CallbackArg; import com.imyeyu.java.bean.timi.TimiResponse;
import com.imyeyu.java.bean.Response;
import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpResponse;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@@ -33,46 +31,6 @@ public class TimiRequest extends JacksonRequest {
return new TimiRequest(Request.post(url)); return new TimiRequest(Request.post(url));
} }
/// 创建 PUT 请求,用于替换资源
///
/// @param url 请求地址
/// @return TimiRequest 请求
public static TimiRequest put(String url) {
return new TimiRequest(Request.put(url));
}
/// 创建 PATCH 请求,用于部分更新资源
///
/// @param url 请求地址
/// @return TimiRequest 请求
public static TimiRequest patch(String url) {
return new TimiRequest(Request.patch(url));
}
/// 创建 DELETE 请求,用于删除资源
///
/// @param url 请求地址
/// @return TimiRequest 请求
public static TimiRequest delete(String url) {
return new TimiRequest(Request.delete(url));
}
/// 创建 HEAD 请求,用于获取响应头
///
/// @param url 请求地址
/// @return TimiRequest 请求
public static TimiRequest head(String url) {
return new TimiRequest(Request.head(url));
}
/// 创建 OPTIONS 请求,用于查询支持的请求方法
///
/// @param url 请求地址
/// @return TimiRequest 请求
public static TimiRequest options(String url) {
return new TimiRequest(Request.options(url));
}
@Override @Override
public TimiRequest timeout(long ms) { public TimiRequest timeout(long ms) {
super.timeout(ms); super.timeout(ms);
@@ -81,25 +39,19 @@ public class TimiRequest extends JacksonRequest {
@Override @Override
public TimiRequest header(String key, String value) { public TimiRequest header(String key, String value) {
super.header(key, value); request.addHeader(key, value);
return this; return this;
} }
@Override @Override
public TimiRequest token(String token) { public TimiRequest token(String token) {
super.token(token); request.addHeader("Token", token);
return this; return this;
} }
@Override @Override
public TimiRequest language(String langHeader) { public TimiRequest language(String langHeader) {
super.language(langHeader); request.addHeader("Accept-Language", langHeader);
return this;
}
@Override
public TimiRequest responseCallback(CallbackArg<HttpResponse> callback) {
super.responseCallback(callback);
return this; return this;
} }
@@ -134,7 +86,7 @@ public class TimiRequest extends JacksonRequest {
* @throws IOException 读取或解析失败 * @throws IOException 读取或解析失败
*/ */
public <T> T resultAs(Class<T> clazz) throws IOException { public <T> T resultAs(Class<T> clazz) throws IOException {
Response<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(clazz)); TimiResponse<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(clazz));
if (resp.isFail()) { if (resp.isFail()) {
throw resp.toException(); throw resp.toException();
} }
@@ -150,7 +102,7 @@ public class TimiRequest extends JacksonRequest {
* @throws IOException 读取或解析失败 * @throws IOException 读取或解析失败
*/ */
public <T> T resultAs(TypeReference<T> typeReference) throws IOException { public <T> T resultAs(TypeReference<T> typeReference) throws IOException {
Response<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(typeReference)); TimiResponse<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(typeReference));
if (resp.isFail()) { if (resp.isFail()) {
throw resp.toException(); throw resp.toException();
} }
@@ -166,7 +118,7 @@ public class TimiRequest extends JacksonRequest {
* @throws IOException 读取或解析失败 * @throws IOException 读取或解析失败
*/ */
public <T> T resultAs(Type type) throws IOException { public <T> T resultAs(Type type) throws IOException {
Response<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(type)); TimiResponse<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(type));
if (resp.isFail()) { if (resp.isFail()) {
throw resp.toException(); throw resp.toException();
} }
@@ -175,7 +127,7 @@ public class TimiRequest extends JacksonRequest {
@Override @Override
public void execute() throws IOException { public void execute() throws IOException {
Response<?> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(Object.class)); TimiResponse<?> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(Object.class));
if (resp.isFail()) { if (resp.isFail()) {
throw resp.toException(); throw resp.toException();
} }
@@ -188,7 +140,7 @@ public class TimiRequest extends JacksonRequest {
* @return Jackson 类型 * @return Jackson 类型
*/ */
protected JavaType timiResponseType(Class<?> clazz) { protected JavaType timiResponseType(Class<?> clazz) {
return getObjectMapper().getTypeFactory().constructParametricType(Response.class, clazz); return getObjectMapper().getTypeFactory().constructParametricType(TimiResponse.class, clazz);
} }
/** /**
@@ -198,7 +150,7 @@ public class TimiRequest extends JacksonRequest {
* @return Jackson 类型 * @return Jackson 类型
*/ */
protected JavaType timiResponseType(TypeReference<?> typeReference) { protected JavaType timiResponseType(TypeReference<?> typeReference) {
return getObjectMapper().getTypeFactory().constructParametricType(Response.class, getObjectMapper().constructType(typeReference)); return getObjectMapper().getTypeFactory().constructParametricType(TimiResponse.class, getObjectMapper().constructType(typeReference));
} }
/** /**
@@ -208,6 +160,6 @@ public class TimiRequest extends JacksonRequest {
* @return Jackson 类型 * @return Jackson 类型
*/ */
protected JavaType timiResponseType(Type type) { protected JavaType timiResponseType(Type type) {
return getObjectMapper().getTypeFactory().constructParametricType(Response.class, getObjectMapper().constructType(type)); return getObjectMapper().getTypeFactory().constructParametricType(TimiResponse.class, getObjectMapper().constructType(type));
} }
} }
@@ -28,7 +28,7 @@ public class ProgressRequestTest {
@Test @Test
public void testLength() throws Exception { public void testLength() throws Exception {
long length = FileRequest.head(URL2).length(); long length = FileRequest.wrap(Request.head(URL2)).length();
System.out.println(IOSize.format(length)); System.out.println(IOSize.format(length));
} }