Compare commits
2 Commits
12bb7603a1
...
016e0b0962
| Author | SHA1 | Date | |
|---|---|---|---|
| 016e0b0962 | |||
| 91ef5e3bc3 |
@ -32,6 +32,10 @@ public class CommonRequest {
|
|||||||
return request.execute().returnContent().asString();
|
return request.execute().returnContent().asString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Number asNumber() throws IOException {
|
||||||
|
return Double.parseDouble(asString());
|
||||||
|
}
|
||||||
|
|
||||||
public byte[] asBytes() throws IOException {
|
public byte[] asBytes() throws IOException {
|
||||||
return request.execute().returnContent().asBytes();
|
return request.execute().returnContent().asBytes();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,48 +14,50 @@ import java.io.IOException;
|
|||||||
* @author 夜雨
|
* @author 夜雨
|
||||||
* @since 2025-07-15 14:27
|
* @since 2025-07-15 14:27
|
||||||
*/
|
*/
|
||||||
public class GsonRequest<T> {
|
public class GsonRequest extends CommonRequest {
|
||||||
|
|
||||||
private static final Gson GSON = new Gson();
|
private static final Gson GSON = new Gson();
|
||||||
|
|
||||||
protected final Request request;
|
|
||||||
|
|
||||||
private Gson gson = null;
|
private Gson gson = null;
|
||||||
|
|
||||||
protected GsonRequest(Request request) {
|
protected GsonRequest(Request request) {
|
||||||
this.request = request;
|
super(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Gson getGson() {
|
protected Gson getGson() {
|
||||||
return TimiJava.firstNotNull(gson, GSON);
|
return TimiJava.firstNotNull(gson, GSON);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> GsonRequest<T> wrap(Request request) {
|
public static GsonRequest wrap(Request request) {
|
||||||
return new GsonRequest<>(request);
|
return new GsonRequest(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> GsonRequest<T> get(String url) {
|
public static GsonRequest get(String url) {
|
||||||
return new GsonRequest<>(Request.get(url));
|
return new GsonRequest(Request.get(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> GsonRequest<T> post(String url) {
|
public static GsonRequest post(String url) {
|
||||||
return new GsonRequest<>(Request.post(url));
|
return new GsonRequest(Request.post(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
public GsonRequest<T> gson(Gson gson) {
|
public GsonRequest gson(Gson gson) {
|
||||||
this.gson = gson;
|
this.gson = gson;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T result() throws IOException {
|
public <T> T resultAs(Class<T> clazz) throws IOException {
|
||||||
return getGson().fromJson(request.execute().returnContent().asString(), new TypeToken<T>() {}.getType());
|
return getGson().fromJson(super.asString(), clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T resultAs(TypeToken<T> typeToken) throws IOException {
|
||||||
|
return getGson().fromJson(super.asString(), typeToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonObject asJsonObject() throws IOException {
|
public JsonObject asJsonObject() throws IOException {
|
||||||
return JsonParser.parseString(request.execute().returnContent().asString()).getAsJsonObject();
|
return JsonParser.parseString(super.asString()).getAsJsonObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonArray asJsonArray() throws IOException {
|
public JsonArray asJsonArray() throws IOException {
|
||||||
return JsonParser.parseString(request.execute().returnContent().asString()).getAsJsonArray();
|
return JsonParser.parseString(super.asString()).getAsJsonArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,26 +10,44 @@ import java.io.IOException;
|
|||||||
* @author 夜雨
|
* @author 夜雨
|
||||||
* @since 2025-07-15 14:34
|
* @since 2025-07-15 14:34
|
||||||
*/
|
*/
|
||||||
public class TimiRequest<T> extends GsonRequest<T> {
|
public class TimiRequest extends GsonRequest {
|
||||||
|
|
||||||
protected TimiRequest(Request request) {
|
protected TimiRequest(Request request) {
|
||||||
super(request);
|
super(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> TimiRequest<T> wrap(Request request) {
|
public static TimiRequest wrap(Request request) {
|
||||||
return new TimiRequest<>(request);
|
return new TimiRequest(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> TimiRequest<T> get(String url) {
|
public static TimiRequest get(String url) {
|
||||||
return new TimiRequest<>(Request.get(url));
|
return new TimiRequest(Request.get(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> TimiRequest<T> post(String url) {
|
public static TimiRequest post(String url) {
|
||||||
return new TimiRequest<>(Request.post(url));
|
return new TimiRequest(Request.post(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
public T result() throws IOException {
|
@Override
|
||||||
TimiResponse<T> resp = getGson().fromJson(request.execute().returnContent().asString(), new TypeToken<TimiResponse<T>>() {}.getType());
|
public String asString() throws IOException {
|
||||||
|
return resultAs(String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Number asNumber() throws IOException {
|
||||||
|
return Double.parseDouble(asString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T resultAs(Class<T> clazz) throws IOException {
|
||||||
|
TimiResponse<T> resp = getGson().fromJson(asJsonObject(), TypeToken.getParameterized(TimiResponse.class, clazz).getType());
|
||||||
|
if (resp.isFail()) {
|
||||||
|
throw resp.toException();
|
||||||
|
}
|
||||||
|
return resp.getData();
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T resultAs(TypeToken<T> typeToken) throws IOException {
|
||||||
|
TimiResponse<T> resp = getGson().fromJson(asJsonObject(), TypeToken.getParameterized(TimiResponse.class, typeToken.getType()).getType());
|
||||||
if (resp.isFail()) {
|
if (resp.isFail()) {
|
||||||
throw resp.toException();
|
throw resp.toException();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user