diff --git a/easypost/__init__.py b/easypost/__init__.py index b2aca010..6f496b5f 100644 --- a/easypost/__init__.py +++ b/easypost/__init__.py @@ -77,7 +77,11 @@ def convert_to_easypost_object(response, api_key, parent=None, name=None): 'PickupRate': PickupRate, 'PostageLabel': PostageLabel, 'CarrierAccount': CarrierAccount, - 'User': User + 'User': User, + 'Report': Report, + 'ShipmentReport': Report, + 'PaymentLogReport': Report, + 'TrackerReport': Report } prefixes = { @@ -98,7 +102,10 @@ def convert_to_easypost_object(response, api_key, parent=None, name=None): 'pickuprate': PickupRate, 'pl': PostageLabel, 'ca': CarrierAccount, - 'user': User + 'user': User, + 'shprep': Report, + 'plrep': Report, + 'trkrep': Report } if isinstance(response, list): @@ -453,7 +460,7 @@ def __repr__(self): def __str__(self): return self.to_json(indent=2) - + def to_json(self, indent=None): return json.dumps(self.to_dict(), sort_keys=True, indent=indent, cls=EasyPostObjectEncoder) @@ -644,9 +651,11 @@ def verify(self, carrier=None): class ScanForm(AllResource, CreateResource): pass + class Insurance(AllResource, CreateResource): pass + class CustomsItem(AllResource, CreateResource): pass @@ -903,6 +912,58 @@ def api_keys(self): return my_api_keys +class Report(AllResource, CreateResource): + + REPORT_TYPES = {'shprep': 'shipment', 'plrep': 'payment_log', 'trkrep': 'tracker'} + + @classmethod + def create(cls, api_key=None, **params): + requestor = Requestor(api_key) + url = cls.class_url() + wrapped_params = {cls.class_name(): params} + + if str(params['type']) in cls.REPORT_TYPES.values(): + url += "/%s" % params['type'] + else: + raise Exception("Undertermined Report Type") + + response, api_key = requestor.request('post', url, wrapped_params, False) + return convert_to_easypost_object(response, api_key) + + @classmethod + def retrieve(cls, easypost_id="", api_key=None, **params): + try: + easypost_id = easypost_id['id'] + except (KeyError, TypeError): + pass + + url = cls.class_url() + + obj_id = easypost_id.split("_")[0] + + if obj_id in cls.REPORT_TYPES: + url += "/%s/%s" % (cls.REPORT_TYPES[obj_id], easypost_id) + else: + raise Exception("Undetermined Report Type") + + requestor = Requestor(api_key) + response, api_key = requestor.request('get', url) + return convert_to_easypost_object(response, api_key) + + @classmethod + def all(cls, api_key=None, **params): + requestor = Requestor(api_key) + url = cls.class_url() + + if str(params['type']) in cls.REPORT_TYPES.values(): + url += "/%s" % params['type'] + else: + raise Exception("Undertemined Report Type") + + response, api_key = requestor.request('get', url, params) + return convert_to_easypost_object(response, api_key) + + class Blob(AllResource, CreateResource): @classmethod def retrieve(cls, easypost_id, api_key=None, **params): diff --git a/examples/report.py b/examples/report.py new file mode 100644 index 00000000..1c60ee2c --- /dev/null +++ b/examples/report.py @@ -0,0 +1,23 @@ +import easypost +easypost.api_key = 'cueqNZUb3ldeWTNX7MU3Mel8UXtaAMUi' + +report = easypost.Report.create( + start_date="2012-12-01", + end_date="2013-01-01", + type="shipment" +) +print(report.id) + +report1 = easypost.Report.retrieve(report.id) + +print(report1.id) + +report2 = easypost.Report.create( + start_date="2013-12-02", + end_date="2014-01-01", + type="shipment" +) + +reports3 = easypost.Report.all(type="shipment") + +print(reports3) diff --git a/tests/report.py b/tests/report.py new file mode 100644 index 00000000..7ab59487 --- /dev/null +++ b/tests/report.py @@ -0,0 +1,68 @@ +import unittest +import easypost +from constants import API_KEY as api_key + +easypost.api_key = api_key + + +class ReportTests(unittest.TestCase): + + def test_shipment_report(self): + report = easypost.Report.create( + start_date="2012-12-01", + end_date="2013-01-01", + type="shipment" + ) + + assert report.object == "ShipmentReport" + assert report.status == "available" + assert report.__class__ == easypost.Report + + report2 = easypost.Report.retrieve(report.id, api_key=api_key) + + assert report2.__class__ == easypost.Report + assert report2.id == report.id + + reports = easypost.Report.all(api_key=api_key, type="shipment") + assert len(reports["reports"]) + assert reports["reports"][0].id == report.id == report2.id + + def test_payment_log_report(self): + report = easypost.Report.create( + start_date="2012-12-01", + end_date="2013-01-01", + type="payment_log" + ) + + assert report.object == "PaymentLogReport" + assert report.status == "available" + assert report.__class__ == easypost.Report + + report2 = easypost.Report.retrieve(report.id, api_key=api_key) + + assert report2.__class__ == easypost.Report + assert report2.id == report.id + + reports = easypost.Report.all(api_key=api_key, type="payment_log") + assert len(reports["reports"]) + assert reports["reports"][0].id == report.id == report2.id + + def test_tracker_report(self): + report = easypost.Report.create( + start_date="2012-12-01", + end_date="2013-01-01", + type="tracker" + ) + + assert report.object == "TrackerReport" + # assert report.status == "available" # Not yet implemented for tracker reports + assert report.__class__ == easypost.Report + + report2 = easypost.Report.retrieve(report.id, api_key=api_key) + + assert report2.__class__ == easypost.Report + assert report2.id == report.id + + reports = easypost.Report.all(api_key=api_key, type="tracker") + assert len(reports["reports"]) + assert reports["reports"][0].id == report.id == report2.id