Skip to content

Commit 0ed9182

Browse files
authored
Add carbon offset functionality (#304)
- Add CarbonOffset class - Add carbon offset property to Rate class - Add carbon offset parameter to Shipment create, buy and re-rate functions - Update local styling to auto-organize JSON properties alphabetically, apply styling to CarbonOffset.cs, Rate.cs and Shipment.cs
1 parent 483d034 commit 0ed9182

File tree

358 files changed

+29329
-28053
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

358 files changed

+29329
-28053
lines changed

CHANGELOG.md

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

3-
## NEXT RELEASE
4-
3+
## Next Release
4+
- Adds the ability to create a shipment with carbon offset
5+
- Adds the ability to buy a shipment with carbon offset
6+
- Adds the ability to one-call-buy a shipment with carbon offset
7+
- Adds the ability to re-rate a shipment with carbon offset
58
- Removes the unusable `carrier` param from the `verify` function on an Address
69

710
## v3.3.0 (2022-07-18)

EasyPost.Tests/Fixture.cs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ public static Dictionary<string, object> BasicPickup
393393
{
394394
get
395395
{
396-
const string pickupDate = "2022-06-4";
396+
const string pickupDate = "2022-08-01";
397397
return new Dictionary<string, object>
398398
{
399399
{
@@ -626,5 +626,85 @@ public static Dictionary<string, object> RmaFormOptions
626626
};
627627
}
628628
}
629+
630+
public static Dictionary<string, object> BasicCarbonOffsetShipment
631+
{
632+
get
633+
{
634+
return new Dictionary<string, object>
635+
{
636+
{ "to_address", PickupAddress },
637+
{"from_address", BasicAddress },
638+
{"parcel", BasicParcel },
639+
};
640+
}
641+
}
642+
643+
public static Dictionary<string, object> FullCarbonOffsetShipment
644+
{
645+
get
646+
{
647+
return new Dictionary<string, object>
648+
{
649+
{
650+
"to_address", PickupAddress
651+
},
652+
{
653+
"from_address", BasicAddress
654+
},
655+
{
656+
"parcel", BasicParcel
657+
},
658+
{
659+
"customs_info", BasicCustomsInfo
660+
},
661+
{
662+
"options", new Dictionary<string, object>
663+
{
664+
{
665+
"label_format", "PNG" // Must be PNG so we can convert to ZPL later
666+
},
667+
{
668+
"invoice_number", "123"
669+
}
670+
}
671+
},
672+
{
673+
"reference", "123"
674+
}
675+
};
676+
}
677+
}
678+
679+
public static Dictionary<string, object> OneCallBuyCarbonOffsetShipment
680+
{
681+
get
682+
{
683+
return new Dictionary<string, object>
684+
{
685+
{
686+
"to_address", PickupAddress
687+
},
688+
{
689+
"from_address", BasicAddress
690+
},
691+
{
692+
"parcel", BasicParcel
693+
},
694+
{
695+
"service", UspsService
696+
},
697+
{
698+
"carrier_accounts", new List<string>
699+
{
700+
UspsCarrierAccountId
701+
}
702+
},
703+
{
704+
"carrier", Usps
705+
}
706+
};
707+
}
708+
}
629709
}
630710
}

