com.clinia.internal.interceptors.AuthInterceptor Maven / Gradle / Ivy
package com.clinia.internal.interceptors;
import java.io.IOException;
import javax.annotation.Nonnull;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Response;
public final class AuthInterceptor implements Interceptor {
private static final String HEADER_WORKSPACE_ID = "x-clinia-workspace-id";
private static final String HEADER_API_KEY = "x-clinia-api-key";
private final String workspaceId;
private final String apiKey;
public AuthInterceptor(String workspaceId, String apiKey) {
this.workspaceId = workspaceId;
this.apiKey = apiKey;
}
@Nonnull
@Override
public Response intercept(Chain chain) throws IOException {
okhttp3.Request originalRequest = chain.request();
okhttp3.Request.Builder builder = originalRequest.newBuilder();
Headers headers = originalRequest.headers();
if (headers.get(HEADER_WORKSPACE_ID) == null) {
builder.header(HEADER_WORKSPACE_ID, workspaceId);
}
if (headers.get(HEADER_API_KEY) == null) {
builder.header(HEADER_API_KEY, apiKey);
}
okhttp3.Request newRequest = builder.build();
return chain.proceed(newRequest);
}
}