From 142ee1076339c2d4394b9f489de26ae847a94f3a Mon Sep 17 00:00:00 2001 From: Nruthya Date: Mon, 5 Dec 2016 03:39:51 -0800 Subject: [PATCH 1/6] first attempt client lib python for reports --- easypost/__init__.py | 60 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/easypost/__init__.py b/easypost/__init__.py index b2aca010..f41b9ff8 100644 --- a/easypost/__init__.py +++ b/easypost/__init__.py @@ -77,7 +77,10 @@ def convert_to_easypost_object(response, api_key, parent=None, name=None): 'PickupRate': PickupRate, 'PostageLabel': PostageLabel, 'CarrierAccount': CarrierAccount, - 'User': User + 'User': User, + 'ShipmentReport' : ShipmentReport, + 'PaymentLogReport' : PaymentLogReport, + 'TrackerReport' : TrackerReport } prefixes = { @@ -98,7 +101,10 @@ def convert_to_easypost_object(response, api_key, parent=None, name=None): 'pickuprate': PickupRate, 'pl': PostageLabel, 'ca': CarrierAccount, - 'user': User + 'user': User, + 'shrep' : ShipmentReport, + 'plrep' : PaymentLogReport, + 'trkrep' : TrackerReport } if isinstance(response, list): @@ -902,6 +908,56 @@ def api_keys(self): return my_api_keys +class Report(AllResource, CreateResource): + @classmethod + def create(cls, api_key=None, **params): + requestor = Requestor(api_key) + url = cls.class_url() + wrapped_params = {cls.class_name(): params} + report_types = ['shipment', 'payment_log', 'tracker'] + + if str(params['type']) in report_types: + 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() + report_types = { 'shprep': 'shipment', 'plrep': 'payment_log', 'trkrep': 'tracker' } + obj_id = params['public_id'].split("_")[0] + + if obj_id in report_types : + url += "/%s/%s" % (report_types[obj_id], params['public_id']) + else: + raise Exception("Undetermined Report Type") + + if easypost_id == "": + requestor = Requestor(api_key) + response, api_key = requestor.request('get', url) + return convert_to_easypost_object(response, api_key) + else: + instance = cls(easypost_id, api_key, **params) + instance.refresh() + return instance + +class ShipmentReport(Report): + pass + +class PaymentLogReport(Report): + pass + +class TrackerReport(Report): + pass + class Blob(AllResource, CreateResource): @classmethod From 4297bd44a2a247cf8127039f4be246e407477316 Mon Sep 17 00:00:00 2001 From: Nruthya Date: Mon, 5 Dec 2016 05:03:44 -0800 Subject: [PATCH 2/6] add all method for reports --- easypost/__init__.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/easypost/__init__.py b/easypost/__init__.py index f41b9ff8..920f2555 100644 --- a/easypost/__init__.py +++ b/easypost/__init__.py @@ -917,7 +917,7 @@ def create(cls, api_key=None, **params): report_types = ['shipment', 'payment_log', 'tracker'] if str(params['type']) in report_types: - url += "/%s" % params[:type] + url += "/%s" % params['type'] else: raise Exception("Undertermined Report Type") @@ -949,6 +949,20 @@ def retrieve(cls, easypost_id="", api_key=None, **params): instance.refresh() return instance + @classmethod + def all(cls, api_key=None, **params): + requestor = Requestor(api_key) + url = cls.class_url() + report_types = ['shipment', 'payment_log', 'tracker'] + + if str(params['type']) in report_types: + 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 ShipmentReport(Report): pass From 8cf793268313239358cd9db1ecb8c219e535e3c7 Mon Sep 17 00:00:00 2001 From: Nruthya Date: Wed, 7 Dec 2016 02:30:13 -0800 Subject: [PATCH 3/6] refactor code and add tests --- easypost/__init__.py | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/easypost/__init__.py b/easypost/__init__.py index 920f2555..ecf31059 100644 --- a/easypost/__init__.py +++ b/easypost/__init__.py @@ -78,9 +78,7 @@ def convert_to_easypost_object(response, api_key, parent=None, name=None): 'PostageLabel': PostageLabel, 'CarrierAccount': CarrierAccount, 'User': User, - 'ShipmentReport' : ShipmentReport, - 'PaymentLogReport' : PaymentLogReport, - 'TrackerReport' : TrackerReport + 'Report': Report, } prefixes = { @@ -101,10 +99,7 @@ def convert_to_easypost_object(response, api_key, parent=None, name=None): 'pickuprate': PickupRate, 'pl': PostageLabel, 'ca': CarrierAccount, - 'user': User, - 'shrep' : ShipmentReport, - 'plrep' : PaymentLogReport, - 'trkrep' : TrackerReport + 'user': User } if isinstance(response, list): @@ -459,7 +454,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) @@ -909,6 +904,8 @@ 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) @@ -916,7 +913,7 @@ def create(cls, api_key=None, **params): wrapped_params = {cls.class_name(): params} report_types = ['shipment', 'payment_log', 'tracker'] - if str(params['type']) in report_types: + if str(params['type']) in report_types.values(): url += "/%s" % params['type'] else: raise Exception("Undertermined Report Type") @@ -932,7 +929,7 @@ def retrieve(cls, easypost_id="", api_key=None, **params): pass url = cls.class_url() - report_types = { 'shprep': 'shipment', 'plrep': 'payment_log', 'trkrep': 'tracker' } + obj_id = params['public_id'].split("_")[0] if obj_id in report_types : @@ -963,15 +960,6 @@ def all(cls, api_key=None, **params): response, api_key = requestor.request('get', url, params) return convert_to_easypost_object(response, api_key) -class ShipmentReport(Report): - pass - -class PaymentLogReport(Report): - pass - -class TrackerReport(Report): - pass - class Blob(AllResource, CreateResource): @classmethod From 60aad4999387cac4370f0ef3f1668f6aa1dd7b47 Mon Sep 17 00:00:00 2001 From: Nruthya Date: Tue, 13 Dec 2016 07:27:00 -0800 Subject: [PATCH 4/6] add tests --- examples/report.py | 23 +++++++++++++++++++++++ tests/report.py | 18 ++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 examples/report.py create mode 100644 tests/report.py diff --git a/examples/report.py b/examples/report.py new file mode 100644 index 00000000..5ed41966 --- /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" +) + +report3 = easypost.Report.all(type="shipment") + +print(report3) diff --git a/tests/report.py b/tests/report.py new file mode 100644 index 00000000..8a0fbb85 --- /dev/null +++ b/tests/report.py @@ -0,0 +1,18 @@ +import unittest +import easypost +from constants import API_KEY as api_key + +easypost.api_key = api_key + +class ReportTests(unittest.TestCase): + + def test_report_creation(self): + report = easypost.Parcel.create( + start_date="2012-12-01", + end_date= "2013-01-01", + type="shipment" + ) + + assert parcel.object == "ShipmentReport" + assert parcel.status == "available" + From 34e75e5b85bcad6d8af68e26fb292813c795f7a6 Mon Sep 17 00:00:00 2001 From: Nruthya Date: Wed, 14 Dec 2016 20:05:58 -0800 Subject: [PATCH 5/6] add final changes for review comments by jake --- easypost/__init__.py | 26 ++++++++++---------------- examples/report.py | 4 ++-- tests/report.py | 16 +++++++++++----- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/easypost/__init__.py b/easypost/__init__.py index ecf31059..511f7e59 100644 --- a/easypost/__init__.py +++ b/easypost/__init__.py @@ -904,16 +904,16 @@ def api_keys(self): return my_api_keys class Report(AllResource, CreateResource): - report_types = { 'shprep': 'shipment', 'plrep': 'payment_log', 'trkrep': 'tracker' } + + 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} - report_types = ['shipment', 'payment_log', 'tracker'] - if str(params['type']) in report_types.values(): + if str(params['type']) in cls.REPORT_TYPES.values(): url += "/%s" % params['type'] else: raise Exception("Undertermined Report Type") @@ -930,29 +930,23 @@ def retrieve(cls, easypost_id="", api_key=None, **params): url = cls.class_url() - obj_id = params['public_id'].split("_")[0] + obj_id = easypost_id.split("_")[0] - if obj_id in report_types : - url += "/%s/%s" % (report_types[obj_id], params['public_id']) + if obj_id in cls.REPORT_TYPES: + url += "/%s/%s" % (cls.REPORT_TYPES[obj_id], easypost_id) else: raise Exception("Undetermined Report Type") - if easypost_id == "": - requestor = Requestor(api_key) - response, api_key = requestor.request('get', url) - return convert_to_easypost_object(response, api_key) - else: - instance = cls(easypost_id, api_key, **params) - instance.refresh() - return instance + 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() - report_types = ['shipment', 'payment_log', 'tracker'] - if str(params['type']) in report_types: + if str(params['type']) in cls.REPORT_TYPES.values(): url += "/%s" % params['type'] else: raise Exception("Undertemined Report Type") diff --git a/examples/report.py b/examples/report.py index 5ed41966..aa2c33de 100644 --- a/examples/report.py +++ b/examples/report.py @@ -18,6 +18,6 @@ type="shipment" ) -report3 = easypost.Report.all(type="shipment") +reports3 = easypost.Report.all(type="shipment") -print(report3) +print(reports3) diff --git a/tests/report.py b/tests/report.py index 8a0fbb85..bc47902f 100644 --- a/tests/report.py +++ b/tests/report.py @@ -6,13 +6,19 @@ class ReportTests(unittest.TestCase): - def test_report_creation(self): - report = easypost.Parcel.create( + def test_report(self): + report = easypost.Report.create( start_date="2012-12-01", - end_date= "2013-01-01", + end_date="2013-01-01", type="shipment" ) - assert parcel.object == "ShipmentReport" - assert parcel.status == "available" + assert report.object == "ShipmentReport" + assert report.status == "available" + report2 = easypost.Report.retrieve(report.id, api_key=api_key) + 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 From bebed2714a5477108d28930cf87299793f76b640 Mon Sep 17 00:00:00 2001 From: Jake Epstein Date: Thu, 15 Dec 2016 14:37:20 -0800 Subject: [PATCH 6/6] fixed Report classing issue and resolved some linter problems --- easypost/__init__.py | 13 ++++++-- examples/report.py | 4 +-- tests/report.py | 70 ++++++++++++++++++++++++++++++++++++-------- 3 files changed, 70 insertions(+), 17 deletions(-) diff --git a/easypost/__init__.py b/easypost/__init__.py index 511f7e59..6f496b5f 100644 --- a/easypost/__init__.py +++ b/easypost/__init__.py @@ -79,6 +79,9 @@ def convert_to_easypost_object(response, api_key, parent=None, name=None): 'CarrierAccount': CarrierAccount, 'User': User, 'Report': Report, + 'ShipmentReport': Report, + 'PaymentLogReport': Report, + 'TrackerReport': Report } prefixes = { @@ -99,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): @@ -645,9 +651,11 @@ def verify(self, carrier=None): class ScanForm(AllResource, CreateResource): pass + class Insurance(AllResource, CreateResource): pass + class CustomsItem(AllResource, CreateResource): pass @@ -903,9 +911,10 @@ def api_keys(self): return my_api_keys + class Report(AllResource, CreateResource): - REPORT_TYPES = { 'shprep': 'shipment', 'plrep': 'payment_log', 'trkrep': 'tracker' } + REPORT_TYPES = {'shprep': 'shipment', 'plrep': 'payment_log', 'trkrep': 'tracker'} @classmethod def create(cls, api_key=None, **params): diff --git a/examples/report.py b/examples/report.py index aa2c33de..1c60ee2c 100644 --- a/examples/report.py +++ b/examples/report.py @@ -3,7 +3,7 @@ report = easypost.Report.create( start_date="2012-12-01", - end_date= "2013-01-01", + end_date="2013-01-01", type="shipment" ) print(report.id) @@ -14,7 +14,7 @@ report2 = easypost.Report.create( start_date="2013-12-02", - end_date= "2014-01-01", + end_date="2014-01-01", type="shipment" ) diff --git a/tests/report.py b/tests/report.py index bc47902f..7ab59487 100644 --- a/tests/report.py +++ b/tests/report.py @@ -4,21 +4,65 @@ easypost.api_key = api_key + class ReportTests(unittest.TestCase): - def test_report(self): - report = easypost.Report.create( - start_date="2012-12-01", - end_date="2013-01-01", - type="shipment" - ) + 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 - assert report.object == "ShipmentReport" - assert report.status == "available" + report2 = easypost.Report.retrieve(report.id, api_key=api_key) - report2 = easypost.Report.retrieve(report.id, api_key=api_key) - assert report2.id == report.id + 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 + reports = easypost.Report.all(api_key=api_key, type="tracker") + assert len(reports["reports"]) + assert reports["reports"][0].id == report.id == report2.id