add bodyEntity
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,3 +1,7 @@
|
|||||||
|
AGENTS.md
|
||||||
|
CLAUDE.md
|
||||||
|
.claude
|
||||||
|
|
||||||
target/
|
target/
|
||||||
!.mvn/wrapper/maven-wrapper.jar
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
!**/src/main/**/target/
|
!**/src/main/**/target/
|
||||||
|
|||||||
7
pom.xml
7
pom.xml
@@ -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.4</version>
|
<version>0.0.5</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
@@ -94,6 +94,11 @@
|
|||||||
<artifactId>timi-io</artifactId>
|
<artifactId>timi-io</artifactId>
|
||||||
<version>0.0.2</version>
|
<version>0.0.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-collections4</artifactId>
|
||||||
|
<version>4.4</version> <!-- 请使用最新版本 -->
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||||
<artifactId>httpclient5-fluent</artifactId>
|
<artifactId>httpclient5-fluent</artifactId>
|
||||||
|
|||||||
@@ -1,40 +1,143 @@
|
|||||||
package com.imyeyu.network;
|
package com.imyeyu.network;
|
||||||
|
|
||||||
import com.imyeyu.utils.Encoder;
|
import com.imyeyu.utils.Encoder;
|
||||||
|
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
|
||||||
import org.apache.hc.client5.http.fluent.Form;
|
import org.apache.hc.client5.http.fluent.Form;
|
||||||
|
import org.apache.hc.core5.http.ContentType;
|
||||||
|
import org.apache.hc.core5.http.HttpEntity;
|
||||||
import org.apache.hc.core5.http.NameValuePair;
|
import org.apache.hc.core5.http.NameValuePair;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.AbstractMap;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HashMap 构建 {@link org.apache.hc.client5.http.fluent.Form} 参数列表
|
* 请求参数列表。
|
||||||
|
* <p>
|
||||||
|
* 该实现允许同名 key 重复出现,适用于 query 参数和表单参数场景。
|
||||||
*
|
*
|
||||||
* @author 夜雨
|
* @author 夜雨
|
||||||
* @since 2025-06-26 15:41
|
* @since 2025-06-26 15:41
|
||||||
*/
|
*/
|
||||||
public class ArgMap<K, V> extends HashMap<K, V> {
|
public class ArgMap<K, V> {
|
||||||
|
|
||||||
|
private final List<Map.Entry<K, V>> entries = new ArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 追加一个参数。
|
||||||
|
*
|
||||||
|
* @param key 参数名
|
||||||
|
* @param value 参数值
|
||||||
|
* @return 当前对象
|
||||||
|
*/
|
||||||
|
public ArgMap<K, V> put(K key, V value) {
|
||||||
|
entries.add(new AbstractMap.SimpleImmutableEntry<>(key, value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按插入顺序遍历全部参数。
|
||||||
|
*
|
||||||
|
* @param consumer 处理逻辑
|
||||||
|
*/
|
||||||
|
public void forEach(BiConsumer<? super K, ? super V> consumer) {
|
||||||
|
entries.forEach(entry -> consumer.accept(entry.getKey(), entry.getValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回参数数量。
|
||||||
|
*
|
||||||
|
* @return 参数数量
|
||||||
|
*/
|
||||||
|
public int size() {
|
||||||
|
return entries.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断参数列表是否为空。
|
||||||
|
*
|
||||||
|
* @return 是否为空
|
||||||
|
*/
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return entries.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空全部参数。
|
||||||
|
*/
|
||||||
|
public void clear() {
|
||||||
|
entries.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回只读参数视图。
|
||||||
|
*
|
||||||
|
* @return 参数条目列表
|
||||||
|
*/
|
||||||
|
public List<Map.Entry<K, V>> entries() {
|
||||||
|
return Collections.unmodifiableList(entries);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换为 multipart 实体。
|
||||||
|
*
|
||||||
|
* @return multipart 请求体
|
||||||
|
*/
|
||||||
|
public HttpEntity toEntity() {
|
||||||
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||||
|
forEach((k, v) -> {
|
||||||
|
String key = k.toString();
|
||||||
|
switch (v) {
|
||||||
|
case File file -> builder.addBinaryBody(key, file);
|
||||||
|
case byte[] bytes -> builder.addBinaryBody(key, bytes);
|
||||||
|
case InputStream stream -> builder.addBinaryBody(key, stream, ContentType.APPLICATION_OCTET_STREAM, key);
|
||||||
|
default -> builder.addTextBody(key, v.toString());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换为表单参数列表。
|
||||||
|
*
|
||||||
|
* @return 表单参数列表
|
||||||
|
*/
|
||||||
public List<NameValuePair> toNameValuePair() {
|
public List<NameValuePair> toNameValuePair() {
|
||||||
Form form = Form.form();
|
Form form = Form.form();
|
||||||
for (Map.Entry<K, V> item : entrySet()) {
|
forEach((k, v) -> form.add(k.toString(), v.toString()));
|
||||||
form.add(item.getKey().toString(), item.getValue().toString());
|
|
||||||
}
|
|
||||||
return form.build();
|
return form.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换为 query string。
|
||||||
|
*
|
||||||
|
* @return query string
|
||||||
|
*/
|
||||||
public String toURL() {
|
public String toURL() {
|
||||||
StringBuilder sb = new StringBuilder();
|
if (isEmpty()) {
|
||||||
for (Map.Entry<K, V> item : entrySet()) {
|
return "";
|
||||||
sb.append(item.getKey().toString()).append('=').append(Encoder.urlArg(item.getValue().toString()));
|
|
||||||
sb.append('&');
|
|
||||||
}
|
}
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
forEach((k, v) -> sb.append(k.toString()).append('=').append(Encoder.urlArg(v.toString())).append('&'));
|
||||||
sb.deleteCharAt(sb.length() - 1);
|
sb.deleteCharAt(sb.length() - 1);
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将参数拼接到指定 URL。
|
||||||
|
*
|
||||||
|
* @param url 原始 URL
|
||||||
|
* @return 拼接后的 URL
|
||||||
|
*/
|
||||||
public String toURL(String url) {
|
public String toURL(String url) {
|
||||||
|
if (isEmpty()) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
StringBuilder sb = new StringBuilder(url);
|
StringBuilder sb = new StringBuilder(url);
|
||||||
if (url.contains("?")) {
|
if (url.contains("?")) {
|
||||||
if (!url.endsWith("?") && !url.endsWith("&")) {
|
if (!url.endsWith("?") && !url.endsWith("&")) {
|
||||||
@@ -42,29 +145,52 @@ public class ArgMap<K, V> extends HashMap<K, V> {
|
|||||||
}
|
}
|
||||||
sb.append(toURL());
|
sb.append(toURL());
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
} else {
|
}
|
||||||
return url + "?" + toURL();
|
return url + "?" + toURL();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用一组参数创建对象。
|
||||||
|
*
|
||||||
|
* @param key 参数名
|
||||||
|
* @param value 参数值
|
||||||
|
* @param <K> key 类型
|
||||||
|
* @param <V> value 类型
|
||||||
|
* @return 参数对象
|
||||||
|
*/
|
||||||
public static <K, V> ArgMap<K, V> of(K key, V value) {
|
public static <K, V> ArgMap<K, V> of(K key, V value) {
|
||||||
ArgMap<K, V> map = new ArgMap<>();
|
return new ArgMap<K, V>().put(key, value);
|
||||||
map.put(key, value);
|
|
||||||
return map;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用两组参数创建对象。
|
||||||
|
*
|
||||||
|
* @param key1 参数名 1
|
||||||
|
* @param value1 参数值 1
|
||||||
|
* @param key2 参数名 2
|
||||||
|
* @param value2 参数值 2
|
||||||
|
* @param <K> key 类型
|
||||||
|
* @param <V> value 类型
|
||||||
|
* @return 参数对象
|
||||||
|
*/
|
||||||
public static <K, V> ArgMap<K, V> of(K key1, V value1, K key2, V value2) {
|
public static <K, V> ArgMap<K, V> of(K key1, V value1, K key2, V value2) {
|
||||||
ArgMap<K, V> map = new ArgMap<>();
|
return new ArgMap<K, V>().put(key1, value1).put(key2, value2);
|
||||||
map.put(key1, value1);
|
|
||||||
map.put(key2, value2);
|
|
||||||
return map;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用三组参数创建对象。
|
||||||
|
*
|
||||||
|
* @param key1 参数名 1
|
||||||
|
* @param value1 参数值 1
|
||||||
|
* @param key2 参数名 2
|
||||||
|
* @param value2 参数值 2
|
||||||
|
* @param key3 参数名 3
|
||||||
|
* @param value3 参数值 3
|
||||||
|
* @param <K> key 类型
|
||||||
|
* @param <V> value 类型
|
||||||
|
* @return 参数对象
|
||||||
|
*/
|
||||||
public static <K, V> ArgMap<K, V> of(K key1, V value1, K key2, V value2, K key3, V value3) {
|
public static <K, V> ArgMap<K, V> of(K key1, V value1, K key2, V value2, K key3, V value3) {
|
||||||
ArgMap<K, V> map = new ArgMap<>();
|
return new ArgMap<K, V>().put(key1, value1).put(key2, value2).put(key3, value3);
|
||||||
map.put(key1, value1);
|
|
||||||
map.put(key2, value2);
|
|
||||||
map.put(key3, value3);
|
|
||||||
return map;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.imyeyu.network;
|
|||||||
import lombok.AccessLevel;
|
import lombok.AccessLevel;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
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.util.Timeout;
|
import org.apache.hc.core5.util.Timeout;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -49,6 +50,11 @@ public class CommonRequest {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CommonRequest bodyEntity(HttpEntity entity) {
|
||||||
|
request.body(entity);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public String asString() throws IOException {
|
public String asString() throws IOException {
|
||||||
return request.execute().returnContent().asString();
|
return request.execute().returnContent().asString();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.imyeyu.network;
|
|||||||
import com.imyeyu.io.IO;
|
import com.imyeyu.io.IO;
|
||||||
import org.apache.hc.client5.http.fluent.Request;
|
import org.apache.hc.client5.http.fluent.Request;
|
||||||
import org.apache.hc.core5.http.Header;
|
import org.apache.hc.core5.http.Header;
|
||||||
|
import org.apache.hc.core5.http.HttpEntity;
|
||||||
import org.apache.hc.core5.http.HttpResponse;
|
import org.apache.hc.core5.http.HttpResponse;
|
||||||
import org.apache.hc.core5.http.ProtocolException;
|
import org.apache.hc.core5.http.ProtocolException;
|
||||||
|
|
||||||
@@ -16,7 +17,7 @@ import java.nio.file.Path;
|
|||||||
* @author 夜雨
|
* @author 夜雨
|
||||||
* @since 2025-06-26 13:03
|
* @since 2025-06-26 13:03
|
||||||
*/
|
*/
|
||||||
public class FileRequest extends CommonRequest {
|
public class FileRequest extends GsonRequest {
|
||||||
|
|
||||||
protected FileRequest(Request request) {
|
protected FileRequest(Request request) {
|
||||||
super(request);
|
super(request);
|
||||||
@@ -58,6 +59,18 @@ public class FileRequest extends CommonRequest {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileRequest body(Object object) {
|
||||||
|
super.body(object);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileRequest bodyEntity(HttpEntity entity) {
|
||||||
|
super.bodyEntity(entity);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public void toFile(String path, String fileName) throws IOException, NoPermissionException {
|
public void toFile(String path, String fileName) throws IOException, NoPermissionException {
|
||||||
toFile(new File(IO.fitPath(path) + fileName));
|
toFile(new File(IO.fitPath(path) + fileName));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import com.google.gson.reflect.TypeToken;
|
|||||||
import com.imyeyu.java.TimiJava;
|
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.core5.http.ContentType;
|
import org.apache.hc.core5.http.ContentType;
|
||||||
|
import org.apache.hc.core5.http.HttpEntity;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@@ -75,6 +76,12 @@ public class GsonRequest extends CommonRequest {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GsonRequest bodyEntity(HttpEntity entity) {
|
||||||
|
super.bodyEntity(entity);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public <T> T resultAs(Class<T> clazz) throws IOException {
|
public <T> T resultAs(Class<T> clazz) throws IOException {
|
||||||
return getGson().fromJson(super.asString(), clazz);
|
return getGson().fromJson(super.asString(), clazz);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,6 +66,18 @@ public class ProgressiveRequest extends FileRequest {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ProgressiveRequest body(Object object) {
|
||||||
|
super.body(object);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ProgressiveRequest bodyEntity(HttpEntity entity) {
|
||||||
|
super.bodyEntity(entity);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
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()));
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.imyeyu.network;
|
|||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import com.imyeyu.java.bean.timi.TimiResponse;
|
import com.imyeyu.java.bean.timi.TimiResponse;
|
||||||
import org.apache.hc.client5.http.fluent.Request;
|
import org.apache.hc.client5.http.fluent.Request;
|
||||||
|
import org.apache.hc.core5.http.HttpEntity;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@@ -58,6 +59,12 @@ public class TimiRequest extends GsonRequest {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TimiRequest bodyEntity(HttpEntity entity) {
|
||||||
|
super.bodyEntity(entity);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String asString() throws IOException {
|
public String asString() throws IOException {
|
||||||
return resultAs(String.class);
|
return resultAs(String.class);
|
||||||
|
|||||||
@@ -24,4 +24,13 @@ public class ArgMapTest {
|
|||||||
String url = ArgMap.of("newKey", "newValue").toURL("http://localhost/test" + uri);
|
String url = ArgMap.of("newKey", "newValue").toURL("http://localhost/test" + uri);
|
||||||
assert url.equals("http://localhost/test/detail?id=123&key=value&newKey=newValue");
|
assert url.equals("http://localhost/test/detail?id=123&key=value&newKey=newValue");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void duplicateKeyTest() {
|
||||||
|
ArgMap<String, String> argMap = new ArgMap<>();
|
||||||
|
argMap.put("id", "1");
|
||||||
|
argMap.put("id", "2");
|
||||||
|
assert argMap.toURL().equals("id=1&id=2");
|
||||||
|
assert argMap.toURL("/detail").equals("/detail?id=1&id=2");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user