Skip to content

Commit

Permalink
fix: route normal create/update functions based on type
Browse files Browse the repository at this point in the history
  • Loading branch information
Justintime50 committed Jul 8, 2024
1 parent 77012f5 commit 5036b2a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 41 deletions.
5 changes: 5 additions & 0 deletions easypost/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@
"FedexAccount",
"FedexSmartpostAccount",
]
_UPS_OATH_CARRIER_ACCOUNT_TYPES = [
"UpsAccount",
"UpsMailInnovationsAccount",
"UpsSurepostAccount",
]
_FILTERS_KEY = "filters"
31 changes: 12 additions & 19 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:
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 @@ -60,26 +68,11 @@ def types(self) -> List[Dict[str, Any]]:

return convert_to_easypost_object(response=response)

def create_ups(self, **params) -> CarrierAccount:
"""Create a UPS CarrierAccount which has its own custom workflow."""
url = "/ups_oauth_registrations"
wrapped_params = {"ups_oauth_registrations": params}

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

return convert_to_easypost_object(response=response)

def update_ups(self, id: str, **params) -> CarrierAccount:
"""Update a UPS CarrierAccount which has its own custom workflow."""
return self._update_resource("UpsOauthRegistrations", id, **params)

def _select_carrier_account_creation_endpoint(self, carrier_account_type: Optional[Any]) -> str:
"""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"
24 changes: 12 additions & 12 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.

14 changes: 7 additions & 7 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.

8 changes: 5 additions & 3 deletions tests/test_carrier_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ def test_carrier_account_create_ups(prod_client):
"""
carrier_account = {
"type": "UpsAccount",
"account_number": 123,
}

try:
prod_client.carrier_account.create_ups(**carrier_account)
prod_client.carrier_account.create(**carrier_account)
except ApiError as error:
assert error.http_status == 422
assert any(
Expand All @@ -121,13 +122,14 @@ 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 routed the request correctly.
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_ups("ca_123", **carrier_account)
prod_client.carrier_account.update("ca_123", **carrier_account)
except ApiError as error:
assert error.http_status == 404

0 comments on commit 5036b2a

Please sign in to comment.