219 lines
5.3 KiB
Java
219 lines
5.3 KiB
Java
|
package top.octopusyan.common.http;
|
||
|
|
||
|
import okhttp3.*;
|
||
|
import org.jetbrains.annotations.NotNull;
|
||
|
import top.octopusyan.common.http.config.ILogStrategy;
|
||
|
import top.octopusyan.common.http.request.IRequestHandler;
|
||
|
import top.octopusyan.common.http.ssl.HttpSslConfig;
|
||
|
import top.octopusyan.common.http.ssl.HttpSslFactory;
|
||
|
|
||
|
import java.net.MalformedURLException;
|
||
|
import java.net.URL;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.List;
|
||
|
import java.util.concurrent.TimeUnit;
|
||
|
|
||
|
/**
|
||
|
* @author : octopus yan
|
||
|
* @email : octopus_yan@foxmail.com
|
||
|
* @description : http 设置
|
||
|
* @create : 2022-4-1 15:44
|
||
|
*/
|
||
|
public class HttpConfig {
|
||
|
|
||
|
private static volatile HttpConfig sConfig;
|
||
|
|
||
|
/** 服务器地址 */
|
||
|
private String serverPath;
|
||
|
|
||
|
/** 请求处理器 */
|
||
|
private IRequestHandler mHandler;
|
||
|
|
||
|
/** 日志打印策略 */
|
||
|
private ILogStrategy mLogStrategy;
|
||
|
|
||
|
/** OkHttp 客户端 */
|
||
|
private OkHttpClient mClient;
|
||
|
|
||
|
/** 通用参数 */
|
||
|
private HashMap<String, Object> mParams;
|
||
|
/** 通用请求头 */
|
||
|
private HashMap<String, String> mHeaders;
|
||
|
|
||
|
/** 日志开关 */
|
||
|
private boolean mLogEnabled = true;
|
||
|
/** 日志 TAG */
|
||
|
private String mLogTag = "EasyHttp";
|
||
|
|
||
|
/** 重试次数 */
|
||
|
private int mRetryCount;
|
||
|
/** 重试时间 */
|
||
|
private long mRetryTime = 2000;
|
||
|
|
||
|
private HttpConfig(OkHttpClient client) {
|
||
|
mClient = client;
|
||
|
mParams = new HashMap<String, Object>();
|
||
|
mHeaders = new HashMap<>();
|
||
|
}
|
||
|
private HttpConfig() {
|
||
|
mParams = new HashMap<String, Object>();
|
||
|
mHeaders = new HashMap<>();
|
||
|
initHttpClient();
|
||
|
}
|
||
|
|
||
|
public static HttpConfig getInstance() {
|
||
|
if (sConfig == null) {
|
||
|
// 当前没有初始化配置
|
||
|
throw new IllegalStateException("You haven't initialized the configuration yet");
|
||
|
}
|
||
|
return sConfig;
|
||
|
}
|
||
|
|
||
|
private static void setInstance(HttpConfig config) {
|
||
|
sConfig = config;
|
||
|
}
|
||
|
|
||
|
public static HttpConfig with(OkHttpClient client) {
|
||
|
return new HttpConfig(client);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 无参初始化 默认配置生成 okhttpclient
|
||
|
*/
|
||
|
public static HttpConfig init() {
|
||
|
return new HttpConfig();
|
||
|
}
|
||
|
|
||
|
public HttpConfig setParams(HashMap<String, Object> params) {
|
||
|
if (params == null) {
|
||
|
params = new HashMap<String, Object>();
|
||
|
}
|
||
|
mParams = params;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public HttpConfig setHeaders(HashMap<String, String> headers) {
|
||
|
if (headers == null) {
|
||
|
headers = new HashMap<>();
|
||
|
}
|
||
|
mHeaders = headers;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public HashMap<String, Object> getParams() {
|
||
|
return mParams;
|
||
|
}
|
||
|
|
||
|
public HashMap<String, String> getHeaders() {
|
||
|
return mHeaders;
|
||
|
}
|
||
|
|
||
|
public HttpConfig addHeader(String key, String value) {
|
||
|
if (key != null && value != null) {
|
||
|
mHeaders.put(key, value);
|
||
|
}
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public HttpConfig addParam(String key, String value) {
|
||
|
if (key != null && value != null) {
|
||
|
mParams.put(key, value);
|
||
|
}
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public OkHttpClient getClient() {
|
||
|
return mClient;
|
||
|
}
|
||
|
|
||
|
public HttpConfig setLogStrategy(ILogStrategy strategy) {
|
||
|
mLogStrategy = strategy;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public HttpConfig setLogEnabled(boolean enabled) {
|
||
|
mLogEnabled = enabled;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public HttpConfig setLogTag(String tag) {
|
||
|
mLogTag = tag;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public HttpConfig setHandler(IRequestHandler mHandler) {
|
||
|
this.mHandler = mHandler;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public HttpConfig setRetryCount(int count) {
|
||
|
if (count < 0) {
|
||
|
// 重试次数必须大于等于 0 次
|
||
|
throw new IllegalArgumentException("The number of retries must be greater than 0");
|
||
|
}
|
||
|
mRetryCount = count;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public HttpConfig serverPath(String serverPath) {
|
||
|
this.serverPath = serverPath;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public HttpConfig setRetryTime(long time) {
|
||
|
if (time < 0) {
|
||
|
// 重试时间必须大于等于 0 毫秒
|
||
|
throw new IllegalArgumentException("The retry time must be greater than 0");
|
||
|
}
|
||
|
mRetryTime = time;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public ILogStrategy getLogStrategy() {
|
||
|
return mLogStrategy;
|
||
|
}
|
||
|
|
||
|
public boolean isLogEnabled() {
|
||
|
return mLogEnabled && mLogStrategy != null;
|
||
|
}
|
||
|
|
||
|
public String getLogTag() {
|
||
|
return mLogTag;
|
||
|
}
|
||
|
|
||
|
public int getRetryCount() {
|
||
|
return mRetryCount;
|
||
|
}
|
||
|
|
||
|
public long getRetryTime() {
|
||
|
return mRetryTime;
|
||
|
}
|
||
|
|
||
|
public String getServerPath() {
|
||
|
return serverPath;
|
||
|
}
|
||
|
|
||
|
public IRequestHandler getHandler() {
|
||
|
return mHandler;
|
||
|
}
|
||
|
|
||
|
public void into() {
|
||
|
if (mClient == null)
|
||
|
throw new IllegalArgumentException("The OkHttp client object cannot be empty");
|
||
|
|
||
|
try {
|
||
|
// 校验主机和路径的 url 是否合法
|
||
|
new URL(serverPath);
|
||
|
} catch (MalformedURLException e) {
|
||
|
throw new IllegalArgumentException("The configured host path url address is not correct");
|
||
|
}
|
||
|
|
||
|
HttpConfig.setInstance(this);
|
||
|
}
|
||
|
|
||
|
private void initHttpClient() {
|
||
|
;
|
||
|
}
|
||
|
}
|