jp.gopay.sdk.utils.functions.GopayFunctions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gopay-java-sdk Show documentation
Show all versions of gopay-java-sdk Show documentation
Official Gyro-n Payments Java SDK
package jp.gopay.sdk.utils.functions;
import jp.gopay.sdk.builders.Request;
import jp.gopay.sdk.models.errors.GoPayException;
import jp.gopay.sdk.models.errors.TooManyRequestsException;
import jp.gopay.sdk.utils.GoPayCallback;
import jp.gopay.sdk.utils.Sleeper;
import java.io.IOException;
public class GopayFunctions {
public static EndoFunction identity() {
return new EndoFunction() {
@Override
public T apply(T t) throws Exception {
return t;
}
};
}
private static boolean shouldHandle(ErrorHandler handler, Throwable t){
return handler != null && handler.getShouldHandleError().test(t);
}
public static , M2, T2 extends Request> Request compose(
final T1 firstRequest,
final Function onSuccess,
final ErrorHandler errorHandler) {
return new Request(){
@Override
public void dispatch(final GoPayCallback callback) {
firstRequest.dispatch(new GoPayCallback() {
@Override
public void getResponse(M1 response) {
onSuccess.apply(response).dispatch(callback);
}
@Override
public void getFailure(Throwable error) {
if(shouldHandle(errorHandler, error)){
errorHandler.getHandler().apply(error).dispatch(callback);
} else {
callback.getFailure(error);
}
}
});
}
@Override
public M2 dispatch() throws IOException, GoPayException, TooManyRequestsException {
try{
M1 response = firstRequest.dispatch();
return onSuccess.apply(response).dispatch();
} catch(IOException | GoPayException error){
if(shouldHandle(errorHandler, error)){
return errorHandler.getHandler().apply(error).dispatch();
} else {
throw error;
}
}
}
@Override
public M2 dispatch(int maxRetry, Sleeper sleeper) throws IOException, GoPayException, TooManyRequestsException, InterruptedException {
try{
M1 response = firstRequest.dispatch(maxRetry, sleeper);
return onSuccess.apply(response).dispatch(maxRetry, sleeper);
} catch(IOException | GoPayException error){
if(shouldHandle(errorHandler, error)){
return errorHandler.getHandler().apply(error).dispatch(maxRetry, sleeper);
} else {
throw error;
}
}
}
};
}
public static , M2, T2 extends Request> Request compose(
final T1 firstRequest,
final Function onSuccess
){
return compose(firstRequest, onSuccess, null);
}
public static > Request retry(
final T1 request,
final ErrorHandler errorHandler){
return new Request() {
@Override
public void dispatch(final GoPayCallback callback) {
request.dispatch(new GoPayCallback() {
@Override
public void getResponse(M1 response) {
callback.getResponse(response);
}
@Override
public void getFailure(Throwable error) {
if(shouldHandle(errorHandler, error)){
errorHandler.getHandler().apply(error).dispatch(callback);
} else {
callback.getFailure(error);
}
}
});
}
@Override
public M1 dispatch() throws IOException, GoPayException, TooManyRequestsException {
try{
return request.dispatch();
} catch(IOException | GoPayException error){
if(shouldHandle(errorHandler, error)){
return errorHandler.getHandler().apply(error).dispatch();
} else {
throw error;
}
}
}
@Override
public M1 dispatch(int maxRetry, Sleeper sleeper) throws IOException, GoPayException, TooManyRequestsException, InterruptedException {
try{
return request.dispatch(maxRetry, sleeper);
} catch(IOException | GoPayException error){
if(shouldHandle(errorHandler, error)){
return errorHandler.getHandler().apply(error).dispatch(maxRetry, sleeper);
} else {
throw error;
}
}
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy