Skip to content

Commit

Permalink
Merge pull request #243 from EasyPost/carrier_account_register
Browse files Browse the repository at this point in the history
feat: adds register function to CarrierAccount for custom registration carriers
  • Loading branch information
Justintime50 authored Nov 8, 2022
2 parents 78f3c82 + 06c6b44 commit 018d124
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## NEXT RELEASE

- Adds `register` function to CarrierAccount for carriers with custom registration such as FedEx or UPS

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

- Concatenates `error.message` if it incorrectly comes back from the API as a list
Expand Down
9 changes: 9 additions & 0 deletions easypost/carrier_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ def types(cls, api_key: Optional[str] = None) -> List[str]:
requestor = Requestor(local_api_key=api_key)
response, api_key = requestor.request(method=RequestMethod.GET, url="/carrier_types")
return convert_to_easypost_object(response=response, api_key=api_key)

@classmethod
def register(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"
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)
2 changes: 2 additions & 0 deletions easypost/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def __init__(
self.http_status = http_status
self.http_body = http_body
self.original_exception = original_exception
# TODO: add missing `errors` param among others in thread-safe rewrite and update tests

if http_body:
try:
self.json_body = json.loads(http_body)
Expand Down
78 changes: 78 additions & 0 deletions tests/cassettes/test_carrier_account_register.yaml

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

19 changes: 19 additions & 0 deletions tests/test_carrier_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,22 @@ def test_carrier_account_type(prod_api_key):
types = easypost.CarrierAccount.types()

assert isinstance(types, list)


@pytest.mark.vcr()
def test_carrier_account_register(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
which we don't have in a test context, simply assert the error matches when no data is passed.
"""
carrier_account = {
"type": "UpsAccount",
"registration_data": {},
}

try:
easypost.CarrierAccount.register(**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 018d124

Please sign in to comment.