diff --git a/pom.xml b/pom.xml index 7c6afa5..3061175 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,11 @@ httpclient5-fluent 5.2.1 + + com.google.code.gson + gson + 2.11.0 + junit junit diff --git a/src/main/java/com/imyeyu/network/CommonRequest.java b/src/main/java/com/imyeyu/network/CommonRequest.java new file mode 100644 index 0000000..d4994d3 --- /dev/null +++ b/src/main/java/com/imyeyu/network/CommonRequest.java @@ -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(); + } +} diff --git a/src/main/java/com/imyeyu/network/FileRequest.java b/src/main/java/com/imyeyu/network/FileRequest.java new file mode 100644 index 0000000..3e40e41 --- /dev/null +++ b/src/main/java/com/imyeyu/network/FileRequest.java @@ -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()); + } +} diff --git a/src/main/java/com/imyeyu/network/GsonRequest.java b/src/main/java/com/imyeyu/network/GsonRequest.java new file mode 100644 index 0000000..cd19417 --- /dev/null +++ b/src/main/java/com/imyeyu/network/GsonRequest.java @@ -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 { + + 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 GsonRequest wrap(Request request) { + return new GsonRequest<>(request); + } + + public static GsonRequest get(String url) { + return new GsonRequest<>(Request.get(url)); + } + + public static GsonRequest post(String url) { + return new GsonRequest<>(Request.post(url)); + } + + public GsonRequest gson(Gson gson) { + this.gson = gson; + return this; + } + + public T result() throws IOException { + return getGson().fromJson(request.execute().returnContent().asString(), new TypeToken() {}.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(); + } +} diff --git a/src/main/java/com/imyeyu/network/TimiRequest.java b/src/main/java/com/imyeyu/network/TimiRequest.java new file mode 100644 index 0000000..5d058f3 --- /dev/null +++ b/src/main/java/com/imyeyu/network/TimiRequest.java @@ -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 extends GsonRequest { + + protected TimiRequest(Request request) { + super(request); + } + + public static TimiRequest wrap(Request request) { + return new TimiRequest<>(request); + } + + public static TimiRequest get(String url) { + return new TimiRequest<>(Request.get(url)); + } + + public static TimiRequest post(String url) { + return new TimiRequest<>(Request.post(url)); + } + + public T result() throws IOException { + TimiResponse resp = getGson().fromJson(request.execute().returnContent().asString(), new TypeToken>() {}.getType()); + if (resp.isFail()) { + throw resp.toException(); + } + return resp.getData(); + } +}