mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-09-20 02:29:44 +08:00
#855 http请求执行器类RequestExecutor接口增加异步执行方法
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddHttpMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpMediaDownloadRequestExecutor;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 下载媒体文件请求执行器,请求的参数是String, 返回的结果是File
|
||||
* 下载媒体文件请求执行器.
|
||||
* 请求的参数是String, 返回的结果是File
|
||||
* 视频文件不支持下载
|
||||
*
|
||||
* @author Daniel Qian
|
||||
@@ -21,6 +24,11 @@ public abstract class BaseMediaDownloadRequestExecutor<H, P> implements RequestE
|
||||
this.tmpDirFile = tmpDirFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, String data, ResponseHandler<File> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
}
|
||||
|
||||
public static RequestExecutor<File, String> create(RequestHttp requestHttp, File tmpDirFile) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
|
@@ -1,14 +1,17 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddHttpMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpMediaUploadRequestExecutor;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 上传媒体文件请求执行器,请求的参数是File, 返回的结果是String
|
||||
* 上传媒体文件请求执行器.
|
||||
* 请求的参数是File, 返回的结果是String
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@@ -19,6 +22,11 @@ public abstract class MediaUploadRequestExecutor<H, P> implements RequestExecuto
|
||||
this.requestHttp = requestHttp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, File data, ResponseHandler<WxMediaUploadResult> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
}
|
||||
|
||||
public static RequestExecutor<WxMediaUploadResult, File> create(RequestHttp requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
|
@@ -18,6 +18,21 @@ public interface RequestExecutor<T, E> {
|
||||
*
|
||||
* @param uri uri
|
||||
* @param data 数据
|
||||
* @return 响应结果
|
||||
* @throws WxErrorException 自定义异常
|
||||
* @throws IOException io异常
|
||||
*/
|
||||
T execute(String uri, E data) throws WxErrorException, IOException;
|
||||
|
||||
|
||||
/**
|
||||
* 执行http请求.
|
||||
*
|
||||
* @param uri uri
|
||||
* @param data 数据
|
||||
* @param handler http响应处理器
|
||||
* @throws WxErrorException 自定义异常
|
||||
* @throws IOException io异常
|
||||
*/
|
||||
void execute(String uri, E data, ResponseHandler<T> handler) throws WxErrorException, IOException;
|
||||
}
|
||||
|
@@ -2,21 +2,30 @@ package me.chanjar.weixin.common.util.http;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/4/22.
|
||||
*
|
||||
* @author ecoolper
|
||||
*/
|
||||
public interface RequestHttp<H, P> {
|
||||
|
||||
/**
|
||||
* 返回httpClient
|
||||
* 返回httpClient.
|
||||
*
|
||||
* @return 返回httpClient
|
||||
*/
|
||||
H getRequestHttpClient();
|
||||
|
||||
/**
|
||||
* 返回httpProxy
|
||||
* 返回httpProxy.
|
||||
*
|
||||
* @return 返回httpProxy
|
||||
*/
|
||||
P getRequestHttpProxy();
|
||||
|
||||
/**
|
||||
* 返回HttpType.
|
||||
*
|
||||
* @return HttpType
|
||||
*/
|
||||
HttpType getRequestType();
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,19 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* http请求响应回调处理接口.
|
||||
* Created by Binary Wang on 2018/12/8.
|
||||
* </pre>
|
||||
*
|
||||
* @param <T> 返回值类型
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public interface ResponseHandler<T> {
|
||||
/**
|
||||
* 响应结果处理.
|
||||
*
|
||||
* @param t 要处理的对象
|
||||
*/
|
||||
void handle(T t);
|
||||
}
|
@@ -1,11 +1,15 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientSimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddHttpSimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpSimpleGetRequestExecutor;
|
||||
|
||||
/**
|
||||
* 简单的GET请求执行器,请求的参数是String, 返回的结果也是String
|
||||
* 简单的GET请求执行器.
|
||||
* 请求的参数是String, 返回的结果也是String
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@@ -16,6 +20,11 @@ public abstract class SimpleGetRequestExecutor<H, P> implements RequestExecutor<
|
||||
this.requestHttp = requestHttp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, String data, ResponseHandler<String> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
}
|
||||
|
||||
public static RequestExecutor<String, String> create(RequestHttp requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
|
@@ -1,5 +1,8 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheSimplePostRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddHttpSimplePostRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpSimplePostRequestExecutor;
|
||||
@@ -17,6 +20,11 @@ public abstract class SimplePostRequestExecutor<H, P> implements RequestExecutor
|
||||
this.requestHttp = requestHttp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, String data, ResponseHandler<String> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
}
|
||||
|
||||
public static RequestExecutor<String, String> create(RequestHttp requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
|
@@ -1,16 +1,17 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/4.
|
||||
|
@@ -25,7 +25,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
*/
|
||||
public class ApacheMediaDownloadRequestExecutor extends BaseMediaDownloadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
public ApacheMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
|
||||
super(requestHttp, tmpDirFile);
|
||||
}
|
||||
|
Reference in New Issue
Block a user