Skip to content

Commit d15a114

Browse files
authored
Merge pull request #11 from admin4j/feate-dev
feat(http): HttpUtil 支持 send HttpRequest
2 parents 30cac0e + c7b4ae1 commit d15a114

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

admin4j-common-http/src/main/java/io/github/admin4j/http/HttpRequest.java

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.github.admin4j.http.core.Pair;
88
import io.github.admin4j.http.util.HttpUtil;
99
import lombok.Cleanup;
10+
import lombok.Getter;
1011
import lombok.Setter;
1112
import lombok.experimental.Accessors;
1213
import okhttp3.Call;
@@ -22,6 +23,7 @@
2223
* @author andanyang
2324
* @since 2022/10/14 15:43
2425
*/
26+
@Getter
2527
public class HttpRequest {
2628

2729
private Method method;

admin4j-common-http/src/main/java/io/github/admin4j/http/util/HttpUtil.java

+47-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package io.github.admin4j.http.util;
22

33
import io.github.admin4j.http.ApiClient;
4+
import io.github.admin4j.http.HttpRequest;
45
import io.github.admin4j.http.core.HttpDefaultConfig;
56
import io.github.admin4j.http.core.MediaTypeEnum;
67
import io.github.admin4j.http.core.Pair;
78
import lombok.Cleanup;
9+
import okhttp3.Call;
10+
import okhttp3.Callback;
811
import okhttp3.Response;
912
import org.apache.commons.lang3.StringUtils;
1013

@@ -45,6 +48,7 @@ public static void setClient(ApiClient apiJsonClient) {
4548

4649
/**
4750
* get 请求
51+
*
4852
* @param url
4953
* @param queryParams 查询参数
5054
* @return
@@ -57,6 +61,7 @@ public static Response get(String url, Pair<?>... queryParams) {
5761

5862
/**
5963
* get 请求
64+
*
6065
* @param url
6166
* @param queryParams 查询参数
6267
* @return
@@ -69,6 +74,7 @@ public static Response get(String url, Map<String, Object> queryParams) {
6974

7075
/**
7176
* post 请求
77+
*
7278
* @param url
7379
* @param body post body 体
7480
* @return
@@ -79,8 +85,9 @@ public static Response post(String url, Object body) {
7985

8086
/**
8187
* post(application/json;) 请求
88+
*
8289
* @param url
83-
* @param body post body 体
90+
* @param body post body 体
8491
* @param header header 头
8592
* @return
8693
*/
@@ -89,7 +96,8 @@ public static Response post(String url, Object body, Map<String, Object> header)
8996
}
9097

9198
/**
92-
* form(x-www-form-urlencoded) 格式的 post 请求
99+
* form(x-www-form-urlencoded) 格式的 post 请求
100+
*
93101
* @param url
94102
* @param formParams
95103
* @return
@@ -100,6 +108,7 @@ public static Response postForm(String url, Map<String, Object> formParams) {
100108

101109
/**
102110
* form(x-www-form-urlencoded) 格式的 post 请求
111+
*
103112
* @param url
104113
* @param formParams
105114
* @param header
@@ -111,6 +120,7 @@ public static Response postForm(String url, Map<String, Object> formParams, Map<
111120

112121
/**
113122
* put 请求
123+
*
114124
* @param url
115125
* @param body
116126
* @return
@@ -121,6 +131,7 @@ public static Response put(String url, Object body) {
121131

122132
/**
123133
* put 请求
134+
*
124135
* @param url
125136
* @param body
126137
* @param header
@@ -132,6 +143,7 @@ public static Response put(String url, Object body, Map<String, Object> header)
132143

133144
/**
134145
* form(x-www-form-urlencoded) 格式的 put 请求
146+
*
135147
* @param url
136148
* @param formParams
137149
* @return
@@ -142,6 +154,7 @@ public static Response putForm(String url, Map<String, Object> formParams) {
142154

143155
/**
144156
* form(x-www-form-urlencoded) 格式的 put 请求
157+
*
145158
* @param url
146159
* @param formParams
147160
* @return
@@ -152,6 +165,7 @@ public static Response putForm(String url, Map<String, Object> formParams, Map<S
152165

153166
/**
154167
* delete 请求
168+
*
155169
* @param url
156170
* @param body
157171
* @return
@@ -162,6 +176,7 @@ public static Response delete(String url, Object body) {
162176

163177
/**
164178
* delete 请求
179+
*
165180
* @param url
166181
* @param body
167182
* @return
@@ -172,6 +187,7 @@ public static Response delete(String url, Object body, Map<String, Object> heade
172187

173188
/**
174189
* form(x-www-form-urlencoded) 格式的 delete 请求
190+
*
175191
* @param url
176192
* @param formParams
177193
* @return
@@ -182,6 +198,7 @@ public static Response deleteForm(String url, Map<String, Object> formParams) {
182198

183199
/**
184200
* form(x-www-form-urlencoded) 格式的 delete 请求
201+
*
185202
* @param url
186203
* @param formParams
187204
* @return
@@ -192,6 +209,7 @@ public static Response deleteForm(String url, Map<String, Object> formParams, Ma
192209

193210
/**
194211
* form-data(multipart/form-data) 格式的 post 请求
212+
*
195213
* @param url
196214
* @param formParams
197215
* @return
@@ -204,9 +222,10 @@ public static Response postFormData(String url, Map<String, Object> formParams,
204222
return getClient().post(url, MediaTypeEnum.FORM_DATA, null, formParams, header);
205223
}
206224

207-
225+
208226
/**
209227
* form-data(multipart/form-data) 格式的 post 请求
228+
*
210229
* @param url
211230
* @param formParams
212231
* @return
@@ -250,4 +269,29 @@ public static void down(String url, String savePath) throws IOException {
250269
fileOutputStream.write(b);
251270
}
252271
}
272+
273+
//============= 发送http 请求 =================
274+
public static Response send(HttpRequest httpRequest) {
275+
ApiClient apiClient = HttpUtil.getClient();
276+
Call call = apiClient.buildCall(httpRequest.getUrl(), httpRequest.getMethod(), httpRequest.getMediaType(),
277+
httpRequest.getQueryParams(), httpRequest.getQueryMap(), httpRequest.getBody(), httpRequest.getForm(),
278+
httpRequest.getHeaders());
279+
return apiClient.execute(call);
280+
}
281+
282+
283+
/**
284+
* 异步执行
285+
*
286+
* @param httpRequest
287+
* @param callback
288+
*/
289+
public static void asyncExecute(HttpRequest httpRequest, Callback callback) {
290+
291+
ApiClient apiClient = HttpUtil.getClient();
292+
Call call = apiClient.buildCall(httpRequest.getUrl(), httpRequest.getMethod(), httpRequest.getMediaType(),
293+
httpRequest.getQueryParams(), httpRequest.getQueryMap(), httpRequest.getBody(), httpRequest.getForm(),
294+
httpRequest.getHeaders());
295+
apiClient.executeAsync(call, callback);
296+
}
253297
}

0 commit comments

Comments
 (0)