Skip to content

Commit

Permalink
[API-7331] Update sift-java with Score percentiles (Scores API) (#98)
Browse files Browse the repository at this point in the history
* [API-7331] Update sift-java with Score percentiles (Scores API)
  • Loading branch information
mjouahri-sift authored Oct 2, 2023
1 parent 17d8d70 commit de08aad
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGES.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
3.13.0 (2023-09-19)
=================
- Add support for score percentiles in score API

3.12.0 (2023-09-01)
=================
- Add support for Verification 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.12.0</version>
<version>3.13.0</version>
</dependency>
```
### Gradle
```
dependencies {
compile 'com.siftscience:sift-java:3.12.0'
compile 'com.siftscience:sift-java:3.13.0'
}
```
### 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.12.0'
version = '3.13.0'

repositories {
mavenCentral()
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.12.0";
public static final String LIB_VERSION = "3.13.0";
public static final String USER_AGENT_HEADER = String.format("SiftScience/%s sift-java/%s", API_VERSION, LIB_VERSION);
}
8 changes: 1 addition & 7 deletions src/main/java/com/siftscience/EventRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -49,12 +47,8 @@ protected HttpUrl path(HttpUrl baseUrl) {
if (forceWorkflowRun) {
builder.addQueryParameter("force_workflow_run", "true");
}
Set<String> fields = new HashSet<>();
if (returnScorePercentiles) {
fields.add("score_percentiles");
}
if (fields.size() > 0) {
builder.addQueryParameter("fields", StringUtils.joinWithComma(fields));
builder.addQueryParameter("fields", "score_percentiles");
}

// returnScore and abuseTypes are encoded into the URL as query params rather than JSON.
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/siftscience/ScoreRequest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.siftscience;

import java.io.IOException;

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

import java.io.IOException;

/**
* ScoreRequest is the request type of Sift Score API requests.
* https://siftscience.com/developers/docs/curl/score-api
Expand Down Expand Up @@ -45,6 +45,9 @@ protected HttpUrl path(HttpUrl baseUrl) {
builder.addQueryParameter("abuse_types",
StringUtils.joinWithComma(scoreFieldSet.getAbuseTypes()));
}
if (scoreFieldSet.isReturnScorePercentiles()) {
builder.addQueryParameter("fields", "score_percentiles");
}
return builder.build();
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/siftscience/model/ScoreFieldSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class ScoreFieldSet extends FieldSet<ScoreFieldSet> {
@Expose @SerializedName(USER_ID) private String userId;
private List<String> abuseTypes;

private boolean returnScorePercentiles = false;

public String getUserId() {
return userId;
}
Expand All @@ -28,4 +30,12 @@ public ScoreFieldSet setAbuseTypes(List<String> abuseTypes) {
return this;
}

public boolean isReturnScorePercentiles() {
return returnScorePercentiles;
}

public ScoreFieldSet setReturnScorePercentiles(boolean returnScorePercentiles) {
this.returnScorePercentiles = returnScorePercentiles;
return this;
}
}
17 changes: 13 additions & 4 deletions src/test/java/com/siftscience/ScoresTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,14 @@ public void testScoresAPI() throws IOException, InterruptedException, JSONExcept
" \"users\": \"a, b, c, d\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" },\n" +
" ],\n" +
" \"percentiles\": {\n"+
" \"last_7_days\": -1.0,\n"+
" \"last_1_days\": -1.0,\n"+
" \"last_10_days\": 0.019955654101995565,\n"+
" \"last_5_days\": -1.0\n"+
" }\n"+
" },\n" +
" \"promotion_abuse\": {\n" +
" \"score\": 0.472838192111,\n" +
" \"reasons\": []\n" +
Expand Down Expand Up @@ -268,21 +274,24 @@ public void testScoresAPI() throws IOException, InterruptedException, JSONExcept
ScoreRequest request = client.buildRequest(
new ScoreFieldSet()
.setUserId("billy_jones_301")
.setAbuseTypes(abuseTypes));
.setAbuseTypes(abuseTypes)
.setReturnScorePercentiles(true));
ScoreResponse siftResponse = request.send();


// Verify the request.
RecordedRequest request1 = server.takeRequest();
Assert.assertEquals("GET", request1.getMethod());
Assert.assertEquals("/v205/score/billy_jones_301?api_key=YOUR_API_KEY&" +
"abuse_types=payment_abuse,promotion_abuse", request1.getPath());
"abuse_types=payment_abuse,promotion_abuse&fields=score_percentiles", request1.getPath());

// Verify the response.
Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode());
Assert.assertEquals(0, (int) siftResponse.getBody().getStatus());
JSONAssert.assertEquals(response.getBody().readUtf8(),
siftResponse.getBody().toJson(), false);
Assert.assertEquals(expectedPercentiles(),
siftResponse.getScoreResponse("payment_abuse").getPercentiles());

server.shutdown();

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/siftscience/SiftRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void testUserAgentHeader() throws Exception {

// Verify the request.
RecordedRequest recordedRequest = server.takeRequest();
Assert.assertEquals("SiftScience/v205 sift-java/3.12.0", recordedRequest.getHeader("User-Agent"));
Assert.assertEquals("SiftScience/v205 sift-java/3.13.0", recordedRequest.getHeader("User-Agent"));
}

}

0 comments on commit de08aad

Please sign in to comment.