Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Account for new UPS endpoint in carrier account create/update functions #336

Merged
merged 7 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions easypost/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
_CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS = [
"FedexAccount",
"FedexSmartpostAccount",
]
_UPS_OATH_CARRIER_ACCOUNT_TYPES = [
"UpsAccount",
"UpsMailInnovationsAccount",
"UpsSurepostAccount",
]
_FILTERS_KEY = "filters"
14 changes: 12 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_OATH_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_OATH_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,11 @@ 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)
if params.get("type") in _UPS_OATH_CARRIER_ACCOUNT_TYPES:
Justintime50 marked this conversation as resolved.
Show resolved Hide resolved
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 +72,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_OATH_CARRIER_ACCOUNT_TYPES:
return "/ups_oauth_registrations"

return "/carrier_accounts"
69 changes: 69 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.

70 changes: 70 additions & 0 deletions tests/cassettes/test_carrier_account_update_ups.yaml

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

44 changes: 42 additions & 2 deletions tests/test_carrier_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ def test_carrier_account_type(prod_client):

@pytest.mark.vcr()
def test_carrier_account_create_with_custom_workflow(prod_client):
"""Test register a Carrier Account with custom registration such as FedEx or UPS.
"""Test registering a Carrier Account with custom workflow.

We purposefully don't pass data here because real data is required for this endpoint
which we don't have in a test context, simply assert the error matches when no data is passed.
"""
carrier_account = {
"type": "UpsAccount",
"type": "FedexAccount",
"registration_data": {},
}

Expand All @@ -93,3 +93,43 @@ def test_carrier_account_create_with_custom_workflow(prod_client):
[error["field"] == "account_number" and error["message"] == "must be present and a string"]
for error in error.errors
)


@pytest.mark.vcr()
def test_carrier_account_create_ups(prod_client):
"""Test registering a UPS Carrier Account.

We purposefully don't pass data here because real data is required for this endpoint
which we don't have in a test context, simply assert the error matches when no data is passed.
"""
carrier_account = {
"type": "UpsAccount",
"account_number": 123,
}

try:
prod_client.carrier_account.create(**carrier_account)
except ApiError as error:
assert error.http_status == 422
assert any(
[error["field"] == "account_number" and error["message"] == "must be present and a string"]
for error in error.errors
)


@pytest.mark.vcr()
def test_carrier_account_update_ups(prod_client):
"""Test updating a UPS Carrier Account.

We purposefully don't pass data here because real data is required for this endpoint
which we don't have in a test context, simply assert that we sent the request correctly.
"""
carrier_account = {
"type": "UpsAccount",
"account_number": 123,
}

try:
prod_client.carrier_account.update("ca_123", **carrier_account)
except ApiError as error:
assert error.http_status == 404
Loading