Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.vscode/
build/
target/
.m2
Empty file modified examples/spring-boot/gradlew
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>co.dapi</groupId>
<artifactId>dapi-java</artifactId>
<version>1.6.1</version>
<version>1.6.2</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/co/dapi/DapiRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okio.Buffer;

public class DapiRequest {
public final static String Dapi_URL = "https://api.dapi.com";
Expand All @@ -25,10 +26,9 @@ public class DapiRequest {
.create();

private final static OkHttpClient httpClient = new OkHttpClient().newBuilder()
.readTimeout(2, TimeUnit.MINUTES)
.addInterceptor(new TimeoutInterceptor(jsonAgent))
.build();


public static Response HandleSDK(String bodyJson, HashMap<String, String> headersMap) throws IOException {
headersMap.put("host", "dd.dapi.com");
return doRequest(bodyJson, DD_URL, headersMap);
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/co/dapi/TimeoutInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package co.dapi;

import com.google.gson.*;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import okio.Buffer;

class TimeoutInterceptor implements Interceptor {
Gson jsonAgent;

public static final int MAX_TIMEOUT = 300;
public static final int MIN_TIMEOUT = 135;

public static final String CREATE_TRANSFER = "payment/transfer/autoflow";
public static final String CREATE_TRANSFER_TO_EXISTING_BENEFICIARY = "payment/transfer/create";
public static final String CREATE_WIRE_TRANSFER = "wire/transfer/autoflow";
public static final String CREATE_WIRE_TRANSFER_TO_EXISTING_BENEFICIARY = "wire/transfer/create";
public static final String CREATE_ACH_PULL_TRANSFER = "ach/pull/create";
public static final String NYMCARD_LOAD_FUNDS = "nymcard/funds/load";

private static final String[] maxTimeoutEndpoints = {
CREATE_TRANSFER,
CREATE_TRANSFER_TO_EXISTING_BENEFICIARY,
CREATE_WIRE_TRANSFER,
CREATE_WIRE_TRANSFER_TO_EXISTING_BENEFICIARY,
CREATE_ACH_PULL_TRANSFER,
NYMCARD_LOAD_FUNDS
};

public static boolean maxTimeoutEnabled(String endpoint) {
for (String maxTimeoutEndpoint : maxTimeoutEndpoints) {
if (endpoint.contains(maxTimeoutEndpoint)) {
return true;
}
}
return false;
}

private static String stringify(final Request request) {
try {
final Request copy = request.newBuilder().build();
final Buffer buffer = new Buffer();
copy.body().writeTo(buffer);
return buffer.readUtf8();
} catch (final IOException e) {
return null;
}
}

public TimeoutInterceptor(Gson jsonAgent) {
this.jsonAgent = jsonAgent;
}

@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
HashMap bodyMap = jsonAgent.fromJson(stringify(request), HashMap.class);
int timeout;

if (bodyMap.containsKey("action") && maxTimeoutEnabled(bodyMap.get("action").toString())) {
timeout = MAX_TIMEOUT;
} else {
timeout = MIN_TIMEOUT;
}

return chain.withReadTimeout(timeout, TimeUnit.SECONDS)
.withConnectTimeout(timeout, TimeUnit.SECONDS)
.withWriteTimeout(timeout, TimeUnit.SECONDS)
.proceed(request);
}
}