Skip to content

Commit 018d124

Browse files
authored
Merge pull request #243 from EasyPost/carrier_account_register
feat: adds register function to CarrierAccount for custom registration carriers
2 parents 78f3c82 + 06c6b44 commit 018d124

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## NEXT RELEASE
4+
5+
- Adds `register` function to CarrierAccount for carriers with custom registration such as FedEx or UPS
6+
37
## v7.6.1 (2022-10-24)
48

59
- Concatenates `error.message` if it incorrectly comes back from the API as a list

easypost/carrier_account.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@ def types(cls, api_key: Optional[str] = None) -> List[str]:
2323
requestor = Requestor(local_api_key=api_key)
2424
response, api_key = requestor.request(method=RequestMethod.GET, url="/carrier_types")
2525
return convert_to_easypost_object(response=response, api_key=api_key)
26+
27+
@classmethod
28+
def register(cls, api_key: Optional[str] = None, **params) -> "CarrierAccount":
29+
"""Creates a Carrier Account that requires custom registration (eg: FedEx & UPS)."""
30+
requestor = Requestor(local_api_key=api_key)
31+
url = f"{cls.class_url()}/register"
32+
wrapped_params = {cls.snakecase_name(): params}
33+
response, api_key = requestor.request(method=RequestMethod.POST, url=url, params=wrapped_params)
34+
return convert_to_easypost_object(response=response, api_key=api_key)

easypost/error.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def __init__(
2020
self.http_status = http_status
2121
self.http_body = http_body
2222
self.original_exception = original_exception
23+
# TODO: add missing `errors` param among others in thread-safe rewrite and update tests
24+
2325
if http_body:
2426
try:
2527
self.json_body = json.loads(http_body)

tests/cassettes/test_carrier_account_register.yaml

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_carrier_account.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,22 @@ def test_carrier_account_type(prod_api_key):
6363
types = easypost.CarrierAccount.types()
6464

6565
assert isinstance(types, list)
66+
67+
68+
@pytest.mark.vcr()
69+
def test_carrier_account_register(prod_api_key):
70+
"""Test register a Carrier Account with custom registration such as FedEx or UPS.
71+
72+
We purposefully don't pass data here because real data is required for this endpoint
73+
which we don't have in a test context, simply assert the error matches when no data is passed.
74+
"""
75+
carrier_account = {
76+
"type": "UpsAccount",
77+
"registration_data": {},
78+
}
79+
80+
try:
81+
easypost.CarrierAccount.register(**carrier_account)
82+
except easypost.Error as error:
83+
# TODO: Assert against error.errors when that property gets added
84+
assert '{"field": "account_number", "message": "must be present and a string"}' in error.http_body

0 commit comments

Comments
 (0)