166 lines
4.0 KiB
Java
166 lines
4.0 KiB
Java
package com.imyeyu.network;
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
import com.fasterxml.jackson.databind.JavaType;
|
|
import com.imyeyu.java.bean.timi.TimiResponse;
|
|
import org.apache.hc.client5.http.fluent.Request;
|
|
import org.apache.hc.core5.http.HttpEntity;
|
|
|
|
import java.io.IOException;
|
|
import java.lang.reflect.Type;
|
|
|
|
/**
|
|
* @author 夜雨
|
|
* @since 2025-07-15 14:34
|
|
*/
|
|
public class TimiRequest extends JacksonRequest {
|
|
|
|
protected TimiRequest(Request request) {
|
|
super(request);
|
|
}
|
|
|
|
public static TimiRequest wrap(Request request) {
|
|
return new TimiRequest(request);
|
|
}
|
|
|
|
public static TimiRequest get(String url) {
|
|
return new TimiRequest(Request.get(url));
|
|
}
|
|
|
|
public static TimiRequest post(String url) {
|
|
return new TimiRequest(Request.post(url));
|
|
}
|
|
|
|
@Override
|
|
public TimiRequest timeout(long ms) {
|
|
super.timeout(ms);
|
|
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;
|
|
}
|
|
|
|
@Override
|
|
public TimiRequest body(Object object) {
|
|
super.body(object);
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public TimiRequest bodyEntity(HttpEntity entity) {
|
|
super.bodyEntity(entity);
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public String asString() throws IOException {
|
|
return resultAs(String.class);
|
|
}
|
|
|
|
@Override
|
|
public Number asNumber() throws IOException {
|
|
return Double.parseDouble(asString());
|
|
}
|
|
|
|
/**
|
|
* 读取 Timi 响应中的数据字段。
|
|
*
|
|
* @param clazz 数据类型
|
|
* @param <T> 数据泛型
|
|
* @return 数据内容
|
|
* @throws IOException 读取或解析失败
|
|
*/
|
|
public <T> T resultAs(Class<T> clazz) throws IOException {
|
|
TimiResponse<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(clazz));
|
|
if (resp.isFail()) {
|
|
throw resp.toException();
|
|
}
|
|
return resp.getData();
|
|
}
|
|
|
|
/**
|
|
* 读取 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();
|
|
}
|
|
return resp.getData();
|
|
}
|
|
|
|
@Override
|
|
public void execute() throws IOException {
|
|
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));
|
|
}
|
|
}
|