Skip to content

Commit 543d7a7

Browse files
authored
add retrieveEstimatedDeliveryDate function in Shipment class (#262)
1 parent bcf1e00 commit 543d7a7

File tree

7 files changed

+276
-13
lines changed

7 files changed

+276
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## Next release
4+
5+
- Adds `retrieveEstimatedDeliveryDate` function to the Shipment class
6+
37
## v6.5.0 (2023-04-18)
48

59
- Adds beta `retrieveCarrierMetadata` function
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.easypost.model;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class EstimatedDeliveryDate {
7+
private EasyPostTimeInTransitData easypostTimeInTransitData;
8+
private Rate rate;
9+
}
10+
11+
@Getter
12+
class EasyPostTimeInTransitData {
13+
private TimeInTransit daysInTransit;
14+
private String easypostEstimatedDeliveryDate;
15+
private String plannedShipDate;
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.easypost.model;
2+
3+
import java.util.List;
4+
import lombok.Getter;
5+
6+
@Getter
7+
public class EstimatedDeliveryDateResponse {
8+
private List<EstimatedDeliveryDate> rates;
9+
}

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.easypost.http.Requestor;
88
import com.easypost.http.Requestor.RequestMethod;
99
import com.easypost.model.ShipmentCollection;
10+
import com.easypost.model.EstimatedDeliveryDate;
11+
import com.easypost.model.EstimatedDeliveryDateResponse;
1012
import com.easypost.model.Rate;
1113
import com.easypost.model.Shipment;
1214
import com.easypost.model.SmartRate;
@@ -85,8 +87,8 @@ public Shipment retrieve(final String id) throws EasyPostException {
8587
public ShipmentCollection all(final Map<String, Object> params) throws EasyPostException {
8688
String endpoint = "shipments";
8789

88-
ShipmentCollection shipmentCollection =
89-
Requestor.request(RequestMethod.GET, endpoint, params, ShipmentCollection.class, client);
90+
ShipmentCollection shipmentCollection = Requestor.request(RequestMethod.GET, endpoint, params,
91+
ShipmentCollection.class, client);
9092
// we store the params in the collection so that we can use them to get the next page
9193
shipmentCollection.setPurchased(InternalUtilities.getOrDefault(params, "purchased", null));
9294
shipmentCollection.setIncludeChildren(InternalUtilities.getOrDefault(params, "include_children", null));
@@ -214,9 +216,8 @@ public List<SmartRate> smartrates(final String id) throws EasyPostException {
214216
public List<SmartRate> smartrates(final String id, final Map<String, Object> params) throws EasyPostException {
215217
String endpoint = "shipments/" + id + "/smartrate";
216218

217-
SmartrateCollection smartrateCollection =
218-
Requestor.request(RequestMethod.GET, endpoint, params,
219-
SmartrateCollection.class, client);
219+
SmartrateCollection smartrateCollection = Requestor.request(RequestMethod.GET, endpoint, params,
220+
SmartrateCollection.class, client);
220221

221222
return smartrateCollection.getSmartrates();
222223
}
@@ -322,7 +323,7 @@ public Shipment buy(final String id, final Map<String, Object> params, final boo
322323
* @throws EasyPostException when the request fails.
323324
*/
324325
public Shipment buy(final String id, final Map<String, Object> params, final boolean withCarbonOffset,
325-
final String endShipperId) throws EasyPostException {
326+
final String endShipperId) throws EasyPostException {
326327
params.put("carbon_offset", withCarbonOffset);
327328

328329
if (endShipperId != null && !endShipperId.isEmpty()) {
@@ -469,7 +470,7 @@ public SmartRate getLowestSmartRate(final List<SmartRate> smartRates, int delive
469470
* @throws EasyPostException when the request fails.
470471
*/
471472
public SmartRate findLowestSmartrate(final List<SmartRate> smartRates, int deliveryDay,
472-
SmartrateAccuracy deliveryAccuracy) throws EasyPostException {
473+
SmartrateAccuracy deliveryAccuracy) throws EasyPostException {
473474
SmartRate lowestSmartrate = null;
474475

475476
for (SmartRate rate : smartRates) {
@@ -524,4 +525,23 @@ public Shipment generateForm(final String id, final String formType, final Map<S
524525
return Requestor.request(RequestMethod.POST, endpoint, wrappedParams, Shipment.class,
525526
client);
526527
}
528+
529+
/**
530+
* Retrieves the estimated delivery date of each Rate via SmartRate.
531+
*
532+
* @param id The id of the shipment.
533+
* @param plannedShipDate The planned shipment date.
534+
* @return EstimatedDeliveryDate object.
535+
* @throws EasyPostException
536+
*/
537+
public List<EstimatedDeliveryDate> retrieveEstimatedDeliveryDate(final String id, final String plannedShipDate)
538+
throws EasyPostException {
539+
HashMap<String, Object> params = new HashMap<>();
540+
params.put("planned_ship_date", plannedShipDate);
541+
String endpoint = "shipments/" + id + "/smartrate/delivery_date";
542+
543+
EstimatedDeliveryDateResponse response = Requestor.request(RequestMethod.GET, endpoint, params,
544+
EstimatedDeliveryDateResponse.class, client);
545+
return response.getRates();
546+
}
527547
}

src/test/cassettes/shipment/retrieve_estimated_delivery_date.json

Lines changed: 186 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/test/java/com/easypost/Fixtures.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,13 @@ public static HashMap<String, Object> rmaFormOptions() {
318318
public static HashMap<String, Object> referralUser() {
319319
return Objects.requireNonNull(getFixtureData()).users.referral;
320320
}
321+
322+
/**
323+
* Get the default planned ship date.
324+
*
325+
* @return The default planned ship date
326+
*/
327+
public static String plannedShipDate() {
328+
return "2023-04-28";
329+
}
321330
}

src/test/java/com/easypost/ShipmentTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.easypost.exception.General.InvalidParameterError;
66
import com.easypost.model.Address;
77
import com.easypost.model.EndShipper;
8+
import com.easypost.model.EstimatedDeliveryDate;
89
import com.easypost.model.Fee;
910
import com.easypost.model.Form;
1011
import com.easypost.model.Parcel;
@@ -522,8 +523,8 @@ public void testInstanceLowestSmartRate() throws EasyPostException {
522523
vcr.setUpTest("lowest_smartrate");
523524

524525
Shipment shipment = createBasicShipment();
525-
SmartRate lowestSmartRateFilters =
526-
vcr.client.shipment.lowestSmartRate(shipment.getId(), 2, SmartrateAccuracy.Percentile90);
526+
SmartRate lowestSmartRateFilters = vcr.client.shipment.lowestSmartRate(shipment.getId(), 2,
527+
SmartrateAccuracy.Percentile90);
527528

528529
// Test lowest smartrate with valid filters
529530
assertEquals("Priority", lowestSmartRateFilters.getService());
@@ -536,8 +537,8 @@ public void testInstanceLowestSmartRate() throws EasyPostException {
536537
vcr.client.shipment.lowestSmartRate(shipment.getId(), 0, SmartrateAccuracy.Percentile90);
537538
});
538539

539-
SmartRate deprecatedLowestSmartRateFilters =
540-
vcr.client.shipment.lowestSmartRate(shipment.getId(), 2, "percentile_90");
540+
SmartRate deprecatedLowestSmartRateFilters = vcr.client.shipment.lowestSmartRate(shipment.getId(), 2,
541+
"percentile_90");
541542

542543
// Test lowest smartrate with valid filters
543544
assertEquals("Priority", deprecatedLowestSmartRateFilters.getService());
@@ -649,8 +650,8 @@ public void testGenerateFormWithOption() throws EasyPostException {
649650
Shipment shipment = createOneCallBuyShipment();
650651
String formType = "return_packing_slip";
651652

652-
Shipment shipmentWithForm =
653-
vcr.client.shipment.generateForm(shipment.getId(), formType, Fixtures.rmaFormOptions());
653+
Shipment shipmentWithForm = vcr.client.shipment.generateForm(shipment.getId(), formType,
654+
Fixtures.rmaFormOptions());
654655

655656
assertTrue(shipmentWithForm.getForms().size() > 0);
656657

@@ -794,4 +795,22 @@ public void testBuyShipmentWithEndShipperId() throws EasyPostException {
794795

795796
assertNotNull(boughtShipment.getPostageLabel());
796797
}
798+
799+
/**
800+
* Tests that we retrieve time-in-transit data for each of the Rates of a Shipment.
801+
*
802+
* @throws EasyPostException when the request fails.
803+
*/
804+
@Test
805+
public void testRetrieveEstimatedDeliveryDate() throws EasyPostException {
806+
vcr.setUpTest("retrieve_estimated_delivery_date");
807+
808+
Shipment shipment = vcr.client.shipment.create(Fixtures.basicShipment());
809+
810+
List<EstimatedDeliveryDate> estimatedDeliveryDates = vcr.client.shipment
811+
.retrieveEstimatedDeliveryDate(shipment.getId(), Fixtures.plannedShipDate());
812+
for (EstimatedDeliveryDate estimatedDeliveryDate : estimatedDeliveryDates) {
813+
assertNotNull(estimatedDeliveryDate.getEasypostTimeInTransitData());
814+
}
815+
}
797816
}

0 commit comments

Comments
 (0)