Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 01074e1ac2 | |||
|
|
ada0f96e57 | ||
|
|
ef7b9af9b7 | ||
| 1b60210c06 | |||
|
|
436ac6f205 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,3 +1,7 @@
|
||||
AGENTS.md
|
||||
CLAUDE.md
|
||||
.claude
|
||||
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
|
||||
47
pom.xml
47
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.imyeyu.network</groupId>
|
||||
<artifactId>timi-network</artifactId>
|
||||
<version>0.0.3</version>
|
||||
<version>0.0.5</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
@@ -28,10 +28,42 @@
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.3.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok-maven-plugin</artifactId>
|
||||
<version>1.18.20.0</version>
|
||||
<configuration>
|
||||
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
|
||||
<outputDirectory>${project.build.directory}/delombok</outputDirectory>
|
||||
<addOutputDirectory>false</addOutputDirectory>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>delombok</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.36</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.11.2</version>
|
||||
<configuration>
|
||||
<sourcepath>${project.build.directory}/delombok</sourcepath>
|
||||
<encoding>UTF-8</encoding>
|
||||
<charset>UTF-8</charset>
|
||||
<docencoding>UTF-8</docencoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
@@ -65,12 +97,17 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||
<artifactId>httpclient5-fluent</artifactId>
|
||||
<version>5.2.1</version>
|
||||
<version>5.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.11.0</version>
|
||||
<groupId>tools.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.40</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
||||
@@ -1,40 +1,143 @@
|
||||
package com.imyeyu.network;
|
||||
|
||||
import com.imyeyu.utils.Encoder;
|
||||
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.hc.client5.http.fluent.Form;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.NameValuePair;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
/**
|
||||
* HashMap 构建 {@link org.apache.hc.client5.http.fluent.Form} 参数列表
|
||||
* 请求参数列表。
|
||||
* <p>
|
||||
* 该实现允许同名 key 重复出现,适用于 query 参数和表单参数场景。
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-06-26 15:41
|
||||
*/
|
||||
public class ArgMap<K, V> extends HashMap<K, V> {
|
||||
public class ArgMap<K, V> {
|
||||
|
||||
private final List<Map.Entry<K, V>> entries = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 追加一个参数。
|
||||
*
|
||||
* @param key 参数名
|
||||
* @param value 参数值
|
||||
* @return 当前对象
|
||||
*/
|
||||
public ArgMap<K, V> put(K key, V value) {
|
||||
entries.add(new AbstractMap.SimpleImmutableEntry<>(key, value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按插入顺序遍历全部参数。
|
||||
*
|
||||
* @param consumer 处理逻辑
|
||||
*/
|
||||
public void forEach(BiConsumer<? super K, ? super V> consumer) {
|
||||
entries.forEach(entry -> consumer.accept(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回参数数量。
|
||||
*
|
||||
* @return 参数数量
|
||||
*/
|
||||
public int size() {
|
||||
return entries.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断参数列表是否为空。
|
||||
*
|
||||
* @return 是否为空
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return entries.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空全部参数。
|
||||
*/
|
||||
public void clear() {
|
||||
entries.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回只读参数视图。
|
||||
*
|
||||
* @return 参数条目列表
|
||||
*/
|
||||
public List<Map.Entry<K, V>> entries() {
|
||||
return Collections.unmodifiableList(entries);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为 multipart 实体。
|
||||
*
|
||||
* @return multipart 请求体
|
||||
*/
|
||||
public HttpEntity toEntity() {
|
||||
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
forEach((k, v) -> {
|
||||
String key = k.toString();
|
||||
switch (v) {
|
||||
case File file -> builder.addBinaryBody(key, file);
|
||||
case byte[] bytes -> builder.addBinaryBody(key, bytes);
|
||||
case InputStream stream -> builder.addBinaryBody(key, stream, ContentType.APPLICATION_OCTET_STREAM, key);
|
||||
default -> builder.addTextBody(key, v.toString());
|
||||
}
|
||||
});
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为表单参数列表。
|
||||
*
|
||||
* @return 表单参数列表
|
||||
*/
|
||||
public List<NameValuePair> toNameValuePair() {
|
||||
Form form = Form.form();
|
||||
for (Map.Entry<K, V> item : entrySet()) {
|
||||
form.add(item.getKey().toString(), item.getValue().toString());
|
||||
}
|
||||
forEach((k, v) -> form.add(k.toString(), v.toString()));
|
||||
return form.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为 query string。
|
||||
*
|
||||
* @return query string
|
||||
*/
|
||||
public String toURL() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Map.Entry<K, V> item : entrySet()) {
|
||||
sb.append(item.getKey().toString()).append('=').append(Encoder.urlArg(item.getValue().toString()));
|
||||
sb.append('&');
|
||||
if (isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
forEach((k, v) -> sb.append(k.toString()).append('=').append(Encoder.urlArg(v.toString())).append('&'));
|
||||
sb.deleteCharAt(sb.length() - 1);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将参数拼接到指定 URL。
|
||||
*
|
||||
* @param url 原始 URL
|
||||
* @return 拼接后的 URL
|
||||
*/
|
||||
public String toURL(String url) {
|
||||
if (isEmpty()) {
|
||||
return url;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder(url);
|
||||
if (url.contains("?")) {
|
||||
if (!url.endsWith("?") && !url.endsWith("&")) {
|
||||
@@ -42,29 +145,52 @@ public class ArgMap<K, V> extends HashMap<K, V> {
|
||||
}
|
||||
sb.append(toURL());
|
||||
return sb.toString();
|
||||
} else {
|
||||
return url + "?" + toURL();
|
||||
}
|
||||
return url + "?" + toURL();
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用一组参数创建对象。
|
||||
*
|
||||
* @param key 参数名
|
||||
* @param value 参数值
|
||||
* @param <K> key 类型
|
||||
* @param <V> value 类型
|
||||
* @return 参数对象
|
||||
*/
|
||||
public static <K, V> ArgMap<K, V> of(K key, V value) {
|
||||
ArgMap<K, V> map = new ArgMap<>();
|
||||
map.put(key, value);
|
||||
return map;
|
||||
return new ArgMap<K, V>().put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用两组参数创建对象。
|
||||
*
|
||||
* @param key1 参数名 1
|
||||
* @param value1 参数值 1
|
||||
* @param key2 参数名 2
|
||||
* @param value2 参数值 2
|
||||
* @param <K> key 类型
|
||||
* @param <V> value 类型
|
||||
* @return 参数对象
|
||||
*/
|
||||
public static <K, V> ArgMap<K, V> of(K key1, V value1, K key2, V value2) {
|
||||
ArgMap<K, V> map = new ArgMap<>();
|
||||
map.put(key1, value1);
|
||||
map.put(key2, value2);
|
||||
return map;
|
||||
return new ArgMap<K, V>().put(key1, value1).put(key2, value2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用三组参数创建对象。
|
||||
*
|
||||
* @param key1 参数名 1
|
||||
* @param value1 参数值 1
|
||||
* @param key2 参数名 2
|
||||
* @param value2 参数值 2
|
||||
* @param key3 参数名 3
|
||||
* @param value3 参数值 3
|
||||
* @param <K> key 类型
|
||||
* @param <V> value 类型
|
||||
* @return 参数对象
|
||||
*/
|
||||
public static <K, V> ArgMap<K, V> of(K key1, V value1, K key2, V value2, K key3, V value3) {
|
||||
ArgMap<K, V> map = new ArgMap<>();
|
||||
map.put(key1, value1);
|
||||
map.put(key2, value2);
|
||||
map.put(key3, value3);
|
||||
return map;
|
||||
return new ArgMap<K, V>().put(key1, value1).put(key2, value2).put(key3, value3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,23 @@
|
||||
package com.imyeyu.network;
|
||||
|
||||
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.util.Timeout;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2025-07-15 14:27
|
||||
*/
|
||||
@AllArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class CommonRequest {
|
||||
|
||||
protected final Request request;
|
||||
|
||||
protected CommonRequest(Request request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public static CommonRequest wrap(Request request) {
|
||||
return new CommonRequest(request);
|
||||
}
|
||||
@@ -49,6 +50,11 @@ public class CommonRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommonRequest bodyEntity(HttpEntity entity) {
|
||||
request.body(entity);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String asString() throws IOException {
|
||||
return request.execute().returnContent().asString();
|
||||
}
|
||||
@@ -61,6 +67,10 @@ public class CommonRequest {
|
||||
return request.execute().returnContent().asBytes();
|
||||
}
|
||||
|
||||
public InputStream asStream() throws IOException {
|
||||
return request.execute().returnContent().asStream();
|
||||
}
|
||||
|
||||
public void execute() throws IOException {
|
||||
request.execute();
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.imyeyu.network;
|
||||
import com.imyeyu.io.IO;
|
||||
import org.apache.hc.client5.http.fluent.Request;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
import org.apache.hc.core5.http.ProtocolException;
|
||||
|
||||
@@ -16,7 +17,7 @@ import java.nio.file.Path;
|
||||
* @author 夜雨
|
||||
* @since 2025-06-26 13:03
|
||||
*/
|
||||
public class FileRequest extends CommonRequest {
|
||||
public class FileRequest extends JacksonRequest {
|
||||
|
||||
protected FileRequest(Request request) {
|
||||
super(request);
|
||||
@@ -40,21 +41,36 @@ public class FileRequest extends CommonRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileRequest header(String key, String value) {
|
||||
request.addHeader(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileRequest token(String token) {
|
||||
request.addHeader("Token", token);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileRequest language(String langHeader) {
|
||||
request.addHeader("Accept-Language", langHeader);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileRequest body(Object object) {
|
||||
super.body(object);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileRequest bodyEntity(HttpEntity entity) {
|
||||
super.bodyEntity(entity);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void toFile(String path, String fileName) throws IOException, NoPermissionException {
|
||||
toFile(new File(IO.fitPath(path) + fileName));
|
||||
}
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
package com.imyeyu.network;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import org.apache.hc.client5.http.fluent.Request;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2025-07-15 14:27
|
||||
*/
|
||||
public class GsonRequest extends CommonRequest {
|
||||
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
private Gson gson = null;
|
||||
|
||||
protected GsonRequest(Request request) {
|
||||
super(request);
|
||||
}
|
||||
|
||||
protected Gson getGson() {
|
||||
return TimiJava.firstNotNull(gson, GSON);
|
||||
}
|
||||
|
||||
public static GsonRequest wrap(Request request) {
|
||||
return new GsonRequest(request);
|
||||
}
|
||||
|
||||
public static GsonRequest get(String url) {
|
||||
return new GsonRequest(Request.get(url));
|
||||
}
|
||||
|
||||
public static GsonRequest post(String url) {
|
||||
return new GsonRequest(Request.post(url));
|
||||
}
|
||||
|
||||
public GsonRequest gson(Gson gson) {
|
||||
this.gson = gson;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GsonRequest timeout(long ms) {
|
||||
super.timeout(ms);
|
||||
return this;
|
||||
}
|
||||
|
||||
public GsonRequest header(String key, String value) {
|
||||
request.addHeader(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public GsonRequest token(String token) {
|
||||
request.addHeader("Token", token);
|
||||
return this;
|
||||
}
|
||||
|
||||
public GsonRequest language(String langHeader) {
|
||||
request.addHeader("Accept-Language", langHeader);
|
||||
return this;
|
||||
}
|
||||
|
||||
public GsonRequest body(Object object) {
|
||||
request.bodyString(getGson().toJson(object), ContentType.APPLICATION_JSON);
|
||||
return this;
|
||||
}
|
||||
|
||||
public <T> T resultAs(Class<T> clazz) throws IOException {
|
||||
return getGson().fromJson(super.asString(), clazz);
|
||||
}
|
||||
|
||||
public <T> T resultAs(TypeToken<T> typeToken) throws IOException {
|
||||
return getGson().fromJson(super.asString(), typeToken);
|
||||
}
|
||||
|
||||
public JsonObject asJsonObject() throws IOException {
|
||||
return JsonParser.parseString(super.asString()).getAsJsonObject();
|
||||
}
|
||||
|
||||
public JsonArray asJsonArray() throws IOException {
|
||||
return JsonParser.parseString(super.asString()).getAsJsonArray();
|
||||
}
|
||||
}
|
||||
204
src/main/java/com/imyeyu/network/JacksonRequest.java
Normal file
204
src/main/java/com/imyeyu/network/JacksonRequest.java
Normal file
@@ -0,0 +1,204 @@
|
||||
package com.imyeyu.network;
|
||||
|
||||
import org.apache.hc.client5.http.fluent.Request;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import tools.jackson.core.JacksonException;
|
||||
import tools.jackson.core.type.TypeReference;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
import tools.jackson.databind.node.ArrayNode;
|
||||
import tools.jackson.databind.node.ObjectNode;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* 基于 Jackson 的请求封装。
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-07-15 14:27
|
||||
*/
|
||||
public class JacksonRequest extends CommonRequest {
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
private ObjectMapper objectMapper = null;
|
||||
|
||||
protected JacksonRequest(Request request) {
|
||||
super(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前请求使用的对象映射器。
|
||||
*
|
||||
* @return 对象映射器
|
||||
*/
|
||||
protected ObjectMapper getObjectMapper() {
|
||||
if (objectMapper != null) {
|
||||
return objectMapper;
|
||||
}
|
||||
return OBJECT_MAPPER;
|
||||
}
|
||||
|
||||
/**
|
||||
* 包装现有请求对象。
|
||||
*
|
||||
* @param request 请求对象
|
||||
* @return Jackson 请求
|
||||
*/
|
||||
public static JacksonRequest wrap(Request request) {
|
||||
return new JacksonRequest(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 GET 请求。
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @return Jackson 请求
|
||||
*/
|
||||
public static JacksonRequest get(String url) {
|
||||
return new JacksonRequest(Request.get(url));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 POST 请求。
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @return Jackson 请求
|
||||
*/
|
||||
public static JacksonRequest post(String url) {
|
||||
return new JacksonRequest(Request.post(url));
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定对象映射器。
|
||||
*
|
||||
* @param objectMapper 对象映射器
|
||||
* @return 当前请求
|
||||
*/
|
||||
public JacksonRequest objectMapper(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JacksonRequest timeout(long ms) {
|
||||
super.timeout(ms);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JacksonRequest header(String key, String value) {
|
||||
request.addHeader(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JacksonRequest token(String token) {
|
||||
request.addHeader("Token", token);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JacksonRequest language(String langHeader) {
|
||||
request.addHeader("Accept-Language", langHeader);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 JSON 请求体。
|
||||
*
|
||||
* @param object 要序列化的对象
|
||||
* @return 当前请求
|
||||
* @throws IllegalArgumentException JSON 序列化失败时抛出
|
||||
*/
|
||||
public JacksonRequest body(Object object) {
|
||||
try {
|
||||
request.bodyString(getObjectMapper().writeValueAsString(object), ContentType.APPLICATION_JSON);
|
||||
return this;
|
||||
} catch (JacksonException e) {
|
||||
throw new IllegalArgumentException("Jackson serialize request body failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JacksonRequest bodyEntity(HttpEntity entity) {
|
||||
super.bodyEntity(entity);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将响应体解析为指定类型。
|
||||
*
|
||||
* @param clazz 目标类型
|
||||
* @param <T> 结果类型
|
||||
* @return 解析结果
|
||||
* @throws IOException 读取或解析失败
|
||||
*/
|
||||
public <T> T resultAs(Class<T> clazz) throws IOException {
|
||||
return getObjectMapper().readValue(super.asString(), clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将响应体解析为指定泛型类型。
|
||||
*
|
||||
* @param typeReference 目标类型引用
|
||||
* @param <T> 结果类型
|
||||
* @return 解析结果
|
||||
* @throws IOException 读取或解析失败
|
||||
*/
|
||||
public <T> T resultAs(TypeReference<T> typeReference) throws IOException {
|
||||
return getObjectMapper().readValue(super.asString(), typeReference);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将响应体解析为指定反射类型。
|
||||
*
|
||||
* @param type 目标类型
|
||||
* @param <T> 结果类型
|
||||
* @return 解析结果
|
||||
* @throws IOException 读取或解析失败
|
||||
*/
|
||||
public <T> T resultAs(Type type) throws IOException {
|
||||
return getObjectMapper().readValue(super.asString(), getObjectMapper().constructType(type));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将响应体解析为 JSON 节点。
|
||||
*
|
||||
* @return JSON 节点
|
||||
* @throws IOException 读取或解析失败
|
||||
*/
|
||||
public JsonNode asJsonNode() throws IOException {
|
||||
return getObjectMapper().readTree(super.asString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将响应体解析为 JSON 对象。
|
||||
*
|
||||
* @return JSON 对象节点
|
||||
* @throws IOException 读取或解析失败
|
||||
*/
|
||||
public ObjectNode asJsonObject() throws IOException {
|
||||
JsonNode jsonNode = asJsonNode();
|
||||
if (jsonNode instanceof ObjectNode objectNode) {
|
||||
return objectNode;
|
||||
}
|
||||
throw new IOException("Response body is not a JSON object");
|
||||
}
|
||||
|
||||
/**
|
||||
* 将响应体解析为 JSON 数组。
|
||||
*
|
||||
* @return JSON 数组节点
|
||||
* @throws IOException 读取或解析失败
|
||||
*/
|
||||
public ArrayNode asJsonArray() throws IOException {
|
||||
JsonNode jsonNode = asJsonNode();
|
||||
if (jsonNode instanceof ArrayNode arrayNode) {
|
||||
return arrayNode;
|
||||
}
|
||||
throw new IOException("Response body is not a JSON array");
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public class Network {
|
||||
dp.browse(URI.create(Encoder.url(url)));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,21 +48,36 @@ public class ProgressiveRequest extends FileRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProgressiveRequest header(String key, String value) {
|
||||
request.addHeader(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProgressiveRequest token(String token) {
|
||||
request.addHeader("Token", token);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProgressiveRequest language(String langHeader) {
|
||||
request.addHeader("Accept-Language", langHeader);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProgressiveRequest body(Object object) {
|
||||
super.body(object);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProgressiveRequest bodyEntity(HttpEntity entity) {
|
||||
super.bodyEntity(entity);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toFile(Path outputPath) throws IOException, NoPermissionException {
|
||||
processResponse(request.execute(), IO.getOutputStream(outputPath.toFile()));
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
package com.imyeyu.network;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.imyeyu.java.bean.timi.TimiResponse;
|
||||
import org.apache.hc.client5.http.fluent.Request;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import tools.jackson.core.type.TypeReference;
|
||||
import tools.jackson.databind.JavaType;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2025-07-15 14:34
|
||||
*/
|
||||
public class TimiRequest extends GsonRequest {
|
||||
public class TimiRequest extends JacksonRequest {
|
||||
|
||||
protected TimiRequest(Request request) {
|
||||
super(request);
|
||||
@@ -34,16 +37,19 @@ public class TimiRequest extends GsonRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiRequest header(String key, String value) {
|
||||
request.addHeader(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiRequest token(String token) {
|
||||
request.addHeader("Token", token);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiRequest language(String langHeader) {
|
||||
request.addHeader("Accept-Language", langHeader);
|
||||
return this;
|
||||
@@ -55,6 +61,12 @@ public class TimiRequest extends GsonRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiRequest bodyEntity(HttpEntity entity) {
|
||||
super.bodyEntity(entity);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() throws IOException {
|
||||
return resultAs(String.class);
|
||||
@@ -65,16 +77,48 @@ public class TimiRequest extends GsonRequest {
|
||||
return Double.parseDouble(asString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取 Timi 响应中的数据字段。
|
||||
*
|
||||
* @param clazz 数据类型
|
||||
* @param <T> 数据泛型
|
||||
* @return 数据内容
|
||||
* @throws IOException 读取或解析失败
|
||||
*/
|
||||
public <T> T resultAs(Class<T> clazz) throws IOException {
|
||||
TimiResponse<T> resp = getGson().fromJson(asJsonObject(), TypeToken.getParameterized(TimiResponse.class, clazz).getType());
|
||||
TimiResponse<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(clazz));
|
||||
if (resp.isFail()) {
|
||||
throw resp.toException();
|
||||
}
|
||||
return resp.getData();
|
||||
}
|
||||
|
||||
public <T> T resultAs(TypeToken<T> typeToken) throws IOException {
|
||||
TimiResponse<T> resp = getGson().fromJson(asJsonObject(), TypeToken.getParameterized(TimiResponse.class, typeToken.getType()).getType());
|
||||
/**
|
||||
* 读取 Timi 响应中的泛型数据字段。
|
||||
*
|
||||
* @param typeReference 数据类型引用
|
||||
* @param <T> 数据泛型
|
||||
* @return 数据内容
|
||||
* @throws IOException 读取或解析失败
|
||||
*/
|
||||
public <T> T resultAs(TypeReference<T> typeReference) throws IOException {
|
||||
TimiResponse<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(typeReference));
|
||||
if (resp.isFail()) {
|
||||
throw resp.toException();
|
||||
}
|
||||
return resp.getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取 Timi 响应中的泛型数据字段。
|
||||
*
|
||||
* @param type 数据类型
|
||||
* @param <T> 数据泛型
|
||||
* @return 数据内容
|
||||
* @throws IOException 读取或解析失败
|
||||
*/
|
||||
public <T> T resultAs(Type type) throws IOException {
|
||||
TimiResponse<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(type));
|
||||
if (resp.isFail()) {
|
||||
throw resp.toException();
|
||||
}
|
||||
@@ -83,9 +127,39 @@ public class TimiRequest extends GsonRequest {
|
||||
|
||||
@Override
|
||||
public void execute() throws IOException {
|
||||
TimiResponse<?> resp = getGson().fromJson(asJsonObject(), TypeToken.getParameterized(TimiResponse.class, Object.class).getType());
|
||||
TimiResponse<?> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(Object.class));
|
||||
if (resp.isFail()) {
|
||||
throw resp.toException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造带泛型的 Timi 响应类型。
|
||||
*
|
||||
* @param clazz 数据类型
|
||||
* @return Jackson 类型
|
||||
*/
|
||||
protected JavaType timiResponseType(Class<?> clazz) {
|
||||
return getObjectMapper().getTypeFactory().constructParametricType(TimiResponse.class, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造带泛型的 Timi 响应类型。
|
||||
*
|
||||
* @param typeReference 数据类型引用
|
||||
* @return Jackson 类型
|
||||
*/
|
||||
protected JavaType timiResponseType(TypeReference<?> typeReference) {
|
||||
return getObjectMapper().getTypeFactory().constructParametricType(TimiResponse.class, getObjectMapper().constructType(typeReference));
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造带泛型的 Timi 响应类型。
|
||||
*
|
||||
* @param type 数据类型
|
||||
* @return Jackson 类型
|
||||
*/
|
||||
protected JavaType timiResponseType(Type type) {
|
||||
return getObjectMapper().getTypeFactory().constructParametricType(TimiResponse.class, getObjectMapper().constructType(type));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,4 +24,13 @@ public class ArgMapTest {
|
||||
String url = ArgMap.of("newKey", "newValue").toURL("http://localhost/test" + uri);
|
||||
assert url.equals("http://localhost/test/detail?id=123&key=value&newKey=newValue");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void duplicateKeyTest() {
|
||||
ArgMap<String, String> argMap = new ArgMap<>();
|
||||
argMap.put("id", "1");
|
||||
argMap.put("id", "2");
|
||||
assert argMap.toURL().equals("id=1&id=2");
|
||||
assert argMap.toURL("/detail").equals("/detail?id=1&id=2");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user