com.aliyun.datahub.client.http.interceptor.HttpAuthInterceptor Maven / Gradle / Ivy
The newest version!
package com.aliyun.datahub.client.http.interceptor;
import com.aliyun.datahub.client.auth.Account;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
public class HttpAuthInterceptor implements Interceptor {
private final Account account;
public HttpAuthInterceptor( Account account) {
this.account = account;
}
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request request = addAuthHeaders(chain.request());
return chain.proceed(request);
}
private Request addAuthHeaders(Request request) {
if (this.account == null) {
return request;
}
Request.Builder reqBuilder = request.newBuilder();
account.addAuthHeaders(reqBuilder);
return reqBuilder.build();
}
}