add CommonRequest, FileRequest, GsonRequest, TimiRequest

This commit is contained in:
Timi
2025-07-15 16:23:34 +08:00
parent 43919851ed
commit 77c32fb52f
5 changed files with 203 additions and 0 deletions

View File

@ -27,6 +27,11 @@
<artifactId>httpclient5-fluent</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View 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();
}
}

View 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());
}
}

View 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();
}
}

View 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();
}
}