Skip to content

Commit df06afb

Browse files
authored
[feat] Add claims API service (#342)
- Add claims API service - Update fixture data
1 parent bdcaa6f commit df06afb

16 files changed

+2620
-11
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## Next Release
4+
5+
- Adds new `Claim` service for filing claims on EasyPost shipments and insurances
6+
37
## v9.3.0 (2024-07-12)
48

59
- Adds new `shipment.recommend_ship_date`, `smartrate.recommend_ship_date`, and `smartrate.estimate_delivery_date` functions

easypost/easypost_client.py

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
BillingService,
1919
CarrierAccountService,
2020
CarrierMetadataService,
21+
ClaimService,
2122
CustomsInfoService,
2223
CustomsItemService,
2324
EndShipperService,
@@ -62,6 +63,7 @@ def __init__(
6263
self.billing = BillingService(self)
6364
self.carrier_account = CarrierAccountService(self)
6465
self.carrier_metadata = CarrierMetadataService(self)
66+
self.claim = ClaimService(self)
6567
self.customs_info = CustomsInfoService(self)
6668
self.customs_item = CustomsItemService(self)
6769
self.end_shipper = EndShipperService(self)

easypost/easypost_object.py

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"brd": "Brand",
1919
"ca": "CarrierAccount",
2020
"cfrep": "Report",
21+
"clm": "Claim",
2122
"cstinfo": "CustomsInfo",
2223
"cstitem": "CustomsItem",
2324
"es": "EndShipper",

easypost/models/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from easypost.models.billing import Billing
66
from easypost.models.brand import Brand
77
from easypost.models.carrier_account import CarrierAccount
8+
from easypost.models.claim import Claim
89
from easypost.models.customs_info import CustomsInfo
910
from easypost.models.customs_item import CustomsItem
1011
from easypost.models.end_shipper import EndShipper

easypost/models/claim.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from easypost.easypost_object import EasyPostObject
2+
3+
4+
class Claim(EasyPostObject):
5+
pass

easypost/services/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from easypost.services.billing_service import BillingService
88
from easypost.services.carrier_account_service import CarrierAccountService
99
from easypost.services.carrier_metadata_service import CarrierMetadataService
10+
from easypost.services.claim_service import ClaimService
1011
from easypost.services.customs_info_service import CustomsInfoService
1112
from easypost.services.customs_item_service import CustomsItemService
1213
from easypost.services.end_shipper_service import EndShipperService

easypost/services/base_service.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -42,47 +42,51 @@ def _instance_url(self, class_name: str, id: str) -> str:
4242
"""Generate an instance URL based on a class name and ID."""
4343
return f"{self._class_url(class_name)}/{id}"
4444

45-
def _create_resource(self, class_name: str, **params) -> Any:
45+
def _create_resource(self, class_name: str, beta: bool = False, **params) -> Any:
4646
"""Create an EasyPost object via the EasyPost API."""
4747
url = self._class_url(class_name)
4848
wrapped_params = {self._snakecase_name(class_name): params}
4949

50-
response = Requestor(self._client).request(method=RequestMethod.POST, url=url, params=wrapped_params)
50+
response = Requestor(self._client).request(method=RequestMethod.POST, url=url, params=wrapped_params, beta=beta)
5151

5252
return convert_to_easypost_object(response=response)
5353

54-
def _all_resources(self, class_name: str, filters: Optional[Dict[str, Any]] = None, **params) -> Any:
54+
def _all_resources(
55+
self, class_name: str, filters: Optional[Dict[str, Any]] = None, beta: bool = False, **params
56+
) -> Any:
5557
"""Retrieve a list of EasyPostObjects from the EasyPost API."""
5658
url = self._class_url(class_name)
57-
response = Requestor(self._client).request(method=RequestMethod.GET, url=url, params=params)
59+
response = Requestor(self._client).request(method=RequestMethod.GET, url=url, params=params, beta=beta)
5860

5961
if filters: # presence of filters indicates we are dealing with a paginated response
6062
response[_FILTERS_KEY] = filters # Save the filters used to reference in potential get_next_page call
6163

6264
return convert_to_easypost_object(response=response)
6365

64-
def _retrieve_resource(self, class_name: str, id: str) -> Any:
66+
def _retrieve_resource(self, class_name: str, id: str, beta: bool = False) -> Any:
6567
"""Retrieve an object from the EasyPost API."""
6668
url = self._instance_url(class_name, id)
6769

68-
response = Requestor(self._client).request(method=RequestMethod.GET, url=url)
70+
response = Requestor(self._client).request(method=RequestMethod.GET, url=url, beta=beta)
6971

7072
return convert_to_easypost_object(response=response)
7173

72-
def _update_resource(self, class_name: str, id: str, method: RequestMethod = RequestMethod.PATCH, **params) -> Any:
74+
def _update_resource(
75+
self, class_name: str, id: str, method: RequestMethod = RequestMethod.PATCH, beta: bool = False, **params
76+
) -> Any:
7377
"""Update an EasyPost object via the EasyPost API."""
7478
url = self._instance_url(class_name, id)
7579
wrapped_params = {self._snakecase_name(class_name): params}
7680

77-
response = Requestor(self._client).request(method=method, url=url, params=wrapped_params)
81+
response = Requestor(self._client).request(method=method, url=url, params=wrapped_params, beta=beta)
7882

7983
return convert_to_easypost_object(response=response)
8084

81-
def _delete_resource(self, class_name: str, id: str) -> Any:
85+
def _delete_resource(self, class_name: str, id: str, beta: bool = False) -> Any:
8286
"""Delete an EasyPost object via the EasyPost API."""
8387
url = self._instance_url(class_name, id)
8488

85-
response = Requestor(self._client).request(method=RequestMethod.DELETE, url=url)
89+
response = Requestor(self._client).request(method=RequestMethod.DELETE, url=url, beta=beta)
8690

8791
return convert_to_easypost_object(response=response)
8892

easypost/services/claim_service.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from typing import (
2+
Any,
3+
Dict,
4+
Optional,
5+
)
6+
7+
from easypost.easypost_object import convert_to_easypost_object
8+
from easypost.models import Claim
9+
from easypost.requestor import (
10+
RequestMethod,
11+
Requestor,
12+
)
13+
from easypost.services.base_service import BaseService
14+
15+
16+
class ClaimService(BaseService):
17+
def __init__(self, client):
18+
self._client = client
19+
self._model_class = Claim.__name__
20+
21+
def create(self, **params) -> Claim:
22+
"""Create a Claim."""
23+
url = "/claims"
24+
25+
response = Requestor(self._client).request(method=RequestMethod.POST, url=url, params=params, beta=False)
26+
27+
return convert_to_easypost_object(response=response)
28+
29+
def all(self, **params) -> Dict[str, Any]:
30+
"""Retrieve a list of Claims."""
31+
filters = {
32+
"key": "claims",
33+
}
34+
35+
return self._all_resources(class_name=self._model_class, filters=filters, beta=False, **params)
36+
37+
def retrieve(self, id: str) -> Claim:
38+
"""Retrieve a Claim."""
39+
return self._retrieve_resource(class_name=self._model_class, id=id, beta=False)
40+
41+
def get_next_page(
42+
self,
43+
claims: Dict[str, Any],
44+
page_size: int,
45+
optional_params: Optional[Dict[str, Any]] = None,
46+
) -> Dict[str, Any]:
47+
"""Retrieve the next page of the list Claim response."""
48+
self._check_has_next_page(collection=claims)
49+
50+
params = {
51+
"before_id": claims["claims"][-1].id,
52+
"page_size": page_size,
53+
}
54+
55+
if optional_params:
56+
params.update(optional_params)
57+
58+
return self.all(**params)
59+
60+
def cancel(self, id: str) -> Claim:
61+
"""Cancel a Claim."""
62+
url = f"/claims/{id}/cancel"
63+
64+
response = Requestor(self._client).request(
65+
method=RequestMethod.POST,
66+
url=url,
67+
beta=False,
68+
)
69+
70+
return convert_to_easypost_object(response=response)

examples

Submodule examples updated 223 files

tests/cassettes/test_claim_all.yaml

+128
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)