Skip to content

Commit 8ea650a

Browse files
authored
Merge pull request #338 from EasyPost/smartrate_service
feat: adds new recommend_ship_date and estimate_delivery_date functions
2 parents 3970032 + 05fb39e commit 8ea650a

12 files changed

+1747
-137
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Next Release
44

5+
- Adds new `shipment.recommend_ship_date`, `smartrate.recommend_ship_date`, and `smartrate.estimate_delivery_date` functions
56
- Routes `UpsAccount`, `UpsMailInnovationsAccount`, and `UpsSurepostAccount` create/update requests to the new `/ups_oauth_registrations` endpoint
67
- Starting `2024-08-05`, UPS accounts will require a new payload to register or update. See [UPS OAuth 2.0 Update](https://support.easypost.com/hc/en-us/articles/26635027512717-UPS-OAuth-2-0-Update?utm_medium=email&_hsenc=p2ANqtz-96MmFtWICOzy9sKRbbcZSiMovZSrY3MSX1_bgY9N3f9yLVfWQdLhjAGq-SmNcOnDIS6GYhZ0OApjDBrGkKyLLMx1z6_TFOVp6-wllhEFQINrkuRuc&_hsmi=313130292&utm_content=313130292&utm_source=hs_email) for more details
78

easypost/easypost_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
ReportService,
3333
ScanFormService,
3434
ShipmentService,
35+
SmartRateService,
3536
TrackerService,
3637
UserService,
3738
WebhookService,
@@ -75,6 +76,7 @@ def __init__(
7576
self.report = ReportService(self)
7677
self.scan_form = ScanFormService(self)
7778
self.shipment = ShipmentService(self)
79+
self.smartrate = SmartRateService(self)
7880
self.tracker = TrackerService(self)
7981
self.user = UserService(self)
8082
self.webhook = WebhookService(self)

easypost/services/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from easypost.services.report_service import ReportService
2222
from easypost.services.scan_form_service import ScanFormService
2323
from easypost.services.shipment_service import ShipmentService
24+
from easypost.services.smartrate_service import SmartRateService
2425
from easypost.services.tracker_service import TrackerService
2526
from easypost.services.user_service import UserService
2627
from easypost.services.webhook_service import WebhookService

easypost/services/shipment_service.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,13 @@ def retrieve_estimated_delivery_date(self, id: str, planned_ship_date: str) -> L
153153
response = Requestor(self._client).request(method=RequestMethod.GET, url=url, params=wrapped_params)
154154

155155
return convert_to_easypost_object(response=response.get("rates", []))
156+
157+
def recommend_ship_date(self, id: str, **params) -> List[Dict[str, Any]]:
158+
"""Retrieve a recommended ship date for an existing Shipment via the Precision Shipping API,
159+
based on a specific desired delivery date.
160+
"""
161+
url = f"{self._instance_url(self._model_class, id)}/smartrate/precision_shipping"
162+
163+
response = Requestor(self._client).request(method=RequestMethod.GET, url=url, params=params)
164+
165+
return convert_to_easypost_object(response=response.get("rates", []))
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import (
2+
Any,
3+
Dict,
4+
List,
5+
)
6+
7+
from easypost.easypost_object import convert_to_easypost_object
8+
from easypost.requestor import (
9+
RequestMethod,
10+
Requestor,
11+
)
12+
from easypost.services.base_service import BaseService
13+
14+
15+
class SmartRateService(BaseService):
16+
def __init__(self, client):
17+
self._client = client
18+
self._model_class = "Smartrate"
19+
20+
def estimate_delivery_date(self, **params) -> List[Dict[str, Any]]:
21+
"""Retrieve the estimated delivery date of each carrier-service level combination via the
22+
Smart Deliver By API, based on a specific ship date and origin-destination postal code pair.
23+
"""
24+
url = "/smartrate/deliver_by"
25+
26+
response = Requestor(self._client).request(method=RequestMethod.POST, url=url, params=params)
27+
28+
return convert_to_easypost_object(response=response)
29+
30+
def recommend_ship_date(self, **params) -> List[Dict[str, Any]]:
31+
"""Retrieve a recommended ship date for each carrier-service level combination via the
32+
Smart Deliver On API, based on a specific delivery date and origin-destination postal code pair.
33+
"""
34+
url = "/smartrate/deliver_on"
35+
36+
response = Requestor(self._client).request(method=RequestMethod.POST, url=url, params=params)
37+
38+
return convert_to_easypost_object(response=response)

tests/cassettes/test_retrieve_estimated_delivery_date.yaml

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

tests/cassettes/test_shipment_recommend_ship_date.yaml

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

tests/cassettes/test_smartrate_estimate_delivery_date.yaml

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

tests/cassettes/test_smartrate_recommend_ship_date.yaml

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

tests/conftest.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,9 @@ def rma_form_options():
344344

345345
@pytest.fixture
346346
def planned_ship_date():
347-
return "2023-12-28"
347+
return "2024-07-11"
348+
349+
350+
@pytest.fixture
351+
def desired_delivery_date():
352+
return "2024-07-16"

0 commit comments

Comments
 (0)