com.payu.sdk.api.http.ApiAuthenticator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api-client Show documentation
Show all versions of api-client Show documentation
A fresh implementation of the PayU API Client for Android
The newest version!
package com.payu.sdk.api.http;
import com.squareup.okhttp.Authenticator;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.net.Proxy;
import java.util.logging.Logger;
public class ApiAuthenticator extends PayUAuthenticator implements Authenticator {
private static final int MAX_REDIRECTS = 2;
private static final Logger logger = Logger.getLogger(ApiAuthenticator.class.getName());
public ApiAuthenticator(String apiLogin, String apiKey, String fingerprint) {
super(apiLogin, apiKey, fingerprint);
}
@Override public Request authenticate(Proxy proxy, Response response) throws IOException {
if (responseCount(response) >= MAX_REDIRECTS) {
return null; //Up, many redirects, no authorization...
}
return authRequest(response.request().newBuilder());
}
@Override public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return null;
}
private int responseCount(Response response) {
int result = 1;
while ((response = response.priorResponse()) != null) {
result++;
}
logger.info(String.format("Number of redirects: %s\n", result));
return result;
}
}