package com.imyeyu.network; import com.imyeyu.java.TimiJava; import com.imyeyu.java.bean.CallbackArg; import org.apache.hc.client5.http.fluent.ContentResponseHandler; 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; import java.io.InputStream; /** * @author 夜雨 * @since 2025-07-15 14:27 */ public class CommonRequest { protected final Request request; /// 响应回调 protected CallbackArg responseCallback; protected CommonRequest(Request request) { this.request = request; } public static CommonRequest wrap(Request request) { return new CommonRequest(request); } public static CommonRequest get(String url) { return new CommonRequest(Request.get(url)); } public static CommonRequest post(String 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) { 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); } public CommonRequest language(String langHeader) { return header("Accept-Language", langHeader); } /// 设置响应回调,回调在响应体读取前执行 /// /// @param callback 响应回调,传入 null 表示移除回调 /// @return 当前请求 public CommonRequest responseCallback(CallbackArg callback) { this.responseCallback = callback; return this; } public CommonRequest bodyEntity(HttpEntity entity) { request.body(entity); return this; } public String asString() throws IOException { return handleResponse(response -> new ContentResponseHandler().handleResponse(response).asString()); } public Number asNumber() throws IOException { return Double.parseDouble(asString()); } public byte[] asBytes() throws IOException { return handleResponse(response -> new ContentResponseHandler().handleResponse(response).asBytes()); } public InputStream asStream() throws IOException { return handleResponse(response -> new ContentResponseHandler().handleResponse(response).asStream()); } public void execute() throws IOException { handleResponse(response -> null); } /// 使用统一入口处理响应,子类的自定义响应处理必须调用此方法 /// /// @param handler 响应处理器 /// @param 返回值类型 /// @return 响应处理结果 /// @throws IOException 请求或响应处理失败 protected T handleResponse(HttpClientResponseHandler handler) throws IOException { return request.execute().handleResponse(response -> { if (responseCallback != null) { responseCallback.handler(response); } return handler.handleResponse(response); }); } }