Skip to content

Commit 1691a87

Browse files
authored
Merge pull request #228 from EasyPost/end_shipper_id_buy
feat: adds end_shipper_id on the buy call of a Shipment
2 parents 47e84a4 + 1159450 commit 1691a87

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
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 support to pass `end_shipper_id` on the buy call of a Shipment
6+
37
## v7.5.0 (2022-08-25)
48

59
- Adds the `EndShipper` class with `create`, `retrieve`, `all`, and `save` functions

easypost/shipment.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ def get_smartrates(self) -> List[object]:
4747
response, _ = requestor.request(method=RequestMethod.GET, url=url)
4848
return response.get("result", [])
4949

50-
def buy(self, with_carbon_offset: Optional[bool] = False, **params) -> "Shipment":
50+
def buy(
51+
self, with_carbon_offset: Optional[bool] = False, end_shipper_id: Optional[str] = None, **params
52+
) -> "Shipment":
5153
"""Buy a shipment."""
5254
requestor = Requestor(local_api_key=self._api_key)
5355
url = "%s/%s" % (self.instance_url(), "buy")
5456
params["carbon_offset"] = with_carbon_offset
57+
if end_shipper_id:
58+
params["end_shipper_id"] = end_shipper_id
5559

5660
response, api_key = requestor.request(method=RequestMethod.POST, url=url, params=params)
5761
self.refresh_from(values=response, api_key=api_key)

tests/test_shipment.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
from unittest.mock import patch
2+
13
import pytest
24

35
import easypost
6+
from easypost.requestor import RequestMethod
47

58

69
@pytest.mark.vcr()
@@ -279,3 +282,33 @@ def test_shipment_rerate_with_carbon_offset(one_call_buy_shipment):
279282
new_carbon_rates = shipment.regenerate_rates(with_carbon_offset=True)
280283

281284
assert all(rate.carbon_offset is not None for rate in new_carbon_rates.rates)
285+
286+
287+
@patch(
288+
"easypost.requestor.Requestor.request",
289+
return_value=[
290+
{"mock-request": None},
291+
"mock-api-key",
292+
],
293+
)
294+
def test_shipment_buy_with_end_shipper_id(mock_request):
295+
"""Because this requires an API call in prod, we mock the request instead of using
296+
VCR to ensure test user accounts don't get charged for real postage since we can only
297+
guarantee a USPS carrier account will be configured for a user.
298+
"""
299+
shipment_id = "shp_123"
300+
end_shipper_id = "es_123"
301+
rate_id = "rate_123"
302+
303+
mock_shipment = easypost.Shipment(easypost_id=shipment_id)
304+
mock_shipment.buy(rate={"id": rate_id}, end_shipper_id=end_shipper_id)
305+
306+
mock_request.assert_called_once_with(
307+
method=RequestMethod.POST,
308+
url=f"/shipments/{shipment_id}/buy",
309+
params={
310+
"rate": {"id": rate_id},
311+
"carbon_offset": False,
312+
"end_shipper_id": end_shipper_id,
313+
},
314+
)

0 commit comments

Comments
 (0)