Skip to content

Commit

Permalink
Merge pull request #336 from EasyPost/ups_oauth
Browse files Browse the repository at this point in the history
feat: add new create_ups and update_ups functions
  • Loading branch information
Justintime50 authored Jul 9, 2024
2 parents 0b650b6 + 61d8fcb commit 02b474d
Show file tree
Hide file tree
Showing 9 changed files with 594 additions and 61 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## Next Release

- Routes `UpsAccount`, `UpsMailInnovationsAccount`, and `UpsSurepostAccount` create/update requests to the new `/ups_oauth_registrations` endpoint
- 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

## v9.2.0 (2024-04-10)

- Fix payment method funding and deletion failures due to undetermined payment method type
Expand Down
5 changes: 5 additions & 0 deletions easypost/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
_CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS = [
"FedexAccount",
"FedexSmartpostAccount",
]
_UPS_OAUTH_CARRIER_ACCOUNT_TYPES = [
"UpsAccount",
"UpsMailInnovationsAccount",
"UpsSurepostAccount",
]
_FILTERS_KEY = "filters"
_EXCLUDED_CLASS_NAMES = ["ups_oauth_registrations"]
5 changes: 4 additions & 1 deletion easypost/services/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
)

from easypost.constant import (
_EXCLUDED_CLASS_NAMES,
_FILTERS_KEY,
NO_MORE_PAGES_ERROR,
)
Expand All @@ -30,7 +31,9 @@ def _snakecase_name(self, class_name: str) -> str:
def _class_url(self, class_name: str) -> str:
"""Generate a URL based on class name."""
transformed_class_name = self._snakecase_name(class_name)
if transformed_class_name[-1:] in ("s", "h"):
if transformed_class_name in _EXCLUDED_CLASS_NAMES:
return f"/{transformed_class_name}"
elif transformed_class_name[-1:] in ("s", "h"):
return f"/{transformed_class_name}es"
else:
return f"/{transformed_class_name}s"
Expand Down
17 changes: 15 additions & 2 deletions easypost/services/carrier_account_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from easypost.constant import (
_CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS,
_UPS_OAUTH_CARRIER_ACCOUNT_TYPES,
MISSING_PARAMETER_ERROR,
)
from easypost.easypost_object import convert_to_easypost_object
Expand All @@ -32,7 +33,10 @@ def create(self, **params) -> CarrierAccount:
raise MissingParameterError(MISSING_PARAMETER_ERROR.format("type"))

url = self._select_carrier_account_creation_endpoint(carrier_account_type=carrier_account_type)
wrapped_params = {self._snakecase_name(self._model_class): params}
if carrier_account_type in _UPS_OAUTH_CARRIER_ACCOUNT_TYPES:
wrapped_params = {"ups_oauth_registrations": params}
else:
wrapped_params = {self._snakecase_name(self._model_class): params}

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

Expand All @@ -48,7 +52,14 @@ def retrieve(self, id: str) -> CarrierAccount:

def update(self, id: str, **params) -> CarrierAccount:
"""Update a CarrierAccount."""
return self._update_resource(self._model_class, id, **params)
carrier_account = self.retrieve(id)

if carrier_account.get("type") in _UPS_OAUTH_CARRIER_ACCOUNT_TYPES:
class_name = "UpsOauthRegistrations"
else:
class_name = self._model_class

return self._update_resource(class_name, id, **params)

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

return "/carrier_accounts"
136 changes: 136 additions & 0 deletions tests/cassettes/test_carrier_account_create_ups.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 02b474d

Please sign in to comment.