Skip to content

Commit 02b474d

Browse files
authored
Merge pull request #336 from EasyPost/ups_oauth
feat: add new create_ups and update_ups functions
2 parents 0b650b6 + 61d8fcb commit 02b474d

9 files changed

+594
-61
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## Next Release
4+
5+
- Routes `UpsAccount`, `UpsMailInnovationsAccount`, and `UpsSurepostAccount` create/update requests to the new `/ups_oauth_registrations` endpoint
6+
- 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
7+
38
## v9.2.0 (2024-04-10)
49

510
- Fix payment method funding and deletion failures due to undetermined payment method type

easypost/constant.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
_CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS = [
3636
"FedexAccount",
3737
"FedexSmartpostAccount",
38+
]
39+
_UPS_OAUTH_CARRIER_ACCOUNT_TYPES = [
3840
"UpsAccount",
41+
"UpsMailInnovationsAccount",
42+
"UpsSurepostAccount",
3943
]
4044
_FILTERS_KEY = "filters"
45+
_EXCLUDED_CLASS_NAMES = ["ups_oauth_registrations"]

easypost/services/base_service.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
)
77

88
from easypost.constant import (
9+
_EXCLUDED_CLASS_NAMES,
910
_FILTERS_KEY,
1011
NO_MORE_PAGES_ERROR,
1112
)
@@ -30,7 +31,9 @@ def _snakecase_name(self, class_name: str) -> str:
3031
def _class_url(self, class_name: str) -> str:
3132
"""Generate a URL based on class name."""
3233
transformed_class_name = self._snakecase_name(class_name)
33-
if transformed_class_name[-1:] in ("s", "h"):
34+
if transformed_class_name in _EXCLUDED_CLASS_NAMES:
35+
return f"/{transformed_class_name}"
36+
elif transformed_class_name[-1:] in ("s", "h"):
3437
return f"/{transformed_class_name}es"
3538
else:
3639
return f"/{transformed_class_name}s"

easypost/services/carrier_account_service.py

Lines changed: 15 additions & 2 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_OAUTH_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_OAUTH_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,14 @@ 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+
carrier_account = self.retrieve(id)
56+
57+
if carrier_account.get("type") in _UPS_OAUTH_CARRIER_ACCOUNT_TYPES:
58+
class_name = "UpsOauthRegistrations"
59+
else:
60+
class_name = self._model_class
61+
62+
return self._update_resource(class_name, id, **params)
5263

5364
def delete(self, id: str) -> None:
5465
"""Delete a CarrierAccount."""
@@ -64,5 +75,7 @@ def _select_carrier_account_creation_endpoint(self, carrier_account_type: Option
6475
"""Determines which API endpoint to use for the creation call."""
6576
if carrier_account_type in _CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS:
6677
return "/carrier_accounts/register"
78+
elif carrier_account_type in _UPS_OAUTH_CARRIER_ACCOUNT_TYPES:
79+
return "/ups_oauth_registrations"
6780

6881
return "/carrier_accounts"

tests/cassettes/test_carrier_account_create_ups.yaml

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

tests/cassettes/test_carrier_account_create_with_custom_workflow.yaml

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

0 commit comments

Comments
 (0)