Skip to content

Commit e25c9f3

Browse files
authored
Merge pull request #214 from EasyPost/carbon_offset
add ability to create/buy a shipment that has carbon_offset
2 parents d6c7ccd + dd2ff3c commit e25c9f3

37 files changed

+5744
-1260
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
## NEXT RELEASE
44

55
- Adds `validate_webhook` function that returns your webhook or raises an error if there is a `webhook_secret` mismatch
6+
- Adds ability to create a shipment with carbon_offset
7+
- Adds ability to buy a shipment with carbon_offset
8+
- Adds ability to one-call-buy a shipment with carbon_offset
9+
- Adds ability to rerate a shipment with carbon_offset
610

711
## v7.3.0 (2022-07-18)
812

easypost/shipment.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
)
77

88
from easypost import Rate
9+
from easypost.easypost_object import convert_to_easypost_object
910
from easypost.error import Error
1011
from easypost.requestor import (
1112
RequestMethod,
@@ -19,11 +20,26 @@
1920

2021

2122
class Shipment(AllResource, CreateResource):
22-
def regenerate_rates(self) -> "Shipment":
23+
@classmethod
24+
def create(cls, api_key: Optional[str] = None, with_carbon_offset: Optional[bool] = False, **params) -> "Shipment":
25+
"""Create an Shipment object."""
26+
requestor = Requestor(local_api_key=api_key)
27+
url = cls.class_url()
28+
wrapped_params = {
29+
cls.snakecase_name(): params,
30+
"carbon_offset": with_carbon_offset,
31+
}
32+
response, api_key = requestor.request(method=RequestMethod.POST, url=url, params=wrapped_params)
33+
return convert_to_easypost_object(response=response, api_key=api_key)
34+
35+
def regenerate_rates(self, with_carbon_offset: Optional[bool] = False) -> "Shipment":
2336
"""Regenerate rates for a shipment."""
2437
requestor = Requestor(local_api_key=self._api_key)
2538
url = "%s/%s" % (self.instance_url(), "rerate")
26-
response, api_key = requestor.request(method=RequestMethod.POST, url=url)
39+
params = {
40+
"carbon_offset": with_carbon_offset,
41+
}
42+
response, api_key = requestor.request(method=RequestMethod.POST, url=url, params=params)
2743
self.refresh_from(values=response, api_key=api_key)
2844
return self
2945

@@ -34,10 +50,12 @@ def get_smartrates(self) -> List[object]:
3450
response, _ = requestor.request(method=RequestMethod.GET, url=url)
3551
return response.get("result", [])
3652

37-
def buy(self, **params) -> "Shipment":
53+
def buy(self, with_carbon_offset: Optional[bool] = False, **params) -> "Shipment":
3854
"""Buy a shipment."""
3955
requestor = Requestor(local_api_key=self._api_key)
4056
url = "%s/%s" % (self.instance_url(), "buy")
57+
params["carbon_offset"] = with_carbon_offset
58+
4159
response, api_key = requestor.request(method=RequestMethod.POST, url=url, params=params)
4260
self.refresh_from(values=response, api_key=api_key)
4361
return self

tests/cassettes/test_batch_add_remove_shipment.yaml

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

tests/cassettes/test_error.yaml

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

tests/cassettes/test_generate_form.yaml

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

tests/cassettes/test_insurance_create.yaml

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

tests/cassettes/test_insurance_retrieve.yaml

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

tests/cassettes/test_pickup_buy.yaml

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

tests/cassettes/test_pickup_cancel.yaml

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

tests/cassettes/test_pickup_create.yaml

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

0 commit comments

Comments
 (0)