-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from SiftScience/arjun_decisions_api
(For AlexL) Support for Apply Decisions API in sift-java client
- Loading branch information
Showing
15 changed files
with
579 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
1.3 (2016-01-17) | ||
================== | ||
|
||
- Add support for decisions API. | ||
|
||
1.2 (2016-12-14) | ||
================== | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,20 +197,50 @@ WorkflowStatusRequest request = client.buildRequest(new WorkflowStatusFieldSet() | |
.setWorkflowRunId("someid")); | ||
``` | ||
|
||
### Apply Decision API | ||
|
||
[API Docs](https://siftscience.com/developers/docs/java/decisions-api/apply-decision) | ||
|
||
To apply a decision to a user, create a request with accountId, 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. | ||
```java | ||
ApplyDecisionRequest request = client.buildRequest( | ||
new ApplyDecisionFieldSet() | ||
.setAccountId("your_account_id") | ||
.setUserId("a_user_id") | ||
.setOrderId("a_order_id") | ||
.setDecisionId("decision_id") | ||
.setSource(DecisionSource.MANUAL_REVIEW) | ||
.setAnalyst("[email protected]") | ||
.setDescription("description of decision applied") | ||
); | ||
``` | ||
|
||
#### Get decisions | ||
|
||
[API Docs](https://siftscience.com/developers/docs/java/decisions-api/get-decisions) | ||
|
||
To retrieve available decisions, build a request with a GetDecisionsFieldSet. | ||
```java | ||
GetDecisions request = client.buildRequest(new GetDecisionsFieldSet() | ||
GetDecisionsRequest request = client.buildRequest(new GetDecisionsFieldSet() | ||
.setAbuseTypes(ImmutableList.of(AbuseType.PAYMENT_ABUSE, AbuseType.CONTENT_ABUSE)) | ||
.setAccountId("your_account_id")); | ||
``` | ||
|
||
Additionally, this field set supports filtering on results by entity and abuse type(s). | ||
```java | ||
GetDecisions request = client.buildRequest(new GetDecisionsFieldSet() | ||
GetDecisionsRequest request = client.buildRequest(new GetDecisionsFieldSet() | ||
.setAccountId("your_account_id")) | ||
.setEntityType(EntityType.ORDER) | ||
.setAbuseTypes(ImmutableList.of(AbuseType.PAYMENT_ABUSE, AbuseType.CONTENT_ABUSE)) | ||
|
@@ -220,7 +250,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 | ||
GetDecisions request = client.buildRequest(new GetDecisionsFieldSet() | ||
GetDecisionsRequest request = client.buildRequest(new GetDecisionsFieldSet() | ||
.setAccountId("your_account_id")) | ||
.setFrom(15) | ||
.setLimit(10); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.siftscience; | ||
|
||
import java.io.IOException; | ||
|
||
import com.siftscience.model.ApplyDecisionFieldSet; | ||
import okhttp3.*; | ||
|
||
public class ApplyDecisionRequest extends SiftRequest<ApplyDecisionResponse>{ | ||
|
||
ApplyDecisionRequest(HttpUrl baseUrl, OkHttpClient okClient, ApplyDecisionFieldSet fields) { | ||
super(baseUrl, okClient, fields); | ||
} | ||
|
||
@Override | ||
protected HttpUrl path(HttpUrl baseUrl) { | ||
HttpUrl.Builder path = baseUrl.newBuilder("v3/accounts") | ||
.addPathSegment(((ApplyDecisionFieldSet) fieldSet).getAccountId()) | ||
.addPathSegment("users") | ||
.addPathSegment(((ApplyDecisionFieldSet) fieldSet).getUserId()); | ||
|
||
String orderId = ((ApplyDecisionFieldSet) fieldSet).getOrderId(); | ||
if (orderId != null && !orderId.isEmpty()) { | ||
path.addPathSegment("orders").addPathSegment(orderId); | ||
} | ||
|
||
return path.addPathSegment("decisions").build(); | ||
} | ||
|
||
@Override | ||
ApplyDecisionResponse buildResponse(okhttp3.Response response, FieldSet requestFields) throws IOException { | ||
return new ApplyDecisionResponse(response, requestFields); | ||
} | ||
|
||
@Override | ||
protected void modifyRequestBuilder(Request.Builder builder) { | ||
super.modifyRequestBuilder(builder); | ||
builder.header("Authorization", Credentials.basic(fieldSet.getApiKey(),"")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.siftscience; | ||
|
||
import com.siftscience.model.DecisionLog; | ||
import com.siftscience.model.ApplyDecisionResponseBody; | ||
import okhttp3.Response; | ||
|
||
import java.io.IOException; | ||
|
||
public class ApplyDecisionResponse extends SiftResponse<DecisionLog>{ | ||
|
||
ApplyDecisionResponse(Response okResponse, FieldSet requestBody) throws IOException { | ||
super(okResponse, requestBody); | ||
} | ||
|
||
@Override | ||
public void populateBodyFromJson(String jsonBody) { | ||
body = DecisionLog.fromJson(jsonBody); | ||
} | ||
|
||
public DecisionLog getDecisionLog() { | ||
return body; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/java/com/siftscience/model/ApplyDecisionFieldSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.siftscience.model; | ||
|
||
import com.google.gson.*; | ||
import com.google.gson.annotations.Expose; | ||
import com.google.gson.annotations.SerializedName; | ||
import com.siftscience.FieldSet; | ||
import com.siftscience.exception.MissingFieldException; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
public class ApplyDecisionFieldSet extends FieldSet<ApplyDecisionFieldSet> { | ||
|
||
public enum DecisionSource { | ||
MANUAL_REVIEW("manual_review"), | ||
AUTOMATED_RULE("automated_rule"), | ||
CHARGEBACK("chargeback"); | ||
|
||
private final String value; | ||
|
||
DecisionSource(String value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
return value; | ||
} | ||
|
||
static class DecisionSourceDeserializer implements JsonDeserializer<DecisionSource> { | ||
@Override | ||
public DecisionSource deserialize(JsonElement json, | ||
Type typeOfT, | ||
JsonDeserializationContext context) | ||
throws JsonParseException { | ||
return DecisionSource.valueOf(json.getAsString().toUpperCase()); | ||
} | ||
} | ||
|
||
static class DecisionSourceSerializer implements JsonSerializer<DecisionSource> { | ||
@Override | ||
public JsonElement serialize(DecisionSource source, Type typeOfSrc, JsonSerializationContext context) { | ||
return gson.toJsonTree(source.value); | ||
} | ||
} | ||
} | ||
|
||
|
||
@Expose @SerializedName("decision_id") private String decisionId; | ||
@Expose @SerializedName("source") private DecisionSource source; | ||
@Expose @SerializedName("analyst") private String analyst; | ||
@Expose @SerializedName("descrion") private String description; | ||
@Expose @SerializedName("time") private Long time; | ||
private String accountId; | ||
private String userId; | ||
private String orderId; | ||
|
||
public ApplyDecisionFieldSet() {} | ||
|
||
public ApplyDecisionFieldSet setDecisionId(String decisionId) { | ||
this.decisionId = decisionId; | ||
return this; | ||
} | ||
|
||
public ApplyDecisionFieldSet setSource(DecisionSource source) { | ||
this.source = source; | ||
return this; | ||
} | ||
|
||
public ApplyDecisionFieldSet setDescription(String description) { | ||
this.description = description; | ||
return this; | ||
} | ||
|
||
public ApplyDecisionFieldSet setAnalyst(String analyst) { | ||
this.analyst = analyst; | ||
return this; | ||
} | ||
|
||
public ApplyDecisionFieldSet setTime(Long time) { | ||
this.time = time; | ||
return this; | ||
} | ||
|
||
public String getAccountId() { | ||
return accountId; | ||
} | ||
|
||
public ApplyDecisionFieldSet setAccountId(String accountId) { | ||
this.accountId = accountId; | ||
return this; | ||
} | ||
|
||
public String getUserId() { | ||
return userId; | ||
} | ||
|
||
public ApplyDecisionFieldSet setUserId(String userId) { | ||
this.userId = userId; | ||
return this; | ||
} | ||
|
||
public String getOrderId() { | ||
return orderId; | ||
} | ||
|
||
public ApplyDecisionFieldSet setOrderId(String orderId) { | ||
this.orderId = orderId; | ||
return this; | ||
} | ||
|
||
@Override | ||
public void validate() { | ||
super.validate(); | ||
if (DecisionSource.MANUAL_REVIEW.equals(source) && (analyst == null || analyst.isEmpty())) { | ||
throw new MissingFieldException("'analyst' required for decisions " + | ||
"with source type MANUAL_REVIEW"); | ||
} | ||
} | ||
} |
Oops, something went wrong.