add response callback

This commit is contained in:
Timi
2026-08-07 15:45:08 +08:00
parent 2763e0d091
commit 34e4a733f7
5 changed files with 84 additions and 22 deletions
@@ -1,6 +1,7 @@
package com.imyeyu.network;
import com.imyeyu.io.IO;
import com.imyeyu.java.bean.CallbackArg;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
@@ -59,6 +60,12 @@ public class FileRequest extends JacksonRequest {
return this;
}
@Override
public FileRequest responseCallback(CallbackArg<HttpResponse> callback) {
super.responseCallback(callback);
return this;
}
@Override
public FileRequest body(Object object) {
super.body(object);
@@ -84,15 +91,16 @@ public class FileRequest extends JacksonRequest {
}
public long length() throws IOException, ProtocolException {
HttpResponse response = request.execute().returnResponse();
final int status = response.getCode();
if (status < 200 || 300 <= status) {
throw new IOException("HTTP error status: " + status);
}
final Header contentLengthHeader = response.getHeader("Content-Length");
if (contentLengthHeader == null) {
throw new IOException("Content-Length header missing");
}
return Long.parseLong(contentLengthHeader.getValue());
return handleResponse(response -> {
final int status = response.getCode();
if (status < 200 || 300 <= status) {
throw new IOException("HTTP error status: " + status);
}
final Header contentLengthHeader = response.getHeader("Content-Length");
if (contentLengthHeader == null) {
throw new IOException("Content-Length header missing");
}
return Long.parseLong(contentLengthHeader.getValue());
});
}
}