EasyPost.Tests/ShipmentTest.cs

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public async Task TestBuy()
8787
{
8888
_vcr.SetUpTest("buy");
8989

90-
9190
Shipment shipment = await CreateFullShipment();
9291

9392
await shipment.Buy(shipment.LowestRate());
@@ -278,8 +277,8 @@ public async Task TestInstanceLowestSmartrate()
278277

279278
// test lowest smartrate with valid filters
280279
Smartrate lowestSmartrate = await shipment.LowestSmartrate(1, SmartrateAccuracy.Percentile90);
281-
Assert.AreEqual("First", lowestSmartrate.service);
282-
Assert.AreEqual(5.49, lowestSmartrate.rate);
280+
Assert.AreEqual("Express", lowestSmartrate.service);
281+
Assert.AreEqual(23.75, lowestSmartrate.rate);
283282
Assert.AreEqual("USPS", lowestSmartrate.carrier);
284283

285284
// test lowest smartrate with invalid filters (should error due to strict delivery_days)
@@ -330,8 +329,8 @@ public async Task TestStaticLowestSmartrate()
330329
// test lowest smartrate with valid filters
331330
List<Smartrate> smartrates = await shipment.GetSmartrates();
332331
Smartrate lowestSmartrate = Shipment.GetLowestSmartrate(smartrates, 1, SmartrateAccuracy.Percentile90);
333-
Assert.AreEqual("First", lowestSmartrate.service);
334-
Assert.AreEqual(5.49, lowestSmartrate.rate);
332+
Assert.AreEqual("Express", lowestSmartrate.service);
333+
Assert.AreEqual(23.75, lowestSmartrate.rate);
335334
Assert.AreEqual("USPS", lowestSmartrate.carrier);
336335

337336
// test lowest smartrate with invalid filters (should error due to strict delivery_days)
@@ -358,5 +357,65 @@ public async Task TestGenerateForm()
358357
Assert.AreEqual(formType, form.form_type);
359358
Assert.IsTrue(form.form_url != null);
360359
}
360+
361+
[TestMethod]
362+
public async Task TestCreateShipmentWithCarbonOffset()
363+
{
364+
_vcr.SetUpTest("create_shipment_with_carbon_offset");
365+
366+
Shipment shipment = await Shipment.Create(Fixture.BasicCarbonOffsetShipment, true);
367+
368+
Assert.IsInstanceOfType(shipment, typeof(Shipment));
369+
370+
Rate rate = shipment.LowestRate();
371+
CarbonOffset carbonOffset = rate.carbon_offset;
372+
373+
Assert.IsNotNull(carbonOffset);
374+
Assert.IsNotNull(carbonOffset.price);
375+
}
376+
377+
[TestMethod]
378+
public async Task TestBuyShipmentWithCarbonOffset()
379+
{
380+
_vcr.SetUpTest("buy_shipment_with_carbon_offset");
381+
382+
Shipment shipment = await Shipment.Create(Fixture.FullCarbonOffsetShipment);
383+
384+
await shipment.Buy(shipment.LowestRate(), withCarbonOffset: true);
385+
386+
Assert.IsNotNull(shipment.fees);
387+
bool carbonOffsetIncluded = shipment.fees.Any(fee => fee.type == "CarbonOffsetFee");
388+
Assert.IsTrue(carbonOffsetIncluded);
389+
}
390+
391+
[TestMethod]
392+
public async Task TestOneCallBuyShipmentWithCarbonOffset()
393+
{
394+
_vcr.SetUpTest("one_call_buy_shipment_with_carbon_offset");
395+
396+
Shipment shipment = await Shipment.Create(Fixture.OneCallBuyCarbonOffsetShipment, true);
397+
398+
Assert.IsNotNull(shipment.fees);
399+
bool carbonOffsetIncluded = shipment.fees.Any(fee => fee.type == "CarbonOffsetFee");
400+
Assert.IsTrue(carbonOffsetIncluded);
401+
}
402+
403+
[TestMethod]
404+
public async Task TestRegenerateRatesWithCarbonOffset()
405+
{
406+
_vcr.SetUpTest("regenerate_rates_with_carbon_offset");
407+
408+
Shipment shipment = await Shipment.Create(Fixture.OneCallBuyCarbonOffsetShipment);
409+
List<Rate> baseRates = shipment.rates;
410+
411+
await shipment.RegenerateRates(withCarbonOffset: true);
412+
List<Rate> newRatesWithCarbon = shipment.rates;
413+
414+
Rate baseRate = baseRates.First();
415+
Rate newRateWithCarbon = newRatesWithCarbon.First();
416+
417+
Assert.IsNull(baseRate.carbon_offset);
418+
Assert.IsNotNull(newRateWithCarbon.carbon_offset);
419+
}
361420
}
362421
}

EasyPost.Tests/cassettes/net462/address/all.json

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

0 commit comments

Comments
 (0)