提交 239677f1 编写于 作者: 如梦技术's avatar 如梦技术 🐛

优化拦截器,支持多个,将日志拦截器放到最后

上级 d8d47ea5
......@@ -32,6 +32,8 @@ import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
......@@ -53,7 +55,7 @@ public class HttpRequest {
private Boolean followSslRedirects;
private HttpLoggingInterceptor.Level level;
private CookieJar cookieJar;
private Interceptor interceptor;
private final List<Interceptor> interceptors = new ArrayList<>();
private Authenticator authenticator;
private Duration connectTimeout;
private Duration readTimeout;
......@@ -205,16 +207,11 @@ public class HttpRequest {
if (this.sslSocketFactory != null && this.trustManager != null) {
builder.sslSocketFactory(sslSocketFactory, trustManager);
}
if (this.level != null && HttpLoggingInterceptor.Level.NONE != this.level) {
builder.addInterceptor(getLoggingInterceptor(level));
} else if (globalLoggingInterceptor != null) {
builder.addInterceptor(globalLoggingInterceptor);
}
if (this.authenticator != null) {
builder.authenticator(authenticator);
}
if (this.interceptor != null) {
builder.addInterceptor(this.interceptor);
if (!this.interceptors.isEmpty()) {
builder.interceptors().addAll(this.interceptors);
}
if (this.cookieJar != null) {
builder.cookieJar(cookieJar);
......@@ -225,6 +222,11 @@ public class HttpRequest {
if (this.followSslRedirects != null) {
builder.followSslRedirects(this.followSslRedirects);
}
if (this.level != null && HttpLoggingInterceptor.Level.NONE != this.level) {
builder.addInterceptor(getLoggingInterceptor(level));
} else if (globalLoggingInterceptor != null) {
builder.addInterceptor(globalLoggingInterceptor);
}
// 设置 User-Agent
this.requestBuilder.header("User-Agent", userAgent);
// url
......@@ -332,7 +334,7 @@ public class HttpRequest {
}
public HttpRequest interceptor(Interceptor interceptor) {
this.interceptor = interceptor;
this.interceptors.add(interceptor);
return this;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册