Skip to content

Commit a2a930d

Browse files
committed
chore: move get_lowest_stateless_rate to utils
1 parent 8261a67 commit a2a930d

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

easypost/beta/rate.py

-28
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from typing import (
22
Any,
33
Dict,
4-
List,
54
Optional,
65
)
76

87
from easypost.easypost_object import convert_to_easypost_object
9-
from easypost.error import Error
108
from easypost.requestor import (
119
RequestMethod,
1210
Requestor,
@@ -26,29 +24,3 @@ def retrieve_stateless_rates(cls, api_key: Optional[str] = None, **params) -> Di
2624
response, api_key = requestor.request(method=RequestMethod.POST, url=url, params=wrapped_params, beta=True)
2725

2826
return convert_to_easypost_object(response=response.get("rates", None), api_key=api_key)
29-
30-
@classmethod
31-
def get_lowest_stateless_rate(
32-
cls, stateless_rates: List[Dict[str, Any]], carriers: List[str] = None, services: List[str] = None
33-
) -> Dict[str, Any]:
34-
"""Get the lowest stateless rate."""
35-
carriers = carriers or []
36-
services = services or []
37-
lowest_rate = None
38-
39-
carriers = [carrier.lower() for carrier in carriers]
40-
services = [service.lower() for service in services]
41-
42-
for rate in stateless_rates:
43-
if (carriers and rate["carrier"].lower() not in carriers) or (
44-
services and rate["service"].lower() not in services
45-
):
46-
continue
47-
48-
if lowest_rate is None or float(rate.rate) < float(lowest_rate.rate):
49-
lowest_rate = rate
50-
51-
if lowest_rate is None:
52-
raise Error(message="No rates found.")
53-
54-
return lowest_rate

easypost/util.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from typing import List
1+
from typing import (
2+
Any,
3+
Dict,
4+
List,
5+
)
26

37
from easypost.easypost_object import EasyPostObject
48
from easypost.error import Error
@@ -29,3 +33,29 @@ def get_lowest_object_rate(
2933
raise Error(message="No rates found.")
3034

3135
return lowest_rate
36+
37+
38+
def get_lowest_stateless_rate(
39+
stateless_rates: List[Dict[str, Any]], carriers: List[str] = None, services: List[str] = None
40+
) -> Dict[str, Any]:
41+
"""Get the lowest stateless rate."""
42+
carriers = carriers or []
43+
services = services or []
44+
lowest_rate = None
45+
46+
carriers = [carrier.lower() for carrier in carriers]
47+
services = [service.lower() for service in services]
48+
49+
for rate in stateless_rates:
50+
if (carriers and rate["carrier"].lower() not in carriers) or (
51+
services and rate["service"].lower() not in services
52+
):
53+
continue
54+
55+
if lowest_rate is None or float(rate.rate) < float(lowest_rate.rate):
56+
lowest_rate = rate
57+
58+
if lowest_rate is None:
59+
raise Error(message="No rates found.")
60+
61+
return lowest_rate

tests/test_beta_rate.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
import easypost
4+
from easypost.util import get_lowest_stateless_rate
45

56

67
@pytest.mark.vcr()
@@ -16,6 +17,6 @@ def test_beta_get_lowest_stateless_rate(basic_shipment):
1617
"""Tests that we can return the lowest stateless rate from a list of stateless rates."""
1718
stateless_rates = easypost.beta.Rate.retrieve_stateless_rates(**basic_shipment)
1819

19-
lowest_stateless_rate = easypost.beta.Rate.get_lowest_stateless_rate(stateless_rates)
20+
lowest_stateless_rate = get_lowest_stateless_rate(stateless_rates)
2021

2122
assert lowest_stateless_rate["service"] == "First"

0 commit comments

Comments
 (0)