Skip to content

Commit

Permalink
Handle adding carrier accounts that require custom workflows (#245)
Browse files Browse the repository at this point in the history
- Overload existing `create` function in carrier_account to dynamically adjust URL based on carrier account type
- Update unit test, cassette
  • Loading branch information
nwithan8 authored Nov 23, 2022
1 parent 93825c2 commit fe17546
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## NEXT RELEASE

- Adds `register` function to CarrierAccount for carriers with custom registration such as FedEx or UPS
- [ADDED] `create` function for CarrierAccount now supports carrier accounts with custom workflows (e.g. FedEx, UPS)

## v7.6.1 (2022-10-24)

Expand Down
21 changes: 19 additions & 2 deletions easypost/carrier_account.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import (
Any,
List,
Optional,
)

from easypost.constant import _CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS
from easypost.easypost_object import convert_to_easypost_object
from easypost.requestor import (
RequestMethod,
Expand All @@ -16,6 +18,14 @@
)


def _select_carrier_account_creation_endpoint(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"

return "/carrier_accounts"


class CarrierAccount(AllResource, CreateResource, UpdateResource, DeleteResource):
@classmethod
def types(cls, api_key: Optional[str] = None) -> List[str]:
Expand All @@ -24,11 +34,18 @@ def types(cls, api_key: Optional[str] = None) -> List[str]:
response, api_key = requestor.request(method=RequestMethod.GET, url="/carrier_types")
return convert_to_easypost_object(response=response, api_key=api_key)

# Overrides CreateResource.create
@classmethod
def register(cls, api_key: Optional[str] = None, **params) -> "CarrierAccount":
def create(cls, api_key: Optional[str] = None, **params) -> "CarrierAccount":
"""Creates a Carrier Account that requires custom registration (eg: FedEx & UPS)."""
requestor = Requestor(local_api_key=api_key)
url = f"{cls.class_url()}/register"

carrier_account_type = params.get("type")

if carrier_account_type is None:
raise ValueError("Missing required parameter 'type'")

url = _select_carrier_account_creation_endpoint(carrier_account_type=carrier_account_type)
wrapped_params = {cls.snakecase_name(): params}
response, api_key = requestor.request(method=RequestMethod.POST, url=url, params=wrapped_params)
return convert_to_easypost_object(response=response, api_key=api_key)
5 changes: 5 additions & 0 deletions easypost/constant.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
SUPPORT_EMAIL = "[email protected]"
TIMEOUT = 60

_CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS = [
"FedexAccount",
"UpsAccount",
]

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

4 changes: 2 additions & 2 deletions tests/test_carrier_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_carrier_account_type(prod_api_key):


@pytest.mark.vcr()
def test_carrier_account_register(prod_api_key):
def test_carrier_account_create_with_custom_workflow(prod_api_key):
"""Test register a Carrier Account with custom registration such as FedEx or UPS.
We purposefully don't pass data here because real data is required for this endpoint
Expand All @@ -78,7 +78,7 @@ def test_carrier_account_register(prod_api_key):
}

try:
easypost.CarrierAccount.register(**carrier_account)
easypost.CarrierAccount.create(**carrier_account)
except easypost.Error as error:
# TODO: Assert against error.errors when that property gets added
assert '{"field": "account_number", "message": "must be present and a string"}' in error.http_body

0 comments on commit fe17546

Please sign in to comment.