Skip to content

Commit 7bb1132

Browse files
author
Jack Chen
authored
Add Claims service (#320)
* Add Claims service * address comment * address feedback
1 parent 31d922e commit 7bb1132

File tree

18 files changed

+1645
-3
lines changed

18 files changed

+1645
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# CHANGELOG
22

3-
## v7.3.0 (2024-07-15)
3+
## Next Release
4+
5+
- Add new `Claim` service for filing claims on EasyPost shipments and insurances
6+
7+
## v7.3.0 (2024-07-16)
48

59
- Adds new `shipment.recommendShipDate`, `smartrate.recommendShipDate`, and `smartrate.estimateDeliveryDate` functions
610
- Routes `UpsAccount`, `UpsMailInnovationsAccount`, and `UpsSurepostAccount` create/update requests to the new `/ups_oauth_registrations` endpoint

examples

Submodule examples updated 32 files

src/main/java/com/easypost/Constants.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.easypost.http.HashMapSerializer;
44
import com.easypost.model.AddressVerification;
55
import com.easypost.model.AddressVerificationDeserializer;
6+
import com.easypost.model.DateDeserializer;
67
import com.easypost.model.Error;
78
import com.easypost.model.ErrorDeserializer;
89
import com.easypost.model.SmartrateCollection;
@@ -16,6 +17,7 @@
1617
import com.google.gson.Gson;
1718
import com.google.gson.GsonBuilder;
1819

20+
import java.util.Date;
1921
import java.util.HashMap;
2022
import java.util.List;
2123

@@ -83,7 +85,8 @@ public abstract static class Http {
8385
.registerTypeAdapter(Error.class, new ErrorDeserializer())
8486
.registerTypeAdapter(AddressVerification.class, new AddressVerificationDeserializer())
8587
.registerTypeAdapter(StatelessRate[].class, new StatelessRateDeserializer())
86-
.registerTypeAdapter(Webhook[].class, new WebhookDeserializer()).create();
88+
.registerTypeAdapter(Webhook[].class, new WebhookDeserializer())
89+
.registerTypeAdapter(Date.class, new DateDeserializer()).create();
8790
public static final Gson PRETTY_PRINT_GSON = new GsonBuilder().setPrettyPrinting().serializeNulls()
8891
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
8992
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.easypost.model;
2+
3+
import java.util.Date;
4+
import java.util.List;
5+
import lombok.Getter;
6+
7+
@Getter
8+
public class Claim extends EasyPostResource {
9+
private Date statusTimestamp;
10+
private List<ClaimHistoryEntry> history;
11+
private String approvedAmount;
12+
private String checkDeliveryAddress;
13+
private String contactEmail;
14+
private String description;
15+
private String insuranceAmount;
16+
private String insuranceId;
17+
private String paymentMethod;
18+
private String recipientName;
19+
private String requestedAmount;
20+
private String salvageValue;
21+
private String shipmentId;
22+
private String status;
23+
private String statusDetail;
24+
private String trackingCode;
25+
private String type;
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.easypost.model;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import com.easypost.exception.General.EndOfPaginationError;
7+
import lombok.Getter;
8+
import lombok.Setter;
9+
10+
@Getter
11+
public final class ClaimCollection extends PaginatedCollection<Claim> {
12+
private List<Claim> claims;
13+
14+
@Setter
15+
private String type;
16+
private String status;
17+
18+
@Override
19+
protected Map<String, Object> buildNextPageParameters(List<Claim> claims, Integer pageSize)
20+
throws EndOfPaginationError {
21+
String lastId = claims.get(claims.size() - 1).getId();
22+
23+
Map<String, Object> parameters = new java.util.HashMap<>();
24+
parameters.put("before_id", lastId);
25+
parameters.put("type", type);
26+
parameters.put("status", status);
27+
28+
if (pageSize != null) {
29+
parameters.put("page_size", pageSize);
30+
}
31+
32+
return parameters;
33+
}
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.easypost.model;
2+
3+
import java.util.Date;
4+
import lombok.Getter;
5+
6+
@Getter
7+
public class ClaimHistoryEntry {
8+
private String status;
9+
private String statusDetail;
10+
private Date statusTimestamp;
11+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.easypost.model;
2+
3+
import com.google.gson.JsonDeserializationContext;
4+
import com.google.gson.JsonDeserializer;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonParseException;
7+
8+
import java.lang.reflect.Type;
9+
import java.text.ParseException;
10+
import java.text.SimpleDateFormat;
11+
import java.util.Date;
12+
import java.util.TimeZone;
13+
14+
public class DateDeserializer implements JsonDeserializer<Date> {
15+
private static final String[] DATE_FORMATS = new String[]{
16+
// Basic formats
17+
"yyyy-MM-dd",
18+
"yyyy-MM-dd'T'HH:mm:ss",
19+
"yyyy-MM-dd'T'HH:mm:ss'Z'",
20+
21+
// With milliseconds
22+
"yyyy-MM-dd'T'HH:mm:ss.SSS",
23+
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
24+
25+
// With time zone offset
26+
"yyyy-MM-dd'T'HH:mm:ssZ",
27+
"yyyy-MM-dd'T'HH:mm:ssXXX",
28+
29+
// Alternative time formats
30+
"yyyy-MM-dd HH:mm:ss",
31+
"yyyy-MM-dd HH:mm:ss.SSS",
32+
33+
// Month and day only
34+
"yyyy-MM-dd",
35+
"MM/dd/yyyy",
36+
"MM-dd-yyyy",
37+
38+
// Year and week
39+
"yyyy-'W'ww",
40+
"yyyy-'W'ww-u",
41+
42+
// Full date and time
43+
"EEE, d MMM yyyy HH:mm:ss Z",
44+
"EEE, d MMM yyyy HH:mm:ss z",
45+
"EEEE, MMMM d, yyyy h:mm:ss a",
46+
47+
// Short formats
48+
"M/d/yy",
49+
"M-d-yy",
50+
"M.d.yy",
51+
"MM/dd/yyyy",
52+
"MM-dd-yyyy"
53+
};
54+
55+
/**
56+
* Deserialize the Date format from a JSON object.
57+
*
58+
* @param json JSON object to deserialize.
59+
* @param type Type of the object to deserialize.
60+
* @param context Deserialization context.
61+
* @return Deserialized Date object.
62+
* @throws JsonParseException if the JSON object is not a valid SmartrateCollection.
63+
*/
64+
@Override
65+
public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context)
66+
throws JsonParseException {
67+
for (String format : DATE_FORMATS) {
68+
try {
69+
SimpleDateFormat sdf = new SimpleDateFormat(format);
70+
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
71+
return sdf.parse(json.getAsString());
72+
} catch (ParseException e) {
73+
throw new JsonParseException("Unable to parse this date format");
74+
}
75+
}
76+
throw new JsonParseException("Unparseable date: \"" + json.getAsString() + "\"");
77+
}
78+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.easypost.service;
2+
3+
import java.util.Map;
4+
import java.util.function.Function;
5+
6+
import com.easypost.exception.EasyPostException;
7+
import com.easypost.exception.General.EndOfPaginationError;
8+
import com.easypost.http.Requestor;
9+
import com.easypost.http.Requestor.RequestMethod;
10+
import com.easypost.model.Claim;
11+
import com.easypost.model.ClaimCollection;
12+
13+
import lombok.SneakyThrows;
14+
15+
public class ClaimService {
16+
private final EasyPostClient client;
17+
18+
/**
19+
* ClaimService constructor.
20+
*
21+
* @param client The client object.
22+
*/
23+
ClaimService(EasyPostClient client) {
24+
this.client = client;
25+
}
26+
27+
/**
28+
* Create a new claim object from a map of parameters.
29+
*
30+
* @param params Map of parameters.
31+
* @return Claim object
32+
* @throws EasyPostException when the request fails.
33+
*/
34+
public Claim create(final Map<String, Object> params) throws EasyPostException {
35+
String endpoint = "claims";
36+
37+
return Requestor.request(RequestMethod.POST, endpoint, params, Claim.class, client);
38+
}
39+
40+
/**
41+
* Retrieve an Claim from the API.
42+
*
43+
* @param id The ID of the Claim to retrieve.
44+
* @return Claim object
45+
* @throws EasyPostException when the request fails.
46+
*/
47+
public Claim retrieve(final String id) throws EasyPostException {
48+
String endpoint = "claims/" + id;
49+
50+
return Requestor.request(RequestMethod.GET, endpoint, null, Claim.class, client);
51+
}
52+
53+
/**
54+
* Get a list of Claims.
55+
*
56+
* @param params a map of parameters
57+
* @return ClaimCollection object
58+
* @throws EasyPostException when the request fails.
59+
*/
60+
public ClaimCollection all(final Map<String, Object> params) throws EasyPostException {
61+
String type = (String) params.get("type");
62+
String status = (String) params.get("status");
63+
params.remove(type);
64+
params.remove(status);
65+
String endpoint = "claims";
66+
67+
ClaimCollection claimCollection =
68+
Requestor.request(RequestMethod.GET, endpoint, params, ClaimCollection.class, client);
69+
claimCollection.setType(type);
70+
claimCollection.setType(status);
71+
72+
return claimCollection;
73+
}
74+
75+
/**
76+
* Cancel an Claim from the API.
77+
*
78+
* @param id The ID of the Claim to cancel.
79+
* @return Claim object
80+
* @throws EasyPostException when the request fails.
81+
*/
82+
public Claim cancel(final String id) throws EasyPostException {
83+
String endpoint = String.format("claims/%s/cancel", id);
84+
85+
return Requestor.request(RequestMethod.POST, endpoint, null, Claim.class, client);
86+
}
87+
88+
/**
89+
* Get the next page of an ClaimCollection.
90+
*
91+
* @param collection ClaimCollection to get next page of.
92+
* @return ClaimCollection object.
93+
* @throws EndOfPaginationError when there are no more pages to retrieve.
94+
*/
95+
public ClaimCollection getNextPage(ClaimCollection collection) throws EndOfPaginationError {
96+
return getNextPage(collection, null);
97+
}
98+
99+
/**
100+
* Get the next page of an ClaimCollection.
101+
*
102+
* @param collection ClaimCollection to get next page of.
103+
* @param pageSize The number of results to return on the next page.
104+
* @return ClaimCollection object.
105+
* @throws EndOfPaginationError when there are no more pages to retrieve.
106+
*/
107+
public ClaimCollection getNextPage(
108+
ClaimCollection collection, Integer pageSize) throws EndOfPaginationError {
109+
return collection.getNextPage(new Function<Map<String, Object>, ClaimCollection>() {
110+
@Override @SneakyThrows
111+
public ClaimCollection apply(Map<String, Object> parameters) {
112+
return all(parameters);
113+
}
114+
}, collection.getClaims(), pageSize);
115+
}
116+
}

src/main/java/com/easypost/service/EasyPostClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class EasyPostClient {
2626
public final CarrierAccountService carrierAccount;
2727
public final CarrierMetadataService carrierMetadata;
2828
public final CarrierTypeService carrierType;
29+
public final ClaimService claim;
2930
public final CustomsInfoService customsInfo;
3031
public final CustomsItemService customsItem;
3132
public final EndShipperService endShipper;
@@ -136,6 +137,7 @@ public EasyPostClient(String apiKey, int connectTimeoutMilliseconds, int readTim
136137
this.carrierAccount = new CarrierAccountService(this);
137138
this.carrierMetadata = new CarrierMetadataService(this);
138139
this.carrierType = new CarrierTypeService(this);
140+
this.claim = new ClaimService(this);
139141
this.customsInfo = new CustomsInfoService(this);
140142
this.customsItem = new CustomsItemService(this);
141143
this.endShipper = new EndShipperService(this);

0 commit comments

Comments
 (0)