com.truelayer.java.http.interceptors.IdempotencyKeyGeneratorInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truelayer-java Show documentation
Show all versions of truelayer-java Show documentation
TrueLayer Java SDK for https://truelayer.com
package com.truelayer.java.http.interceptors;
import static org.apache.commons.lang3.ObjectUtils.isEmpty;
import com.truelayer.java.Constants;
import java.io.IOException;
import java.util.UUID;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
public class IdempotencyKeyGeneratorInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!needsIdempotencyGeneration(request)) {
return chain.proceed(request);
}
Request newRequest = request.newBuilder()
.header(Constants.HeaderNames.IDEMPOTENCY_KEY, UUID.randomUUID().toString())
.build();
return chain.proceed(newRequest);
}
private boolean needsIdempotencyGeneration(Request request) {
return isEmpty(request.header(Constants.HeaderNames.IDEMPOTENCY_KEY));
}
}