|
|
@ -1,17 +1,20 @@
|
|
|
|
package ${domain};
|
|
|
|
package ${domain}.frame.client;
|
|
|
|
|
|
|
|
|
|
|
|
import ${domain}.frame.utils.AESUtil;
|
|
|
|
|
|
|
|
import ${domain}.frame.base.ApiRequest;
|
|
|
|
|
|
|
|
import ${domain}.frame.base.ApiResponse;
|
|
|
|
|
|
|
|
import ${domain}.frame.base.ErrorType;
|
|
|
|
|
|
|
|
import ${domain}.frame.utils.MD5Util;
|
|
|
|
|
|
|
|
import ${domain}.frame.utils.MapperUtil;
|
|
|
|
|
|
|
|
import ${domain}.frame.okhttp3.ProgressRequestBody;
|
|
|
|
|
|
|
|
import okhttp3.MultipartBody;
|
|
|
|
import okhttp3.MultipartBody;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
import okhttp3.Request;
|
|
|
|
import okhttp3.Request;
|
|
|
|
import okhttp3.RequestBody;
|
|
|
|
import okhttp3.RequestBody;
|
|
|
|
import okhttp3.Response;
|
|
|
|
import okhttp3.Response;
|
|
|
|
|
|
|
|
import ${domain}.frame.base.BaseRequest;
|
|
|
|
|
|
|
|
import ${domain}.frame.base.BaseResponse;
|
|
|
|
|
|
|
|
import ${domain}.frame.base.ErrorType;
|
|
|
|
|
|
|
|
import ${domain}.frame.client.CallAfter;
|
|
|
|
|
|
|
|
import ${domain}.frame.client.CallBefore;
|
|
|
|
|
|
|
|
import ${domain}.frame.client.Callback;
|
|
|
|
|
|
|
|
import ${domain}.frame.okhttp3.ProgressRequestBody;
|
|
|
|
|
|
|
|
import ${domain}.frame.utils.AESUtil;
|
|
|
|
|
|
|
|
import ${domain}.frame.utils.MD5Util;
|
|
|
|
|
|
|
|
import ${domain}.frame.utils.MapperUtil;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.ConnectException;
|
|
|
|
import java.net.ConnectException;
|
|
|
@ -21,28 +24,19 @@ import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* ApiTest - 测试用例
|
|
|
|
* ApiClient - 请求客户端
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author ${author?default("")}
|
|
|
|
* @author
|
|
|
|
* @version 0.0.1
|
|
|
|
* @version 0.0.1
|
|
|
|
* @since ${.now?string["yyyy-MM-dd"]}
|
|
|
|
* @since 2020-06-18
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class ApiClient {
|
|
|
|
public class ApiClient {
|
|
|
|
private static ApiClient ourInstance = null;
|
|
|
|
public static ApiClient getInstance(String serverUrl, String appKey, String appSecret) {
|
|
|
|
|
|
|
|
return new ApiClient(serverUrl, appKey, appSecret, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT);
|
|
|
|
public static void init(String serverUrl, String appKey, String appSecret) {
|
|
|
|
|
|
|
|
init(serverUrl, appKey, appSecret, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void init(String serverUrl, String appKey, String appSecret, int connectTimeout, int readTimeout) {
|
|
|
|
public static ApiClient getInstance(String serverUrl, String appKey, String appSecret, int connectTimeout, int readTimeout) {
|
|
|
|
ourInstance = new ApiClient(serverUrl, appKey, appSecret, connectTimeout, readTimeout);
|
|
|
|
return new ApiClient(serverUrl, appKey, appSecret, connectTimeout, readTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static ApiClient getInstance() {
|
|
|
|
|
|
|
|
if (ourInstance == null) {
|
|
|
|
|
|
|
|
System.err.print("ApiClient need init");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ourInstance;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//应用码
|
|
|
|
//应用码
|
|
|
@ -56,8 +50,8 @@ public class ApiClient {
|
|
|
|
private static final int DEFAULT_CONNECT_TIMEOUT = 5;//秒
|
|
|
|
private static final int DEFAULT_CONNECT_TIMEOUT = 5;//秒
|
|
|
|
private static final int DEFAULT_READ_TIMEOUT = 10;//秒
|
|
|
|
private static final int DEFAULT_READ_TIMEOUT = 10;//秒
|
|
|
|
|
|
|
|
|
|
|
|
private Before before = null;
|
|
|
|
private CallBefore callBefore = null;
|
|
|
|
private After after = null;
|
|
|
|
private CallAfter callAfter = null;
|
|
|
|
private String token = "";
|
|
|
|
private String token = "";
|
|
|
|
private boolean debug = false;
|
|
|
|
private boolean debug = false;
|
|
|
|
|
|
|
|
|
|
|
@ -71,33 +65,21 @@ public class ApiClient {
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public interface Callback<T extends ApiResponse> {
|
|
|
|
public void setCallAfter(CallAfter callAfter) {
|
|
|
|
void call(T response);
|
|
|
|
this.callAfter = callAfter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface Before {
|
|
|
|
|
|
|
|
void call(ApiRequest request);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface After {
|
|
|
|
|
|
|
|
void call(ApiRequest request, ApiResponse response);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setAfter(After after) {
|
|
|
|
|
|
|
|
this.after = after;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setBefore(Before before) {
|
|
|
|
public void setCallBefore(CallBefore callBefore) {
|
|
|
|
this.before = before;
|
|
|
|
this.callBefore = callBefore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public <T extends ApiResponse> T execute(ApiRequest<T> request) {
|
|
|
|
public <T extends BaseResponse> T execute(BaseRequest<T> request) {
|
|
|
|
return execute(request, null);
|
|
|
|
return execute(request, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public <T extends ApiResponse> T execute(ApiRequest<T> request, ProgressRequestBody.ProgressListener listener) {
|
|
|
|
public <T extends BaseResponse> T execute(BaseRequest<T> request, ProgressRequestBody.ProgressListener listener) {
|
|
|
|
if (before != null) {
|
|
|
|
if (callBefore != null) {
|
|
|
|
before.call(request);
|
|
|
|
callBefore.call(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (debug) {
|
|
|
|
if (debug) {
|
|
|
@ -152,14 +134,14 @@ public class ApiClient {
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
t.addError(ErrorType.SYSTEM_ERROR, "请求异常!");
|
|
|
|
t.addError(ErrorType.SYSTEM_ERROR, "请求异常!");
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
if (after != null) {
|
|
|
|
if (callAfter != null) {
|
|
|
|
after.call(request, t);
|
|
|
|
callAfter.call(request, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public <T extends ApiResponse> void executeCall(ApiRequest<T> request, Callback callback) {
|
|
|
|
public <T extends BaseResponse> void executeCall(BaseRequest<T> request, Callback callback) {
|
|
|
|
ExecutorService executorService = Executors.newFixedThreadPool(1);
|
|
|
|
ExecutorService executorService = Executors.newFixedThreadPool(1);
|
|
|
|
executorService.execute(new Runnable() {
|
|
|
|
executorService.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -170,7 +152,7 @@ public class ApiClient {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public <T extends ApiResponse> void executeCall(ApiRequest<T> request, Callback callback, ProgressRequestBody.ProgressListener listener) {
|
|
|
|
public <T extends BaseResponse> void executeCall(BaseRequest<T> request, Callback callback, ProgressRequestBody.ProgressListener listener) {
|
|
|
|
ExecutorService executorService = Executors.newFixedThreadPool(1);
|
|
|
|
ExecutorService executorService = Executors.newFixedThreadPool(1);
|
|
|
|
executorService.execute(new Runnable() {
|
|
|
|
executorService.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -187,13 +169,13 @@ public class ApiClient {
|
|
|
|
* @param request
|
|
|
|
* @param request
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private String sign(ApiRequest request, String currentTime) {
|
|
|
|
private String sign(BaseRequest request, String currentTime) {
|
|
|
|
String json = MapperUtil.toJson(request);
|
|
|
|
String json = MapperUtil.toJson(request);
|
|
|
|
return MD5Util.encode(appSecret + json + currentTime);
|
|
|
|
return MD5Util.encode(appSecret + json + currentTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setToken(String token) {
|
|
|
|
public void setToken(String token) {
|
|
|
|
this.token = token;
|
|
|
|
this.token = token == null ? "" : token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isDebug() {
|
|
|
|
public boolean isDebug() {
|