From 5036b2accd2fb708314cb0726c08e5442fabdf3b Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Mon, 8 Jul 2024 14:27:16 -0600 Subject: [PATCH] fix: route normal create/update functions based on type --- easypost/constant.py | 5 +++ easypost/services/carrier_account_service.py | 31 +++++++------------ .../test_carrier_account_create_ups.yaml | 24 +++++++------- .../test_carrier_account_update_ups.yaml | 14 ++++----- tests/test_carrier_account.py | 8 +++-- 5 files changed, 41 insertions(+), 41 deletions(-) diff --git a/easypost/constant.py b/easypost/constant.py index b076129..b90ae70 100644 --- a/easypost/constant.py +++ b/easypost/constant.py @@ -36,4 +36,9 @@ "FedexAccount", "FedexSmartpostAccount", ] +_UPS_OATH_CARRIER_ACCOUNT_TYPES = [ + "UpsAccount", + "UpsMailInnovationsAccount", + "UpsSurepostAccount", +] _FILTERS_KEY = "filters" diff --git a/easypost/services/carrier_account_service.py b/easypost/services/carrier_account_service.py index 9b21026..995c8d1 100644 --- a/easypost/services/carrier_account_service.py +++ b/easypost/services/carrier_account_service.py @@ -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 @@ -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) @@ -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.""" @@ -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" diff --git a/tests/cassettes/test_carrier_account_create_ups.yaml b/tests/cassettes/test_carrier_account_create_ups.yaml index 5cf98f8..07ff059 100644 --- a/tests/cassettes/test_carrier_account_create_ups.yaml +++ b/tests/cassettes/test_carrier_account_create_ups.yaml @@ -1,6 +1,6 @@ interactions: - request: - body: '{"ups_oauth_registrations": {"type": "UpsAccount"}}' + body: '{"ups_oauth_registrations": {"type": "UpsAccount", "account_number": 123}}' headers: Accept: - '*/*' @@ -9,7 +9,7 @@ interactions: Connection: - keep-alive Content-Length: - - '51' + - '74' Content-Type: - application/json authorization: @@ -20,14 +20,14 @@ interactions: uri: https://api.easypost.com/v2/ups_oauth_registrations response: body: - string: '{"error": {"code": "PARAMETER.REQUIRED", "message": "Missing required - parameter.", "errors": [{"field": "ups_oauth_registrations.account_number", - "message": "is required but missing"}]}}' + string: '{"error": {"code": "PARAMETER.INVALID_TYPE", "message": "Wrong parameter + type.", "errors": [{"field": "ups_oauth_registrations.account_number", "message": + "must be a string"}]}}' headers: cache-control: - private, no-cache, no-store content-length: - - '177' + - '168' content-type: - application/json; charset=utf-8 expires: @@ -47,20 +47,20 @@ interactions: x-download-options: - noopen x-ep-request-uuid: - - cbf7748e6685bf59e2bc356000275a02 + - b98ca9f3668c49a7e2cb1d1f001f7231 x-frame-options: - SAMEORIGIN x-node: - - bigweb35nuq + - bigweb36nuq x-permitted-cross-domain-policies: - none x-proxied: - - intlb3nuq fa152d4755 - - extlb1nuq fa152d4755 + - intlb4nuq fa152d4755 + - extlb2nuq fa152d4755 x-runtime: - - '0.043536' + - '0.053482' x-version-label: - - easypost-202407031852-b015027e75-master + - easypost-202407081949-3228d5845b-master x-xss-protection: - 1; mode=block status: diff --git a/tests/cassettes/test_carrier_account_update_ups.yaml b/tests/cassettes/test_carrier_account_update_ups.yaml index ba6d51d..7957e5d 100644 --- a/tests/cassettes/test_carrier_account_update_ups.yaml +++ b/tests/cassettes/test_carrier_account_update_ups.yaml @@ -1,6 +1,6 @@ interactions: - request: - body: '{"ups_oauth_registrations": {"type": "UpsAccount"}}' + body: '{"ups_oauth_registrations": {"type": "UpsAccount", "account_number": 123}}' headers: Accept: - '*/*' @@ -9,7 +9,7 @@ interactions: Connection: - keep-alive Content-Length: - - '51' + - '74' Content-Type: - application/json authorization: @@ -48,20 +48,20 @@ interactions: x-download-options: - noopen x-ep-request-uuid: - - cbf774916685c061e2bc38b300282931 + - b98ca9f4668c4b72e2cb215a00213aef x-frame-options: - SAMEORIGIN x-node: - - bigweb39nuq + - bigweb36nuq x-permitted-cross-domain-policies: - none x-proxied: - intlb4nuq fa152d4755 - - extlb1nuq fa152d4755 + - extlb2nuq fa152d4755 x-runtime: - - '0.023543' + - '0.033617' x-version-label: - - easypost-202407031852-b015027e75-master + - easypost-202407081949-3228d5845b-master x-xss-protection: - 1; mode=block status: diff --git a/tests/test_carrier_account.py b/tests/test_carrier_account.py index 114358b..b129c3f 100644 --- a/tests/test_carrier_account.py +++ b/tests/test_carrier_account.py @@ -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( @@ -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