Skip to content

Commit 5036b2a

Browse files
committed
fix: route normal create/update functions based on type
1 parent 77012f5 commit 5036b2a

5 files changed

Lines changed: 41 additions & 41 deletions

File tree

easypost/constant.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@
3636
"FedexAccount",
3737
"FedexSmartpostAccount",
3838
]
39+
_UPS_OATH_CARRIER_ACCOUNT_TYPES = [
40+
"UpsAccount",
41+
"UpsMailInnovationsAccount",
42+
"UpsSurepostAccount",
43+
]
3944
_FILTERS_KEY = "filters"

easypost/services/carrier_account_service.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from easypost.constant import (
99
_CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS,
10+
_UPS_OATH_CARRIER_ACCOUNT_TYPES,
1011
MISSING_PARAMETER_ERROR,
1112
)
1213
from easypost.easypost_object import convert_to_easypost_object
@@ -32,7 +33,10 @@ def create(self, **params) -> CarrierAccount:
3233
raise MissingParameterError(MISSING_PARAMETER_ERROR.format("type"))
3334

3435
url = self._select_carrier_account_creation_endpoint(carrier_account_type=carrier_account_type)
35-
wrapped_params = {self._snakecase_name(self._model_class): params}
36+
if carrier_account_type in _UPS_OATH_CARRIER_ACCOUNT_TYPES:
37+
wrapped_params = {"ups_oauth_registrations": params}
38+
else:
39+
wrapped_params = {self._snakecase_name(self._model_class): params}
3640

3741
response = Requestor(self._client).request(method=RequestMethod.POST, url=url, params=wrapped_params)
3842

@@ -48,7 +52,11 @@ def retrieve(self, id: str) -> CarrierAccount:
4852

4953
def update(self, id: str, **params) -> CarrierAccount:
5054
"""Update a CarrierAccount."""
51-
return self._update_resource(self._model_class, id, **params)
55+
if params.get("type") in _UPS_OATH_CARRIER_ACCOUNT_TYPES:
56+
class_name = "UpsOauthRegistrations"
57+
else:
58+
class_name = self._model_class
59+
return self._update_resource(class_name, id, **params)
5260

5361
def delete(self, id: str) -> None:
5462
"""Delete a CarrierAccount."""
@@ -60,26 +68,11 @@ def types(self) -> List[Dict[str, Any]]:
6068

6169
return convert_to_easypost_object(response=response)
6270

63-
def create_ups(self, **params) -> CarrierAccount:
64-
"""Create a UPS CarrierAccount which has its own custom workflow."""
65-
url = "/ups_oauth_registrations"
66-
wrapped_params = {"ups_oauth_registrations": params}
67-
68-
response = Requestor(self._client).request(
69-
method=RequestMethod.POST,
70-
url=url,
71-
params=wrapped_params,
72-
)
73-
74-
return convert_to_easypost_object(response=response)
75-
76-
def update_ups(self, id: str, **params) -> CarrierAccount:
77-
"""Update a UPS CarrierAccount which has its own custom workflow."""
78-
return self._update_resource("UpsOauthRegistrations", id, **params)
79-
8071
def _select_carrier_account_creation_endpoint(self, carrier_account_type: Optional[Any]) -> str:
8172
"""Determines which API endpoint to use for the creation call."""
8273
if carrier_account_type in _CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS:
8374
return "/carrier_accounts/register"
75+
elif carrier_account_type in _UPS_OATH_CARRIER_ACCOUNT_TYPES:
76+
return "/ups_oauth_registrations"
8477

8578
return "/carrier_accounts"

tests/cassettes/test_carrier_account_create_ups.yaml

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

tests/cassettes/test_carrier_account_update_ups.yaml

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

tests/test_carrier_account.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ def test_carrier_account_create_ups(prod_client):
104104
"""
105105
carrier_account = {
106106
"type": "UpsAccount",
107+
"account_number": 123,
107108
}
108109

109110
try:
110-
prod_client.carrier_account.create_ups(**carrier_account)
111+
prod_client.carrier_account.create(**carrier_account)
111112
except ApiError as error:
112113
assert error.http_status == 422
113114
assert any(
@@ -121,13 +122,14 @@ def test_carrier_account_update_ups(prod_client):
121122
"""Test updating a UPS Carrier Account.
122123
123124
We purposefully don't pass data here because real data is required for this endpoint
124-
which we don't have in a test context, simply assert that we routed the request correctly.
125+
which we don't have in a test context, simply assert that we sent the request correctly.
125126
"""
126127
carrier_account = {
127128
"type": "UpsAccount",
129+
"account_number": 123,
128130
}
129131

130132
try:
131-
prod_client.carrier_account.update_ups("ca_123", **carrier_account)
133+
prod_client.carrier_account.update("ca_123", **carrier_account)
132134
except ApiError as error:
133135
assert error.http_status == 404

0 commit comments

Comments
 (0)