From fe17546f5612acfd939d5ea21ce1827e3f47e12f Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Wed, 23 Nov 2022 14:15:10 -0700 Subject: [PATCH] Handle adding carrier accounts that require custom workflows (#245) - Overload existing `create` function in carrier_account to dynamically adjust URL based on carrier account type - Update unit test, cassette --- CHANGELOG.md | 2 +- easypost/carrier_account.py | 21 +++++++++++++++++-- easypost/constant.py | 5 +++++ ..._account_create_with_custom_workflow.yaml} | 10 ++++----- tests/test_carrier_account.py | 4 ++-- 5 files changed, 32 insertions(+), 10 deletions(-) rename tests/cassettes/{test_carrier_account_register.yaml => test_carrier_account_create_with_custom_workflow.yaml} (93%) diff --git a/CHANGELOG.md b/CHANGELOG.md index eadc7a4d..5031b4b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/easypost/carrier_account.py b/easypost/carrier_account.py index aa4dbbe0..b0d61ea8 100644 --- a/easypost/carrier_account.py +++ b/easypost/carrier_account.py @@ -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, @@ -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]: @@ -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) diff --git a/easypost/constant.py b/easypost/constant.py index f2b2be3e..2aaa0337 100644 --- a/easypost/constant.py +++ b/easypost/constant.py @@ -1,2 +1,7 @@ SUPPORT_EMAIL = "support@easypost.com" TIMEOUT = 60 + +_CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS = [ + "FedexAccount", + "UpsAccount", +] diff --git a/tests/cassettes/test_carrier_account_register.yaml b/tests/cassettes/test_carrier_account_create_with_custom_workflow.yaml similarity index 93% rename from tests/cassettes/test_carrier_account_register.yaml rename to tests/cassettes/test_carrier_account_create_with_custom_workflow.yaml index 3a6ca579..8e0c17e3 100644 --- a/tests/cassettes/test_carrier_account_register.yaml +++ b/tests/cassettes/test_carrier_account_create_with_custom_workflow.yaml @@ -56,7 +56,7 @@ interactions: x-download-options: - noopen x-ep-request-uuid: - - 513791bf636437fee789f242001139c3 + - d96d15af637d449aec7b4a81006202bc x-frame-options: - SAMEORIGIN x-node: @@ -64,12 +64,12 @@ interactions: x-permitted-cross-domain-policies: - none x-proxied: - - intlb1nuq 29913d444b - - extlb1nuq 29913d444b + - intlb2nuq 29913d444b + - extlb2nuq 29913d444b x-runtime: - - '0.038035' + - '0.034429' x-version-label: - - easypost-202211032002-79b51b1468-master + - easypost-202211222057-6fd042c9a6-master x-xss-protection: - 1; mode=block status: diff --git a/tests/test_carrier_account.py b/tests/test_carrier_account.py index 84af650e..14775743 100644 --- a/tests/test_carrier_account.py +++ b/tests/test_carrier_account.py @@ -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 @@ -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