1
1
package io .github .admin4j .http .util ;
2
2
3
3
import io .github .admin4j .http .ApiClient ;
4
+ import io .github .admin4j .http .HttpRequest ;
4
5
import io .github .admin4j .http .core .HttpDefaultConfig ;
5
6
import io .github .admin4j .http .core .MediaTypeEnum ;
6
7
import io .github .admin4j .http .core .Pair ;
7
8
import lombok .Cleanup ;
9
+ import okhttp3 .Call ;
10
+ import okhttp3 .Callback ;
8
11
import okhttp3 .Response ;
9
12
import org .apache .commons .lang3 .StringUtils ;
10
13
@@ -45,6 +48,7 @@ public static void setClient(ApiClient apiJsonClient) {
45
48
46
49
/**
47
50
* get 请求
51
+ *
48
52
* @param url
49
53
* @param queryParams 查询参数
50
54
* @return
@@ -57,6 +61,7 @@ public static Response get(String url, Pair<?>... queryParams) {
57
61
58
62
/**
59
63
* get 请求
64
+ *
60
65
* @param url
61
66
* @param queryParams 查询参数
62
67
* @return
@@ -69,6 +74,7 @@ public static Response get(String url, Map<String, Object> queryParams) {
69
74
70
75
/**
71
76
* post 请求
77
+ *
72
78
* @param url
73
79
* @param body post body 体
74
80
* @return
@@ -79,8 +85,9 @@ public static Response post(String url, Object body) {
79
85
80
86
/**
81
87
* post(application/json;) 请求
88
+ *
82
89
* @param url
83
- * @param body post body 体
90
+ * @param body post body 体
84
91
* @param header header 头
85
92
* @return
86
93
*/
@@ -89,7 +96,8 @@ public static Response post(String url, Object body, Map<String, Object> header)
89
96
}
90
97
91
98
/**
92
- * form(x-www-form-urlencoded) 格式的 post 请求
99
+ * form(x-www-form-urlencoded) 格式的 post 请求
100
+ *
93
101
* @param url
94
102
* @param formParams
95
103
* @return
@@ -100,6 +108,7 @@ public static Response postForm(String url, Map<String, Object> formParams) {
100
108
101
109
/**
102
110
* form(x-www-form-urlencoded) 格式的 post 请求
111
+ *
103
112
* @param url
104
113
* @param formParams
105
114
* @param header
@@ -111,6 +120,7 @@ public static Response postForm(String url, Map<String, Object> formParams, Map<
111
120
112
121
/**
113
122
* put 请求
123
+ *
114
124
* @param url
115
125
* @param body
116
126
* @return
@@ -121,6 +131,7 @@ public static Response put(String url, Object body) {
121
131
122
132
/**
123
133
* put 请求
134
+ *
124
135
* @param url
125
136
* @param body
126
137
* @param header
@@ -132,6 +143,7 @@ public static Response put(String url, Object body, Map<String, Object> header)
132
143
133
144
/**
134
145
* form(x-www-form-urlencoded) 格式的 put 请求
146
+ *
135
147
* @param url
136
148
* @param formParams
137
149
* @return
@@ -142,6 +154,7 @@ public static Response putForm(String url, Map<String, Object> formParams) {
142
154
143
155
/**
144
156
* form(x-www-form-urlencoded) 格式的 put 请求
157
+ *
145
158
* @param url
146
159
* @param formParams
147
160
* @return
@@ -152,6 +165,7 @@ public static Response putForm(String url, Map<String, Object> formParams, Map<S
152
165
153
166
/**
154
167
* delete 请求
168
+ *
155
169
* @param url
156
170
* @param body
157
171
* @return
@@ -162,6 +176,7 @@ public static Response delete(String url, Object body) {
162
176
163
177
/**
164
178
* delete 请求
179
+ *
165
180
* @param url
166
181
* @param body
167
182
* @return
@@ -172,6 +187,7 @@ public static Response delete(String url, Object body, Map<String, Object> heade
172
187
173
188
/**
174
189
* form(x-www-form-urlencoded) 格式的 delete 请求
190
+ *
175
191
* @param url
176
192
* @param formParams
177
193
* @return
@@ -182,6 +198,7 @@ public static Response deleteForm(String url, Map<String, Object> formParams) {
182
198
183
199
/**
184
200
* form(x-www-form-urlencoded) 格式的 delete 请求
201
+ *
185
202
* @param url
186
203
* @param formParams
187
204
* @return
@@ -192,6 +209,7 @@ public static Response deleteForm(String url, Map<String, Object> formParams, Ma
192
209
193
210
/**
194
211
* form-data(multipart/form-data) 格式的 post 请求
212
+ *
195
213
* @param url
196
214
* @param formParams
197
215
* @return
@@ -204,9 +222,10 @@ public static Response postFormData(String url, Map<String, Object> formParams,
204
222
return getClient ().post (url , MediaTypeEnum .FORM_DATA , null , formParams , header );
205
223
}
206
224
207
-
225
+
208
226
/**
209
227
* form-data(multipart/form-data) 格式的 post 请求
228
+ *
210
229
* @param url
211
230
* @param formParams
212
231
* @return
@@ -250,4 +269,29 @@ public static void down(String url, String savePath) throws IOException {
250
269
fileOutputStream .write (b );
251
270
}
252
271
}
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
+ }
253
297
}
0 commit comments