add static get,post for ProgressiveRequest
This commit is contained in:
@ -4,10 +4,7 @@ import com.imyeyu.io.IO;
|
|||||||
import com.imyeyu.java.TimiJava;
|
import com.imyeyu.java.TimiJava;
|
||||||
import org.apache.hc.client5.http.fluent.Request;
|
import org.apache.hc.client5.http.fluent.Request;
|
||||||
import org.apache.hc.client5.http.fluent.Response;
|
import org.apache.hc.client5.http.fluent.Response;
|
||||||
import org.apache.hc.core5.http.Header;
|
|
||||||
import org.apache.hc.core5.http.HttpEntity;
|
import org.apache.hc.core5.http.HttpEntity;
|
||||||
import org.apache.hc.core5.http.HttpResponse;
|
|
||||||
import org.apache.hc.core5.http.ProtocolException;
|
|
||||||
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
|
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
|
||||||
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||||
|
|
||||||
@ -24,24 +21,27 @@ import java.nio.file.Path;
|
|||||||
* @author 夜雨
|
* @author 夜雨
|
||||||
* @since 2025-06-26 13:03
|
* @since 2025-06-26 13:03
|
||||||
*/
|
*/
|
||||||
public class ProgressiveRequest {
|
public class ProgressiveRequest extends FileRequest {
|
||||||
|
|
||||||
private final Request request;
|
protected final ProgressiveCallback callback;
|
||||||
private final ProgressiveCallback callback;
|
|
||||||
|
|
||||||
private ProgressiveRequest(Request request, ProgressiveCallback callback) {
|
protected ProgressiveRequest(Request request, ProgressiveCallback callback) {
|
||||||
this.request = request;
|
super(request);
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ProgressiveRequest wrap(Request request) {
|
|
||||||
return new ProgressiveRequest(request, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ProgressiveRequest wrap(Request request, ProgressiveCallback callback) {
|
public static ProgressiveRequest wrap(Request request, ProgressiveCallback callback) {
|
||||||
return new ProgressiveRequest(request, callback);
|
return new ProgressiveRequest(request, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ProgressiveRequest get(String url, ProgressiveCallback callback) {
|
||||||
|
return new ProgressiveRequest(Request.get(url), callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ProgressiveRequest post(String url, ProgressiveCallback callback) {
|
||||||
|
return new ProgressiveRequest(Request.post(url), callback);
|
||||||
|
}
|
||||||
|
|
||||||
public void toFile(Path outputPath) throws IOException, NoPermissionException {
|
public void toFile(Path outputPath) throws IOException, NoPermissionException {
|
||||||
processResponse(request.execute(), IO.getOutputStream(outputPath.toFile()));
|
processResponse(request.execute(), IO.getOutputStream(outputPath.toFile()));
|
||||||
}
|
}
|
||||||
@ -50,25 +50,17 @@ public class ProgressiveRequest {
|
|||||||
processResponse(request.execute(), IO.getOutputStream(file));
|
processResponse(request.execute(), IO.getOutputStream(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String asString() throws IOException {
|
||||||
|
return new String(asBytes());
|
||||||
|
}
|
||||||
|
|
||||||
public byte[] asBytes() throws IOException {
|
public byte[] asBytes() throws IOException {
|
||||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
processResponse(request.execute(), os);
|
processResponse(request.execute(), os);
|
||||||
return os.toByteArray();
|
return os.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
private void processResponse(Response response, OutputStream os) throws IOException {
|
private void processResponse(Response response, OutputStream os) throws IOException {
|
||||||
response.handleResponse((HttpClientResponseHandler<Void>) resp -> {
|
response.handleResponse((HttpClientResponseHandler<Void>) resp -> {
|
||||||
HttpEntity entity = resp.getEntity();
|
HttpEntity entity = resp.getEntity();
|
||||||
|
|||||||
Reference in New Issue
Block a user