Skip to content

Commit

Permalink
Merge pull request #44 from EasyPost/reports-python
Browse files Browse the repository at this point in the history
first attempt client lib python for reports
  • Loading branch information
victoryftw authored Dec 15, 2016
2 parents 0a18470 + bebed27 commit add87c7
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 3 deletions.
67 changes: 64 additions & 3 deletions easypost/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -644,9 +651,11 @@ def verify(self, carrier=None):
class ScanForm(AllResource, CreateResource):
pass


class Insurance(AllResource, CreateResource):
pass


class CustomsItem(AllResource, CreateResource):
pass

Expand Down Expand Up @@ -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):
Expand Down
23 changes: 23 additions & 0 deletions examples/report.py
Original file line number Diff line number Diff line change
@@ -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)
68 changes: 68 additions & 0 deletions tests/report.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit add87c7

Please sign in to comment.