v0.0.5
All checks were successful
CI/CD / build-deploy (pull_request) Successful in 3m4s

This commit is contained in:
Timi
2026-03-16 17:13:10 +08:00
parent ef7b9af9b7
commit ada0f96e57
5 changed files with 278 additions and 115 deletions

View File

@@ -1,17 +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);
@@ -75,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();
}
@@ -93,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));
}
}