Skip to content

Commit

Permalink
API-7698: Fix possible issue with connection becoming stalled after i…
Browse files Browse the repository at this point in the history
…nterrupt during call.execute (#107)

* API-7698: Added workaround to fix SocketTimeException in okHttp

* API-7698: Fixed error handling

* API-7698: Added overloaded constructor with both baseUrl and okHttpClient

* API-7698: Made enqueue requests optional

* API-7698: Don't interrupt

* API-7698: Added test
  • Loading branch information
sbogolii-sift authored May 28, 2024
1 parent 2896bd6 commit 89b106c
Show file tree
Hide file tree
Showing 26 changed files with 259 additions and 114 deletions.
4 changes: 4 additions & 0 deletions CHANGES.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
3.14.2 (2024-05-20)
=================
- Fixed bug with okHttp causing SocketTimeoutException

3.14.1 (2024-05-16)
=================
- Added support for warnings in Events API
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ Java 1.7 or later.
<dependency>
<groupId>com.siftscience</groupId>
<artifactId>sift-java</artifactId>
<version>3.14.1</version>
<version>3.14.2</version>
</dependency>
```
### Gradle
```
dependencies {
compile 'com.siftscience:sift-java:3.14.1'
compile 'com.siftscience:sift-java:3.14.2'
}
```
### Other
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'signing'
apply plugin: 'java-library-distribution'

group = 'com.siftscience'
version = '3.14.1'
version = '3.14.2'

repositories {
mavenCentral()
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/siftscience/ApplyDecisionRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import java.io.IOException;

import com.siftscience.model.ApplyDecisionFieldSet;
import okhttp3.*;
import okhttp3.Credentials;
import okhttp3.HttpUrl;
import okhttp3.Request;

public class ApplyDecisionRequest extends SiftRequest<ApplyDecisionResponse>{

ApplyDecisionRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, ApplyDecisionFieldSet fields) {
super(baseUrl, accountId, okClient, fields);
ApplyDecisionRequest(HttpUrl baseUrl, String accountId, HttpClient httpClient, ApplyDecisionFieldSet fields) {
super(baseUrl, accountId, httpClient, fields);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/siftscience/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public class Constants {

public static final String API_VERSION = "v205";
public static final String LIB_VERSION = "3.14.1";
public static final String LIB_VERSION = "3.14.2";
public static final String USER_AGENT_HEADER = String.format("SiftScience/%s sift-java/%s", API_VERSION, LIB_VERSION);
}
11 changes: 5 additions & 6 deletions src/main/java/com/siftscience/CreateMerchantRequest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.siftscience;

import java.io.IOException;

import okhttp3.Credentials;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Credentials;

import java.io.IOException;


public class CreateMerchantRequest extends SiftMerchantRequest<CreateMerchantResponse> {

CreateMerchantRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, FieldSet fields) {
super(baseUrl, accountId, okClient, fields);
CreateMerchantRequest(HttpUrl baseUrl, String accountId, HttpClient httpClient, FieldSet fields) {
super(baseUrl, accountId, httpClient, fields);
}

@Override
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/siftscience/DecisionStatusRequest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.siftscience;

import com.siftscience.model.DecisionStatusFieldSet;
import okhttp3.*;

import java.io.IOException;

import static com.siftscience.model.DecisionStatusFieldSet.ENTITY_CONTENT;
import static com.siftscience.model.DecisionStatusFieldSet.ENTITY_SESSIONS;
import com.siftscience.model.DecisionStatusFieldSet;
import okhttp3.Credentials;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;

public class DecisionStatusRequest extends SiftRequest<DecisionStatusResponse> {
DecisionStatusRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, DecisionStatusFieldSet fields) {
super(baseUrl, accountId, okClient, fields);
DecisionStatusRequest(HttpUrl baseUrl, String accountId, HttpClient httpClient, DecisionStatusFieldSet fields) {
super(baseUrl, accountId, httpClient, fields);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/siftscience/EventRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class EventRequest extends SiftRequest<EventResponse> {
private boolean returnScorePercentiles = false;
private boolean returnWarnings = false;

EventRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, FieldSet fields) {
super(baseUrl, accountId, okClient, fields);
EventRequest(HttpUrl baseUrl, String accountId, HttpClient client, FieldSet fields) {
super(baseUrl, accountId, client, fields);
abuseTypes = null;
}

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/siftscience/GetDecisionsRequest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.siftscience;

import com.siftscience.model.GetDecisionFieldSet;

import java.io.IOException;

import com.siftscience.model.GetDecisionFieldSet;
import okhttp3.Credentials;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class GetDecisionsRequest extends SiftRequest<GetDecisionsResponse> {

GetDecisionsRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, FieldSet fields) {
super(baseUrl, accountId, okClient, fields);
GetDecisionsRequest(HttpUrl baseUrl, String accountId, HttpClient httpClient, FieldSet fields) {
super(baseUrl, accountId, httpClient, fields);
}

public enum Query {
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/siftscience/GetMerchantRequest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.siftscience;

import com.siftscience.model.GetMerchantFieldSet;
import okhttp3.*;

import java.io.IOException;

import com.siftscience.model.GetMerchantFieldSet;
import okhttp3.Credentials;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;


public class GetMerchantRequest extends SiftMerchantRequest<GetMerchantResponse> {

GetMerchantRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, FieldSet fields) {
super(baseUrl, accountId, okClient, fields);
GetMerchantRequest(HttpUrl baseUrl, String accountId, HttpClient httpClient, FieldSet fields) {
super(baseUrl, accountId, httpClient, fields);
}

@Override
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/siftscience/GetMerchantsRequest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.siftscience;

import com.siftscience.model.GetMerchantsFieldSet;
import okhttp3.*;

import java.io.IOException;

import com.siftscience.model.GetMerchantsFieldSet;
import okhttp3.Credentials;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;


public class GetMerchantsRequest extends SiftMerchantRequest<GetMerchantsResponse> {

GetMerchantsRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, FieldSet fields) {
super(baseUrl, accountId, okClient, fields);
GetMerchantsRequest(HttpUrl baseUrl, String accountId, HttpClient httpClient, FieldSet fields) {
super(baseUrl, accountId, httpClient, fields);
}

private static String DEFAULT_BATCH_SIZE = "1000";
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/siftscience/HttpClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.siftscience;

import java.io.IOException;

import com.siftscience.utils.OkHttpUtils;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HttpClient {

private final OkHttpClient okClient;
private boolean enqueueRequests;

public HttpClient(OkHttpClient okHttpClient) {
this.okClient = okHttpClient;
}

public OkHttpClient getOkClient() {
return okClient;
}

public Response execute(Request request) throws IOException {
return enqueueRequests ? OkHttpUtils.execute(request, okClient) :
okClient.newCall(request).execute();
}

public void enqueueRequests() {
this.enqueueRequests = true;
}
}
9 changes: 4 additions & 5 deletions src/main/java/com/siftscience/LabelRequest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.siftscience;

import java.io.IOException;

import com.siftscience.model.LabelFieldSet;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Response;

import java.io.IOException;

/**
* LabelRequest is the request type for the Sift Labels API.
* https://siftscience.com/developers/docs/curl/labels-api
*/
public class LabelRequest extends SiftRequest<LabelResponse> {
LabelRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, LabelFieldSet fields) {
super(baseUrl, accountId, okClient, fields);
LabelRequest(HttpUrl baseUrl, String accountId, HttpClient httpClient, LabelFieldSet fields) {
super(baseUrl, accountId, httpClient, fields);
}

@Override
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/siftscience/ScoreRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.siftscience.model.ScoreFieldSet;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

Expand All @@ -13,8 +12,8 @@
* https://siftscience.com/developers/docs/curl/score-api
*/
public class ScoreRequest extends SiftRequest<ScoreResponse> {
ScoreRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, ScoreFieldSet fields) {
super(baseUrl, accountId, okClient, fields);
ScoreRequest(HttpUrl baseUrl, String accountId, HttpClient httpClient, ScoreFieldSet fields) {
super(baseUrl, accountId, httpClient, fields);
}

/**
Expand Down
45 changes: 27 additions & 18 deletions src/main/java/com/siftscience/SiftClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
public class SiftClient {
private final String accountId;
private final String apiKey;
private final OkHttpClient okClient;
private final HttpClient httpClient;
private HttpUrl baseUrl = HttpUrl.parse("https://api.sift.com");

public SiftClient(String apiKey, String accountId) {
Expand All @@ -58,7 +58,7 @@ public SiftClient(String apiKey, String accountId) {
public SiftClient(String apiKey, String accountId, OkHttpClient okHttpClient) {
this.apiKey = apiKey;
this.accountId = accountId;
this.okClient = okHttpClient;
this.httpClient = new HttpClient(okHttpClient);
}

/**
Expand All @@ -72,6 +72,11 @@ public SiftClient(String apiKey, String accountId, HttpUrl baseUrl) {
this.baseUrl = baseUrl;
}

public SiftClient(String apiKey, String accountId, HttpUrl baseUrl, OkHttpClient okHttpClient) {
this(apiKey, accountId, okHttpClient);
this.baseUrl = baseUrl;
}

public String getApiKey() {
return apiKey;
}
Expand All @@ -80,84 +85,88 @@ public String getAccountId() {
return accountId;
}

public void enqueueRequests() {
httpClient.enqueueRequests();
}

public EventRequest buildRequest(FieldSet fields) {
setupApiKey(fields);
return new EventRequest(baseUrl, getAccountId(), okClient, fields);
return new EventRequest(baseUrl, getAccountId(), httpClient, fields);
}

public ApplyDecisionRequest buildRequest(ApplyDecisionFieldSet fields) {
setupApiKey(fields);
return new ApplyDecisionRequest(baseUrl, getAccountId(), okClient, fields);
return new ApplyDecisionRequest(baseUrl, getAccountId(), httpClient, fields);
}

public GetDecisionsRequest buildRequest(GetDecisionFieldSet fields) {
setupApiKey(fields);
return new GetDecisionsRequest(baseUrl, getAccountId(), okClient, fields);
return new GetDecisionsRequest(baseUrl, getAccountId(), httpClient, fields);
}

public DecisionStatusRequest buildRequest(DecisionStatusFieldSet fields) {
setupApiKey(fields);
return new DecisionStatusRequest(baseUrl, getAccountId(), okClient, fields);
return new DecisionStatusRequest(baseUrl, getAccountId(), httpClient, fields);
}

public LabelRequest buildRequest(LabelFieldSet fields) {
setupApiKey(fields);
return new LabelRequest(baseUrl, getAccountId(), okClient, fields);
return new LabelRequest(baseUrl, getAccountId(), httpClient, fields);
}

public UnlabelRequest buildRequest(UnlabelFieldSet fields) {
setupApiKey(fields);
return new UnlabelRequest(baseUrl, getAccountId(), okClient, fields);
return new UnlabelRequest(baseUrl, getAccountId(), httpClient, fields);
}

public ScoreRequest buildRequest(ScoreFieldSet fields) {
setupApiKey(fields);
return new ScoreRequest(baseUrl, getAccountId(), okClient, fields);
return new ScoreRequest(baseUrl, getAccountId(), httpClient, fields);
}

public UserScoreRequest buildRequest(UserScoreFieldSet fields) {
setupApiKey(fields);
return new UserScoreRequest(baseUrl, getAccountId(), okClient, fields);
return new UserScoreRequest(baseUrl, getAccountId(), httpClient, fields);
}

public WorkflowStatusRequest buildRequest(WorkflowStatusFieldSet fields) {
setupApiKey(fields);
return new WorkflowStatusRequest(baseUrl, getAccountId(), okClient, fields);
return new WorkflowStatusRequest(baseUrl, getAccountId(), httpClient, fields);
}

public GetMerchantsRequest buildRequest(GetMerchantsFieldSet fields) {
setupApiKey(fields);
return new GetMerchantsRequest(baseUrl, getAccountId(), okClient, fields);
return new GetMerchantsRequest(baseUrl, getAccountId(), httpClient, fields);
}

public GetMerchantRequest buildRequest(GetMerchantFieldSet fields) {
setupApiKey(fields);
return new GetMerchantRequest(baseUrl, getAccountId(), okClient, fields);
return new GetMerchantRequest(baseUrl, getAccountId(), httpClient, fields);
}

public CreateMerchantRequest buildRequest(CreateMerchantFieldSet fields) {
setupApiKey(fields);
return new CreateMerchantRequest(baseUrl, getAccountId(), okClient, fields);
return new CreateMerchantRequest(baseUrl, getAccountId(), httpClient, fields);
}

public UpdateMerchantRequest buildRequest(UpdateMerchantFieldSet fields, String merchantId) {
setupApiKey(fields);
return new UpdateMerchantRequest(baseUrl, getAccountId(), okClient, fields, merchantId);
return new UpdateMerchantRequest(baseUrl, getAccountId(), httpClient, fields, merchantId);
}

public VerificationSendRequest buildRequest(VerificationSendFieldSet fields) {
setupApiKey(fields);
return new VerificationSendRequest(baseUrl, getAccountId(), okClient, fields);
return new VerificationSendRequest(baseUrl, getAccountId(), httpClient, fields);
}

public VerificationResendRequest buildRequest(VerificationResendFieldSet fields) {
setupApiKey(fields);
return new VerificationResendRequest(baseUrl, getAccountId(), okClient, fields);
return new VerificationResendRequest(baseUrl, getAccountId(), httpClient, fields);
}

public VerificationCheckRequest buildRequest(VerificationCheckFieldSet fields) {
setupApiKey(fields);
return new VerificationCheckRequest(baseUrl, getAccountId(), okClient, fields);
return new VerificationCheckRequest(baseUrl, getAccountId(), httpClient, fields);
}

private void setupApiKey(FieldSet fields) {
Expand Down
Loading

0 comments on commit 89b106c

Please sign in to comment.