Skip to content

Commit

Permalink
(For David) Adds account id to constructor (#43)
Browse files Browse the repository at this point in the history
* Adds account id to constructor

* Fixes tests

* Adds badge back

* Removes unused method, makes vars final, refactors api key methods

* Adds newline

* Deprecates getRequestBody
  • Loading branch information
garylee1 authored May 14, 2019
1 parent 40e35bc commit 08b4f40
Show file tree
Hide file tree
Showing 52 changed files with 167 additions and 293 deletions.
33 changes: 11 additions & 22 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>2.7.0</version>
<version>3.0.0</version>
</dependency>
```
### Gradle
```
dependencies {
compile 'com.siftscience:sift-java:2.7.0'
compile 'com.siftscience:sift-java:3.0.0'
}
```
### Other
Expand All @@ -34,10 +34,10 @@ $ ./gradlew distZip # Zip file saved to ./build/distributions/

## How To Use

Create a SiftClient object with your API key. SiftClient is thread safe
Create a SiftClient object with your API key and account id. SiftClient is thread-safe
and can be used to access all supported APIs.
```java
SiftClient client = new SiftClient("YOUR_API_KEY");
SiftClient client = new SiftClient("YOUR_API_KEY", "YOUR_ACCOUNT_ID");
```

### Send Events
Expand Down Expand Up @@ -195,31 +195,28 @@ EventRequest createOrderRequest = client.buildRequest(createOrderFieldSet)
To query the Workflow Status API, create a request with a WorkflowStatusFieldSet.
```java
WorkflowStatusRequest request = client.buildRequest(new WorkflowStatusFieldSet()
.setAccountId("your_account_id")
.setWorkflowRunId("someid"));
```

### Apply Decision API

[API Docs](https://sift.com/developers/docs/java/decisions-api/apply-decision)

To apply a decision to a user, create a request with accountId, userId, and ApplyDecisionFieldSet.
To apply a decision to a user, create a request with userId and ApplyDecisionFieldSet.
```java
ApplyDecisionRequest request = client.buildRequest(
new ApplyDecisionFieldSet()
.setAccountId("your_account_id")
.setUserId("a_user_id")
.setDecisionId("decision_id")
.setSource(DecisionSource.AUTOMATED_RULE)
.setDescription("description of decision applied")
);
```

To apply a decision to an order, create a request with accountId, userId, orderId and ApplyDecisionFieldSet.
To apply a decision to an order, create a request with userId, orderId, and ApplyDecisionFieldSet.
```java
ApplyDecisionRequest request = client.buildRequest(
new ApplyDecisionFieldSet()
.setAccountId("your_account_id")
.setUserId("a_user_id")
.setOrderId("a_order_id")
.setDecisionId("decision_id")
Expand All @@ -229,11 +226,10 @@ ApplyDecisionRequest request = client.buildRequest(
);
```

To apply a decision to a session, create a request with accountId, userId, sessionId and ApplyDecisionFieldSet.
To apply a decision to a session, create a request with userId, sessionId, and ApplyDecisionFieldSet.
```java
ApplyDecisionRequest request = client.buildRequest(
new ApplyDecisionFieldSet()
.setAccountId("your_account_id")
.setUserId("a_user_id")
.setSessionId("a_session_id")
.setDecisionId("decision_id")
Expand All @@ -243,11 +239,10 @@ ApplyDecisionRequest request = client.buildRequest(
);
```

To apply a decision to a piece of content, create a request with accountId, userId, contentId and ApplyDecisionFieldSet.
To apply a decision to a piece of content, create a request with userId, contentId, and ApplyDecisionFieldSet.
```java
ApplyDecisionRequest request = client.buildRequest(
new ApplyDecisionFieldSet()
.setAccountId("your_account_id")
.setUserId("a_user_id")
.setContentId("a_content_id")
.setDecisionId("decision_id")
Expand All @@ -265,14 +260,12 @@ ApplyDecisionRequest request = client.buildRequest(
To retrieve available decisions, build a request with a GetDecisionsFieldSet.
```java
GetDecisionsRequest request = client.buildRequest(new GetDecisionsFieldSet()
.setAbuseTypes(ImmutableList.of(AbuseType.PAYMENT_ABUSE, AbuseType.CONTENT_ABUSE))
.setAccountId("your_account_id"));
.setAbuseTypes(ImmutableList.of(AbuseType.PAYMENT_ABUSE, AbuseType.CONTENT_ABUSE)));
```

Additionally, this field set supports filtering on results by entity and abuse type(s).
```java
GetDecisionsRequest request = client.buildRequest(new GetDecisionsFieldSet()
.setAccountId("your_account_id"))
GetDecisionsRequest request = client.buildRequest(new GetDecisionsFieldSet())
.setEntityType(EntityType.ORDER)
.setAbuseTypes(ImmutableList.of(AbuseType.PAYMENT_ABUSE, AbuseType.CONTENT_ABUSE))
```
Expand All @@ -281,8 +274,7 @@ Pagination is also supported, with offset by index (`from`) and limit (`limit`).
The default `limit` is to return up to 100 results.
The default offset value `from` is 0.
```java
GetDecisionsRequest request = client.buildRequest(new GetDecisionsFieldSet()
.setAccountId("your_account_id"))
GetDecisionsRequest request = client.buildRequest(new GetDecisionsFieldSet())
.setFrom(15)
.setLimit(10);
```
Expand All @@ -304,15 +296,13 @@ GetDecisionsRequest nextRequest = client.buildRequest(GetDecisionsFieldSet.fromN
To query the Decision Status API, create a request with a DecisionStatusFieldSet.
```java
DecisionStatusRequest request = client.buildRequest(new DecisionStatusFieldSet()
.setAccountId("your_account_id")
.setEntity(DecisionStatusFieldSet.ENTITY_ORDERS) // or ENTITY_USERS
.setEntityId("someid"));
```

To query the Decision Status API for a Session, create a request with a DecisionStatusFieldSet, including a user id.
```java
DecisionStatusRequest request = client.buildRequest(new DecisionStatusFieldSet()
.setAccountId("your_account_id")
.setEntity(DecisionStatusFieldSet.ENTITY_SESSIONS)
.setUserId("a_user_id")
.setEntityId("a_session_id"));
Expand All @@ -321,7 +311,6 @@ DecisionStatusRequest request = client.buildRequest(new DecisionStatusFieldSet()
To query the Decision Status API for Content, create a request with a DecisionStatusFieldSet, including a user id.
```java
DecisionStatusRequest request = client.buildRequest(new DecisionStatusFieldSet()
.setAccountId("your_account_id")
.setEntity(DecisionStatusFieldSet.ENTITY_CONTENT)
.setUserId("a_user_id")
.setEntityId("a_content_id"));
Expand Down
13 changes: 10 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ apply plugin: 'signing'
apply plugin: 'java-library-distribution'

group = 'com.siftscience'
version = '2.7.0'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '3.0.0'

repositories {
mavenCentral()
}

compileJava {
sourceCompatibility = 1.7
targetCompatibility = 1.7
}

compileTestJava {
sourceCompatibility = 1.8
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'org.skyscreamer', name: 'jsonassert', version: '1.3.0'
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/siftscience/ApplyDecisionRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

public class ApplyDecisionRequest extends SiftRequest<ApplyDecisionResponse>{

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

@Override
protected HttpUrl path(HttpUrl baseUrl) {
HttpUrl.Builder path = baseUrl.newBuilder("v3/accounts")
.addPathSegment(((ApplyDecisionFieldSet) fieldSet).getAccountId())
.addPathSegment(getAccountId())
.addPathSegment("users")
.addPathSegment(((ApplyDecisionFieldSet) fieldSet).getUserId());

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/siftscience/DecisionStatusRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
import static com.siftscience.model.DecisionStatusFieldSet.ENTITY_SESSIONS;

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

@Override
protected HttpUrl path(HttpUrl baseUrl) {
HttpUrl.Builder builder = baseUrl.newBuilder()
.addPathSegment("v3")
.addPathSegment("accounts")
.addPathSegment(((DecisionStatusFieldSet)fieldSet).getAccountId());
.addPathSegment(getAccountId());
String entity = ((DecisionStatusFieldSet)fieldSet).getEntity();
if (entity.equals(ENTITY_CONTENT) || entity.equals(ENTITY_SESSIONS)) {
builder = builder
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/com/siftscience/EventRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class EventRequest extends SiftRequest<EventResponse> {
private boolean isWorkflowStatus = false;
private boolean forceWorkflowRun = false;

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

Expand All @@ -46,12 +46,7 @@ protected HttpUrl path(HttpUrl baseUrl) {

// returnScore and abuseTypes are encoded into the URL as query params rather than JSON.
if (abuseTypes != null && abuseTypes.size() > 0) {
String queryParamVal = "";
for (String abuseType : abuseTypes) {
queryParamVal += (abuseType + ",");
}
builder.addQueryParameter("abuse_types",
queryParamVal.substring(0, queryParamVal.length() - 1));
builder.addQueryParameter("abuse_types", StringUtils.joinWithComma(abuseTypes));
}
return builder.build();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/siftscience/FieldSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ public String toJson() {

private String apiKey;

public String getApiKey() {
protected final String getApiKey() {
return apiKey;
}

public T setApiKey(String apiKey) {
T setApiKey(String apiKey) {
this.apiKey = apiKey;
return (T) this;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/siftscience/GetDecisionsRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

public class GetDecisionsRequest extends SiftRequest<GetDecisionsResponse> {

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

public enum Query {
Expand All @@ -38,7 +38,7 @@ public String toString() {
protected HttpUrl path(HttpUrl baseUrl) {
GetDecisionFieldSet fieldSet = (GetDecisionFieldSet) this.fieldSet;
HttpUrl.Builder path = baseUrl.newBuilder("/v3/accounts")
.addPathSegment(fieldSet.getAccountId())
.addPathSegment(getAccountId())
.addPathSegment("decisions");

if (fieldSet.getEntityType() != null) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/siftscience/LabelRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* https://siftscience.com/developers/docs/curl/labels-api
*/
public class LabelRequest extends SiftRequest<LabelResponse> {
LabelRequest(HttpUrl baseUrl, OkHttpClient okClient, LabelFieldSet fields) {
super(baseUrl, okClient, fields);
LabelRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, LabelFieldSet fields) {
super(baseUrl, accountId, okClient, fields);
}

@Override
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/com/siftscience/ScoreRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* https://siftscience.com/developers/docs/curl/score-api
*/
public class ScoreRequest extends SiftRequest<ScoreResponse> {
ScoreRequest(HttpUrl baseUrl, OkHttpClient okClient, ScoreFieldSet fields) {
super(baseUrl, okClient, fields);
ScoreRequest(HttpUrl baseUrl, String accountId, OkHttpClient okClient, ScoreFieldSet fields) {
super(baseUrl, accountId, okClient, fields);
}

/**
Expand Down Expand Up @@ -42,12 +42,8 @@ protected HttpUrl path(HttpUrl baseUrl) {
builder.addPathSegment("score").addPathSegment(scoreFieldSet.getUserId())
.addQueryParameter("api_key", scoreFieldSet.getApiKey());
if (scoreFieldSet.getAbuseTypes() != null && scoreFieldSet.getAbuseTypes().size() > 0) {
String queryParamVal = "";
for (String abuseType : scoreFieldSet.getAbuseTypes()) {
queryParamVal += (abuseType + ",");
}
builder.addQueryParameter("abuse_types",
queryParamVal.substring(0, queryParamVal.length() - 1));
StringUtils.joinWithComma(scoreFieldSet.getAbuseTypes()));
}
return builder.build();
}
Expand Down
40 changes: 22 additions & 18 deletions src/main/java/com/siftscience/SiftClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,71 +39,75 @@
*
*/
public class SiftClient {
private String apiKey;
private OkHttpClient okClient;
private HttpUrl baseUrl = HttpUrl.parse("https://api.sift.com");
private final String accountId;
private final String apiKey;
private final OkHttpClient okClient;
private final HttpUrl baseUrl = HttpUrl.parse("https://api.sift.com");

public SiftClient(String apiKey) {
this(apiKey, new OkHttpClient());
public SiftClient(String apiKey, String accountId) {
this(apiKey, accountId, new OkHttpClient());
}

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

public String getApiKey() {
return apiKey;
}

public String getAccountId() {
return accountId;
}

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

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

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

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

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

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

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

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

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

private void setupApiKey(FieldSet fields) {
if (fields.getApiKey() == null) {
fields.setApiKey(apiKey);
}
fields.setApiKey(getApiKey());
}
}
Loading

0 comments on commit 08b4f40

Please sign in to comment.