-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Claims service * address comment * address feedback
- Loading branch information
Showing
18 changed files
with
1,645 additions
and
3 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
Submodule examples
updated
32 files
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,26 @@ | ||
package com.easypost.model; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class Claim extends EasyPostResource { | ||
private Date statusTimestamp; | ||
private List<ClaimHistoryEntry> history; | ||
private String approvedAmount; | ||
private String checkDeliveryAddress; | ||
private String contactEmail; | ||
private String description; | ||
private String insuranceAmount; | ||
private String insuranceId; | ||
private String paymentMethod; | ||
private String recipientName; | ||
private String requestedAmount; | ||
private String salvageValue; | ||
private String shipmentId; | ||
private String status; | ||
private String statusDetail; | ||
private String trackingCode; | ||
private String type; | ||
} |
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,34 @@ | ||
package com.easypost.model; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.easypost.exception.General.EndOfPaginationError; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
public final class ClaimCollection extends PaginatedCollection<Claim> { | ||
private List<Claim> claims; | ||
|
||
@Setter | ||
private String type; | ||
private String status; | ||
|
||
@Override | ||
protected Map<String, Object> buildNextPageParameters(List<Claim> claims, Integer pageSize) | ||
throws EndOfPaginationError { | ||
String lastId = claims.get(claims.size() - 1).getId(); | ||
|
||
Map<String, Object> parameters = new java.util.HashMap<>(); | ||
parameters.put("before_id", lastId); | ||
parameters.put("type", type); | ||
parameters.put("status", status); | ||
|
||
if (pageSize != null) { | ||
parameters.put("page_size", pageSize); | ||
} | ||
|
||
return parameters; | ||
} | ||
} |
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,11 @@ | ||
package com.easypost.model; | ||
|
||
import java.util.Date; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ClaimHistoryEntry { | ||
private String status; | ||
private String statusDetail; | ||
private Date statusTimestamp; | ||
} |
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,78 @@ | ||
package com.easypost.model; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParseException; | ||
|
||
import java.lang.reflect.Type; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.TimeZone; | ||
|
||
public class DateDeserializer implements JsonDeserializer<Date> { | ||
private static final String[] DATE_FORMATS = new String[]{ | ||
// Basic formats | ||
"yyyy-MM-dd", | ||
"yyyy-MM-dd'T'HH:mm:ss", | ||
"yyyy-MM-dd'T'HH:mm:ss'Z'", | ||
|
||
// With milliseconds | ||
"yyyy-MM-dd'T'HH:mm:ss.SSS", | ||
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", | ||
|
||
// With time zone offset | ||
"yyyy-MM-dd'T'HH:mm:ssZ", | ||
"yyyy-MM-dd'T'HH:mm:ssXXX", | ||
|
||
// Alternative time formats | ||
"yyyy-MM-dd HH:mm:ss", | ||
"yyyy-MM-dd HH:mm:ss.SSS", | ||
|
||
// Month and day only | ||
"yyyy-MM-dd", | ||
"MM/dd/yyyy", | ||
"MM-dd-yyyy", | ||
|
||
// Year and week | ||
"yyyy-'W'ww", | ||
"yyyy-'W'ww-u", | ||
|
||
// Full date and time | ||
"EEE, d MMM yyyy HH:mm:ss Z", | ||
"EEE, d MMM yyyy HH:mm:ss z", | ||
"EEEE, MMMM d, yyyy h:mm:ss a", | ||
|
||
// Short formats | ||
"M/d/yy", | ||
"M-d-yy", | ||
"M.d.yy", | ||
"MM/dd/yyyy", | ||
"MM-dd-yyyy" | ||
}; | ||
|
||
/** | ||
* Deserialize the Date format from a JSON object. | ||
* | ||
* @param json JSON object to deserialize. | ||
* @param type Type of the object to deserialize. | ||
* @param context Deserialization context. | ||
* @return Deserialized Date object. | ||
* @throws JsonParseException if the JSON object is not a valid SmartrateCollection. | ||
*/ | ||
@Override | ||
public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context) | ||
throws JsonParseException { | ||
for (String format : DATE_FORMATS) { | ||
try { | ||
SimpleDateFormat sdf = new SimpleDateFormat(format); | ||
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); | ||
return sdf.parse(json.getAsString()); | ||
} catch (ParseException e) { | ||
throw new JsonParseException("Unable to parse this date format"); | ||
} | ||
} | ||
throw new JsonParseException("Unparseable date: \"" + json.getAsString() + "\""); | ||
} | ||
} |
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,116 @@ | ||
package com.easypost.service; | ||
|
||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import com.easypost.exception.EasyPostException; | ||
import com.easypost.exception.General.EndOfPaginationError; | ||
import com.easypost.http.Requestor; | ||
import com.easypost.http.Requestor.RequestMethod; | ||
import com.easypost.model.Claim; | ||
import com.easypost.model.ClaimCollection; | ||
|
||
import lombok.SneakyThrows; | ||
|
||
public class ClaimService { | ||
private final EasyPostClient client; | ||
|
||
/** | ||
* ClaimService constructor. | ||
* | ||
* @param client The client object. | ||
*/ | ||
ClaimService(EasyPostClient client) { | ||
this.client = client; | ||
} | ||
|
||
/** | ||
* Create a new claim object from a map of parameters. | ||
* | ||
* @param params Map of parameters. | ||
* @return Claim object | ||
* @throws EasyPostException when the request fails. | ||
*/ | ||
public Claim create(final Map<String, Object> params) throws EasyPostException { | ||
String endpoint = "claims"; | ||
|
||
return Requestor.request(RequestMethod.POST, endpoint, params, Claim.class, client); | ||
} | ||
|
||
/** | ||
* Retrieve an Claim from the API. | ||
* | ||
* @param id The ID of the Claim to retrieve. | ||
* @return Claim object | ||
* @throws EasyPostException when the request fails. | ||
*/ | ||
public Claim retrieve(final String id) throws EasyPostException { | ||
String endpoint = "claims/" + id; | ||
|
||
return Requestor.request(RequestMethod.GET, endpoint, null, Claim.class, client); | ||
} | ||
|
||
/** | ||
* Get a list of Claims. | ||
* | ||
* @param params a map of parameters | ||
* @return ClaimCollection object | ||
* @throws EasyPostException when the request fails. | ||
*/ | ||
public ClaimCollection all(final Map<String, Object> params) throws EasyPostException { | ||
String type = (String) params.get("type"); | ||
String status = (String) params.get("status"); | ||
params.remove(type); | ||
params.remove(status); | ||
String endpoint = "claims"; | ||
|
||
ClaimCollection claimCollection = | ||
Requestor.request(RequestMethod.GET, endpoint, params, ClaimCollection.class, client); | ||
claimCollection.setType(type); | ||
claimCollection.setType(status); | ||
|
||
return claimCollection; | ||
} | ||
|
||
/** | ||
* Cancel an Claim from the API. | ||
* | ||
* @param id The ID of the Claim to cancel. | ||
* @return Claim object | ||
* @throws EasyPostException when the request fails. | ||
*/ | ||
public Claim cancel(final String id) throws EasyPostException { | ||
String endpoint = String.format("claims/%s/cancel", id); | ||
|
||
return Requestor.request(RequestMethod.POST, endpoint, null, Claim.class, client); | ||
} | ||
|
||
/** | ||
* Get the next page of an ClaimCollection. | ||
* | ||
* @param collection ClaimCollection to get next page of. | ||
* @return ClaimCollection object. | ||
* @throws EndOfPaginationError when there are no more pages to retrieve. | ||
*/ | ||
public ClaimCollection getNextPage(ClaimCollection collection) throws EndOfPaginationError { | ||
return getNextPage(collection, null); | ||
} | ||
|
||
/** | ||
* Get the next page of an ClaimCollection. | ||
* | ||
* @param collection ClaimCollection to get next page of. | ||
* @param pageSize The number of results to return on the next page. | ||
* @return ClaimCollection object. | ||
* @throws EndOfPaginationError when there are no more pages to retrieve. | ||
*/ | ||
public ClaimCollection getNextPage( | ||
ClaimCollection collection, Integer pageSize) throws EndOfPaginationError { | ||
return collection.getNextPage(new Function<Map<String, Object>, ClaimCollection>() { | ||
@Override @SneakyThrows | ||
public ClaimCollection apply(Map<String, Object> parameters) { | ||
return all(parameters); | ||
} | ||
}, collection.getClaims(), pageSize); | ||
} | ||
} |
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
Oops, something went wrong.