Skip to content

Commit fe17546

Browse files
authored
Handle adding carrier accounts that require custom workflows (#245)
- Overload existing `create` function in carrier_account to dynamically adjust URL based on carrier account type - Update unit test, cassette
1 parent 93825c2 commit fe17546

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## NEXT RELEASE
44

5-
- Adds `register` function to CarrierAccount for carriers with custom registration such as FedEx or UPS
5+
- [ADDED] `create` function for CarrierAccount now supports carrier accounts with custom workflows (e.g. FedEx, UPS)
66

77
## v7.6.1 (2022-10-24)
88

easypost/carrier_account.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from typing import (
2+
Any,
23
List,
34
Optional,
45
)
56

7+
from easypost.constant import _CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS
68
from easypost.easypost_object import convert_to_easypost_object
79
from easypost.requestor import (
810
RequestMethod,
@@ -16,6 +18,14 @@
1618
)
1719

1820

21+
def _select_carrier_account_creation_endpoint(carrier_account_type: Optional[Any]) -> str:
22+
"""Determines which API endpoint to use for the creation call."""
23+
if carrier_account_type in _CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS:
24+
return "/carrier_accounts/register"
25+
26+
return "/carrier_accounts"
27+
28+
1929
class CarrierAccount(AllResource, CreateResource, UpdateResource, DeleteResource):
2030
@classmethod
2131
def types(cls, api_key: Optional[str] = None) -> List[str]:
@@ -24,11 +34,18 @@ def types(cls, api_key: Optional[str] = None) -> List[str]:
2434
response, api_key = requestor.request(method=RequestMethod.GET, url="/carrier_types")
2535
return convert_to_easypost_object(response=response, api_key=api_key)
2636

37+
# Overrides CreateResource.create
2738
@classmethod
28-
def register(cls, api_key: Optional[str] = None, **params) -> "CarrierAccount":
39+
def create(cls, api_key: Optional[str] = None, **params) -> "CarrierAccount":
2940
"""Creates a Carrier Account that requires custom registration (eg: FedEx & UPS)."""
3041
requestor = Requestor(local_api_key=api_key)
31-
url = f"{cls.class_url()}/register"
42+
43+
carrier_account_type = params.get("type")
44+
45+
if carrier_account_type is None:
46+
raise ValueError("Missing required parameter 'type'")
47+
48+
url = _select_carrier_account_creation_endpoint(carrier_account_type=carrier_account_type)
3249
wrapped_params = {cls.snakecase_name(): params}
3350
response, api_key = requestor.request(method=RequestMethod.POST, url=url, params=wrapped_params)
3451
return convert_to_easypost_object(response=response, api_key=api_key)

easypost/constant.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
SUPPORT_EMAIL = "[email protected]"
22
TIMEOUT = 60
3+
4+
_CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS = [
5+
"FedexAccount",
6+
"UpsAccount",
7+
]

tests/cassettes/test_carrier_account_register.yaml renamed to tests/cassettes/test_carrier_account_create_with_custom_workflow.yaml

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_carrier_account.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test_carrier_account_type(prod_api_key):
6666

6767

6868
@pytest.mark.vcr()
69-
def test_carrier_account_register(prod_api_key):
69+
def test_carrier_account_create_with_custom_workflow(prod_api_key):
7070
"""Test register a Carrier Account with custom registration such as FedEx or UPS.
7171
7272
We purposefully don't pass data here because real data is required for this endpoint
@@ -78,7 +78,7 @@ def test_carrier_account_register(prod_api_key):
7878
}
7979

8080
try:
81-
easypost.CarrierAccount.register(**carrier_account)
81+
easypost.CarrierAccount.create(**carrier_account)
8282
except easypost.Error as error:
8383
# TODO: Assert against error.errors when that property gets added
8484
assert '{"field": "account_number", "message": "must be present and a string"}' in error.http_body

0 commit comments

Comments
 (0)