Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d09b8315b | ||
|
|
8287d6eedd | ||
|
|
113cdeb0d9 |
@@ -1,3 +1,14 @@
|
|||||||
# timi-network
|
# timi-network
|
||||||
|
|
||||||
Java 网络操作工具
|
Java 网络操作工具
|
||||||
|
|
||||||
|
静态工厂方法按 HTTP 方法命名,支持 `get`、`post`、`put`、`patch`、`delete`、`head` 和 `options`,分别用于读取、创建、替换、部分更新、删除资源、获取响应头和查询支持的请求方法
|
||||||
|
|
||||||
|
`CommonRequest`、`JacksonRequest`、`FileRequest` 和 `TimiRequest` 的工厂方法均返回对应类型,`ProgressiveRequest` 的工厂方法额外接收进度回调
|
||||||
|
|
||||||
|
```java
|
||||||
|
JacksonRequest.put("https://example.com/users/1").body(user).execute();
|
||||||
|
JacksonRequest.patch("https://example.com/users/1").body(changes).execute();
|
||||||
|
CommonRequest.delete("https://example.com/users/1").execute();
|
||||||
|
long length = FileRequest.head("https://example.com/files/1").length();
|
||||||
|
```
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>com.imyeyu.network</groupId>
|
<groupId>com.imyeyu.network</groupId>
|
||||||
<artifactId>timi-network</artifactId>
|
<artifactId>timi-network</artifactId>
|
||||||
<version>0.0.14</version>
|
<version>0.2.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
@@ -106,7 +106,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.imyeyu.io</groupId>
|
<groupId>com.imyeyu.io</groupId>
|
||||||
<artifactId>timi-io</artifactId>
|
<artifactId>timi-io</artifactId>
|
||||||
<version>0.0.5</version>
|
<version>0.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||||
|
|||||||
@@ -39,6 +39,46 @@ public class CommonRequest {
|
|||||||
return new CommonRequest(Request.post(url));
|
return new CommonRequest(Request.post(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 创建 PUT 请求,用于替换资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return CommonRequest 请求
|
||||||
|
public static CommonRequest put(String url) {
|
||||||
|
return new CommonRequest(Request.put(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 PATCH 请求,用于部分更新资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return CommonRequest 请求
|
||||||
|
public static CommonRequest patch(String url) {
|
||||||
|
return new CommonRequest(Request.patch(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 DELETE 请求,用于删除资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return CommonRequest 请求
|
||||||
|
public static CommonRequest delete(String url) {
|
||||||
|
return new CommonRequest(Request.delete(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 HEAD 请求,用于获取响应头
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return CommonRequest 请求
|
||||||
|
public static CommonRequest head(String url) {
|
||||||
|
return new CommonRequest(Request.head(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 OPTIONS 请求,用于查询支持的请求方法
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return CommonRequest 请求
|
||||||
|
public static CommonRequest options(String url) {
|
||||||
|
return new CommonRequest(Request.options(url));
|
||||||
|
}
|
||||||
|
|
||||||
public CommonRequest timeout(long ms) {
|
public CommonRequest timeout(long ms) {
|
||||||
if (0 < ms) {
|
if (0 < ms) {
|
||||||
request.connectTimeout(Timeout.ofMilliseconds(ms)).responseTimeout(Timeout.ofMilliseconds(ms));
|
request.connectTimeout(Timeout.ofMilliseconds(ms)).responseTimeout(Timeout.ofMilliseconds(ms));
|
||||||
|
|||||||
@@ -36,6 +36,46 @@ public class FileRequest extends JacksonRequest {
|
|||||||
return new FileRequest(Request.post(url));
|
return new FileRequest(Request.post(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 创建 PUT 请求,用于替换资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return FileRequest 请求
|
||||||
|
public static FileRequest put(String url) {
|
||||||
|
return new FileRequest(Request.put(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 PATCH 请求,用于部分更新资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return FileRequest 请求
|
||||||
|
public static FileRequest patch(String url) {
|
||||||
|
return new FileRequest(Request.patch(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 DELETE 请求,用于删除资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return FileRequest 请求
|
||||||
|
public static FileRequest delete(String url) {
|
||||||
|
return new FileRequest(Request.delete(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 HEAD 请求,用于获取响应头
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return FileRequest 请求
|
||||||
|
public static FileRequest head(String url) {
|
||||||
|
return new FileRequest(Request.head(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 OPTIONS 请求,用于查询支持的请求方法
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return FileRequest 请求
|
||||||
|
public static FileRequest options(String url) {
|
||||||
|
return new FileRequest(Request.options(url));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileRequest timeout(long ms) {
|
public FileRequest timeout(long ms) {
|
||||||
super.timeout(ms);
|
super.timeout(ms);
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class JacksonRequest extends CommonRequest {
|
|||||||
static {
|
static {
|
||||||
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||||
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
OBJECT_MAPPER.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ObjectMapper objectMapper = null;
|
private ObjectMapper objectMapper = null;
|
||||||
@@ -82,6 +82,46 @@ public class JacksonRequest extends CommonRequest {
|
|||||||
return new JacksonRequest(Request.post(url));
|
return new JacksonRequest(Request.post(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 创建 PUT 请求,用于替换资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return JacksonRequest 请求
|
||||||
|
public static JacksonRequest put(String url) {
|
||||||
|
return new JacksonRequest(Request.put(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 PATCH 请求,用于部分更新资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return JacksonRequest 请求
|
||||||
|
public static JacksonRequest patch(String url) {
|
||||||
|
return new JacksonRequest(Request.patch(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 DELETE 请求,用于删除资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return JacksonRequest 请求
|
||||||
|
public static JacksonRequest delete(String url) {
|
||||||
|
return new JacksonRequest(Request.delete(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 HEAD 请求,用于获取响应头
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return JacksonRequest 请求
|
||||||
|
public static JacksonRequest head(String url) {
|
||||||
|
return new JacksonRequest(Request.head(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 OPTIONS 请求,用于查询支持的请求方法
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return JacksonRequest 请求
|
||||||
|
public static JacksonRequest options(String url) {
|
||||||
|
return new JacksonRequest(Request.options(url));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 指定对象映射器。
|
* 指定对象映射器。
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -43,6 +43,51 @@ public class ProgressiveRequest extends FileRequest {
|
|||||||
return new ProgressiveRequest(Request.post(url), callback);
|
return new ProgressiveRequest(Request.post(url), callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 创建 PUT 请求,用于替换资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @param callback 进度回调
|
||||||
|
/// @return ProgressiveRequest 请求
|
||||||
|
public static ProgressiveRequest put(String url, ProgressiveCallback callback) {
|
||||||
|
return new ProgressiveRequest(Request.put(url), callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 PATCH 请求,用于部分更新资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @param callback 进度回调
|
||||||
|
/// @return ProgressiveRequest 请求
|
||||||
|
public static ProgressiveRequest patch(String url, ProgressiveCallback callback) {
|
||||||
|
return new ProgressiveRequest(Request.patch(url), callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 DELETE 请求,用于删除资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @param callback 进度回调
|
||||||
|
/// @return ProgressiveRequest 请求
|
||||||
|
public static ProgressiveRequest delete(String url, ProgressiveCallback callback) {
|
||||||
|
return new ProgressiveRequest(Request.delete(url), callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 HEAD 请求,用于获取响应头
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @param callback 进度回调
|
||||||
|
/// @return ProgressiveRequest 请求
|
||||||
|
public static ProgressiveRequest head(String url, ProgressiveCallback callback) {
|
||||||
|
return new ProgressiveRequest(Request.head(url), callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 OPTIONS 请求,用于查询支持的请求方法
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @param callback 进度回调
|
||||||
|
/// @return ProgressiveRequest 请求
|
||||||
|
public static ProgressiveRequest options(String url, ProgressiveCallback callback) {
|
||||||
|
return new ProgressiveRequest(Request.options(url), callback);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ProgressiveRequest timeout(long ms) {
|
public ProgressiveRequest timeout(long ms) {
|
||||||
super.timeout(ms);
|
super.timeout(ms);
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package com.imyeyu.network;
|
|||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.JavaType;
|
import com.fasterxml.jackson.databind.JavaType;
|
||||||
import com.imyeyu.java.bean.CallbackArg;
|
import com.imyeyu.java.bean.CallbackArg;
|
||||||
import com.imyeyu.java.bean.timi.TimiResponse;
|
import com.imyeyu.java.bean.Response;
|
||||||
import org.apache.hc.client5.http.fluent.Request;
|
import org.apache.hc.client5.http.fluent.Request;
|
||||||
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.HttpResponse;
|
||||||
@@ -33,6 +33,46 @@ public class TimiRequest extends JacksonRequest {
|
|||||||
return new TimiRequest(Request.post(url));
|
return new TimiRequest(Request.post(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 创建 PUT 请求,用于替换资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return TimiRequest 请求
|
||||||
|
public static TimiRequest put(String url) {
|
||||||
|
return new TimiRequest(Request.put(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 PATCH 请求,用于部分更新资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return TimiRequest 请求
|
||||||
|
public static TimiRequest patch(String url) {
|
||||||
|
return new TimiRequest(Request.patch(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 DELETE 请求,用于删除资源
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return TimiRequest 请求
|
||||||
|
public static TimiRequest delete(String url) {
|
||||||
|
return new TimiRequest(Request.delete(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 HEAD 请求,用于获取响应头
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return TimiRequest 请求
|
||||||
|
public static TimiRequest head(String url) {
|
||||||
|
return new TimiRequest(Request.head(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建 OPTIONS 请求,用于查询支持的请求方法
|
||||||
|
///
|
||||||
|
/// @param url 请求地址
|
||||||
|
/// @return TimiRequest 请求
|
||||||
|
public static TimiRequest options(String url) {
|
||||||
|
return new TimiRequest(Request.options(url));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TimiRequest timeout(long ms) {
|
public TimiRequest timeout(long ms) {
|
||||||
super.timeout(ms);
|
super.timeout(ms);
|
||||||
@@ -94,7 +134,7 @@ public class TimiRequest extends JacksonRequest {
|
|||||||
* @throws IOException 读取或解析失败
|
* @throws IOException 读取或解析失败
|
||||||
*/
|
*/
|
||||||
public <T> T resultAs(Class<T> clazz) throws IOException {
|
public <T> T resultAs(Class<T> clazz) throws IOException {
|
||||||
TimiResponse<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(clazz));
|
Response<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(clazz));
|
||||||
if (resp.isFail()) {
|
if (resp.isFail()) {
|
||||||
throw resp.toException();
|
throw resp.toException();
|
||||||
}
|
}
|
||||||
@@ -110,7 +150,7 @@ public class TimiRequest extends JacksonRequest {
|
|||||||
* @throws IOException 读取或解析失败
|
* @throws IOException 读取或解析失败
|
||||||
*/
|
*/
|
||||||
public <T> T resultAs(TypeReference<T> typeReference) throws IOException {
|
public <T> T resultAs(TypeReference<T> typeReference) throws IOException {
|
||||||
TimiResponse<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(typeReference));
|
Response<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(typeReference));
|
||||||
if (resp.isFail()) {
|
if (resp.isFail()) {
|
||||||
throw resp.toException();
|
throw resp.toException();
|
||||||
}
|
}
|
||||||
@@ -126,7 +166,7 @@ public class TimiRequest extends JacksonRequest {
|
|||||||
* @throws IOException 读取或解析失败
|
* @throws IOException 读取或解析失败
|
||||||
*/
|
*/
|
||||||
public <T> T resultAs(Type type) throws IOException {
|
public <T> T resultAs(Type type) throws IOException {
|
||||||
TimiResponse<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(type));
|
Response<T> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(type));
|
||||||
if (resp.isFail()) {
|
if (resp.isFail()) {
|
||||||
throw resp.toException();
|
throw resp.toException();
|
||||||
}
|
}
|
||||||
@@ -135,7 +175,7 @@ public class TimiRequest extends JacksonRequest {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute() throws IOException {
|
public void execute() throws IOException {
|
||||||
TimiResponse<?> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(Object.class));
|
Response<?> resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(Object.class));
|
||||||
if (resp.isFail()) {
|
if (resp.isFail()) {
|
||||||
throw resp.toException();
|
throw resp.toException();
|
||||||
}
|
}
|
||||||
@@ -148,7 +188,7 @@ public class TimiRequest extends JacksonRequest {
|
|||||||
* @return Jackson 类型
|
* @return Jackson 类型
|
||||||
*/
|
*/
|
||||||
protected JavaType timiResponseType(Class<?> clazz) {
|
protected JavaType timiResponseType(Class<?> clazz) {
|
||||||
return getObjectMapper().getTypeFactory().constructParametricType(TimiResponse.class, clazz);
|
return getObjectMapper().getTypeFactory().constructParametricType(Response.class, clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -158,7 +198,7 @@ public class TimiRequest extends JacksonRequest {
|
|||||||
* @return Jackson 类型
|
* @return Jackson 类型
|
||||||
*/
|
*/
|
||||||
protected JavaType timiResponseType(TypeReference<?> typeReference) {
|
protected JavaType timiResponseType(TypeReference<?> typeReference) {
|
||||||
return getObjectMapper().getTypeFactory().constructParametricType(TimiResponse.class, getObjectMapper().constructType(typeReference));
|
return getObjectMapper().getTypeFactory().constructParametricType(Response.class, getObjectMapper().constructType(typeReference));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -168,6 +208,6 @@ public class TimiRequest extends JacksonRequest {
|
|||||||
* @return Jackson 类型
|
* @return Jackson 类型
|
||||||
*/
|
*/
|
||||||
protected JavaType timiResponseType(Type type) {
|
protected JavaType timiResponseType(Type type) {
|
||||||
return getObjectMapper().getTypeFactory().constructParametricType(TimiResponse.class, getObjectMapper().constructType(type));
|
return getObjectMapper().getTypeFactory().constructParametricType(Response.class, getObjectMapper().constructType(type));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class ProgressRequestTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLength() throws Exception {
|
public void testLength() throws Exception {
|
||||||
long length = FileRequest.wrap(Request.head(URL2)).length();
|
long length = FileRequest.head(URL2).length();
|
||||||
System.out.println(IOSize.format(length));
|
System.out.println(IOSize.format(length));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,4 +38,4 @@ public class ProgressRequestTest {
|
|||||||
final Header contentLengthHeader = response.getHeader("Content-Length");
|
final Header contentLengthHeader = response.getHeader("Content-Length");
|
||||||
System.out.println(contentLengthHeader.getValue());
|
System.out.println(contentLengthHeader.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user