Compare commits
4 Commits
51a23c2178
...
12bb7603a1
| Author | SHA1 | Date | |
|---|---|---|---|
| 12bb7603a1 | |||
| 77c32fb52f | |||
| 43919851ed | |||
| 9330c75bbc |
11
pom.xml
11
pom.xml
@ -27,5 +27,16 @@
|
|||||||
<artifactId>httpclient5-fluent</artifactId>
|
<artifactId>httpclient5-fluent</artifactId>
|
||||||
<version>5.2.1</version>
|
<version>5.2.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.11.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
38
src/main/java/com/imyeyu/network/CommonRequest.java
Normal file
38
src/main/java/com/imyeyu/network/CommonRequest.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package com.imyeyu.network;
|
||||||
|
|
||||||
|
import org.apache.hc.client5.http.fluent.Request;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 夜雨
|
||||||
|
* @since 2025-07-15 14:27
|
||||||
|
*/
|
||||||
|
public class CommonRequest {
|
||||||
|
|
||||||
|
protected final Request request;
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
public String asString() throws IOException {
|
||||||
|
return request.execute().returnContent().asString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] asBytes() throws IOException {
|
||||||
|
return request.execute().returnContent().asBytes();
|
||||||
|
}
|
||||||
|
}
|
||||||
61
src/main/java/com/imyeyu/network/FileRequest.java
Normal file
61
src/main/java/com/imyeyu/network/FileRequest.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package com.imyeyu.network;
|
||||||
|
|
||||||
|
import com.imyeyu.io.IO;
|
||||||
|
import org.apache.hc.client5.http.fluent.Request;
|
||||||
|
import org.apache.hc.core5.http.Header;
|
||||||
|
import org.apache.hc.core5.http.HttpResponse;
|
||||||
|
import org.apache.hc.core5.http.ProtocolException;
|
||||||
|
|
||||||
|
import javax.naming.NoPermissionException;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author 夜雨
|
||||||
|
* @since 2025-06-26 13:03
|
||||||
|
*/
|
||||||
|
public class FileRequest extends CommonRequest {
|
||||||
|
|
||||||
|
protected FileRequest(Request request) {
|
||||||
|
super(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FileRequest wrap(Request request) {
|
||||||
|
return new FileRequest(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FileRequest get(String url) {
|
||||||
|
return new FileRequest(Request.get(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FileRequest post(String url) {
|
||||||
|
return new FileRequest(Request.post(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toFile(String path, String fileName) throws IOException, NoPermissionException {
|
||||||
|
toFile(new File(IO.fitPath(path) + fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toFile(Path outputPath) throws IOException, NoPermissionException {
|
||||||
|
toFile(outputPath.toFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toFile(File file) throws IOException, NoPermissionException {
|
||||||
|
IO.toFile(file, asBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
61
src/main/java/com/imyeyu/network/GsonRequest.java
Normal file
61
src/main/java/com/imyeyu/network/GsonRequest.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package com.imyeyu.network;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
import com.imyeyu.java.TimiJava;
|
||||||
|
import org.apache.hc.client5.http.fluent.Request;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 夜雨
|
||||||
|
* @since 2025-07-15 14:27
|
||||||
|
*/
|
||||||
|
public class GsonRequest<T> {
|
||||||
|
|
||||||
|
private static final Gson GSON = new Gson();
|
||||||
|
|
||||||
|
protected final Request request;
|
||||||
|
|
||||||
|
private Gson gson = null;
|
||||||
|
|
||||||
|
protected GsonRequest(Request request) {
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Gson getGson() {
|
||||||
|
return TimiJava.firstNotNull(gson, GSON);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> GsonRequest<T> wrap(Request request) {
|
||||||
|
return new GsonRequest<>(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> GsonRequest<T> get(String url) {
|
||||||
|
return new GsonRequest<>(Request.get(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> GsonRequest<T> post(String url) {
|
||||||
|
return new GsonRequest<>(Request.post(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
public GsonRequest<T> gson(Gson gson) {
|
||||||
|
this.gson = gson;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T result() throws IOException {
|
||||||
|
return getGson().fromJson(request.execute().returnContent().asString(), new TypeToken<T>() {}.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonObject asJsonObject() throws IOException {
|
||||||
|
return JsonParser.parseString(request.execute().returnContent().asString()).getAsJsonObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonArray asJsonArray() throws IOException {
|
||||||
|
return JsonParser.parseString(request.execute().returnContent().asString()).getAsJsonArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
package com.imyeyu.network;
|
package com.imyeyu.network;
|
||||||
|
|
||||||
import com.imyeyu.io.IO;
|
import com.imyeyu.io.IO;
|
||||||
|
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.HttpEntity;
|
import org.apache.hc.core5.http.HttpEntity;
|
||||||
@ -9,6 +10,7 @@ import org.apache.hc.core5.http.io.entity.EntityUtils;
|
|||||||
|
|
||||||
import javax.naming.NoPermissionException;
|
import javax.naming.NoPermissionException;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@ -19,13 +21,12 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,10 +34,27 @@ public class ProgressiveRequest {
|
|||||||
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()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void toFile(File file) throws IOException, NoPermissionException {
|
||||||
|
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);
|
||||||
@ -55,14 +73,18 @@ public class ProgressiveRequest {
|
|||||||
}
|
}
|
||||||
long length = entity.getContentLength();
|
long length = entity.getContentLength();
|
||||||
|
|
||||||
IO.toOutputStream(entity.getContent(), os, new IO.OnWriteCallback() {
|
if (TimiJava.isEmpty(callback)) {
|
||||||
|
IO.toOutputStream(entity.getContent(), os);
|
||||||
|
} else {
|
||||||
|
IO.toOutputStream(entity.getContent(), os, new IO.OnWriteCallback() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean handler(long total, long now) {
|
public boolean handler(long total, long now) {
|
||||||
|
|
||||||
return callback.handler(length, total, now);
|
return callback.handler(length, total, now);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
EntityUtils.consume(entity);
|
EntityUtils.consume(entity);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|||||||
38
src/main/java/com/imyeyu/network/TimiRequest.java
Normal file
38
src/main/java/com/imyeyu/network/TimiRequest.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
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 java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 夜雨
|
||||||
|
* @since 2025-07-15 14:34
|
||||||
|
*/
|
||||||
|
public class TimiRequest<T> extends GsonRequest<T> {
|
||||||
|
|
||||||
|
protected TimiRequest(Request request) {
|
||||||
|
super(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> TimiRequest<T> wrap(Request request) {
|
||||||
|
return new TimiRequest<>(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> TimiRequest<T> get(String url) {
|
||||||
|
return new TimiRequest<>(Request.get(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> TimiRequest<T> post(String url) {
|
||||||
|
return new TimiRequest<>(Request.post(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
public T result() throws IOException {
|
||||||
|
TimiResponse<T> resp = getGson().fromJson(request.execute().returnContent().asString(), new TypeToken<TimiResponse<T>>() {}.getType());
|
||||||
|
if (resp.isFail()) {
|
||||||
|
throw resp.toException();
|
||||||
|
}
|
||||||
|
return resp.getData();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user