diff --git a/README.md b/README.md index aa5423b..8dbfa02 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,59 @@ This will output: "message": "Default address: The address you entered was found but more information is needed (such as an apartment, suite, or box number) to match to a specific address." } ``` + +## Routes + +```python +#Retrieve routes for given zip codes +lob.Route.list(zip_codes=[94158,60031]) +``` + +## Areas + +```python +#List areas +lob.Area.list() + +#List Areas with Count and Offset +lob.Area.list(count=5, offset=2) + +# You can query an area with its `ID` +lob.Area.retrieve(id=) + +#or another way +lob.Area.retrieve() + +#Basic Area Create with Zip Codes +lob.Area.create( + name='Lob', + front = 'https://www.lob.com/areafront.pdf', + back = 'https://www.lob.com/areaback.pdf', + routes = ['94158','60031'], + target_type = 'all', + full_bleed = '1' +) + +#Basic Area Create with Routes Object +var routes = lob.Route.list(zip_codes=[94158,60031]) +lob.Area.create( + name='Lob', + front = 'https://www.lob.com/areafront.pdf', + back = 'https://www.lob.com/areaback.pdf', + routes = routes, + target_type = 'all', + full_bleed = '1' +) + + +#Create Area with Optional Parameters +lob.Area.create( + front = 'https://www.lob.com/areafront.pdf', + back = 'https://www.lob.com/areaback.pdf', + routes = '94158', +) +``` + ## Settings ```python @@ -372,10 +425,10 @@ lob.Postcard.create( lob.BankAccount.list() # Can specify count and offset as well -lob.BankAccount.list(count=5, offset=1) +lob.BankAccount.list(count=5, offset=1) #Retrieve a specific BankAccount by id -lob.BankAccount.retrieve() +lob.BankAccount.retrieve() #Create Bank Account Using Address Ids lob.BankAccount.create( @@ -409,7 +462,7 @@ lob.BankAccount.create( ) #Delete a specific BankAccount by id -lob.BankAccount.delete() +lob.BankAccount.delete() ``` ## Checks @@ -419,12 +472,12 @@ lob.BankAccount.delete() lob.Check.list() # Can specify count and offset as well -lob.Check.list(count=5, offset=1) +lob.Check.list(count=5, offset=1) #Retrieve a specific Check by id lob.Check.retrieve() -#Create Check with Address Id +#Create Check with Address Id lob.Check.create( name = 'Check Test', to_address = , diff --git a/lob/__init__.py b/lob/__init__.py index 436b09b..30afe66 100644 --- a/lob/__init__.py +++ b/lob/__init__.py @@ -2,7 +2,7 @@ api_base = 'https://api.lob.com/v1' #Resources -from lob.resource import (Address, BankAccount, Check, Country, Job, - Object, Packaging, Postcard, Service, Setting, State, Verification) +from lob.resource import (Address, Area, BankAccount, Check, Country, Job, + Object, Packaging, Postcard, Route, Service, Setting, State, Verification) from lob.version import VERSION diff --git a/lob/resource.py b/lob/resource.py index e041c88..7a234b6 100644 --- a/lob/resource.py +++ b/lob/resource.py @@ -5,6 +5,7 @@ def lob_format(resp): types = { 'address': Address, + 'area': Area, 'bank_account': BankAccount, 'check': Check, 'job': Job, @@ -111,6 +112,19 @@ def create(cls, **params): class Address(ListableAPIResource, DeleteableAPIResource, CreateableAPIResource): url = '/addresses' +class Area(ListableAPIResource, CreateableAPIResource): + url = '/areas' + @classmethod + def create(cls, **params): + if isinstance(params, dict): + if 'routes' in params: + if isinstance(params['routes'], LobObject): + routes = [] + for r in params['routes'].data[0]['routes']: + routes.append(params['routes'].data[0]['zip_code'] + '-' + r["route"]) + params['routes'] = routes + return super(Area, cls).create(**params) + class BankAccount(ListableAPIResource, DeleteableAPIResource, CreateableAPIResource): url = '/bank_accounts' @@ -167,6 +181,9 @@ def create(cls, **params): class Packaging(ListableAPIResource): url='/packagings' +class Route(ListableAPIResource): + url='/routes' + class Service(ListableAPIResource): url = '/services' diff --git a/tests/areaback.pdf b/tests/areaback.pdf new file mode 100644 index 0000000..3f5db3c Binary files /dev/null and b/tests/areaback.pdf differ diff --git a/tests/areafront.pdf b/tests/areafront.pdf new file mode 100644 index 0000000..5695e80 Binary files /dev/null and b/tests/areafront.pdf differ diff --git a/tests/test_area.py b/tests/test_area.py new file mode 100644 index 0000000..e69661b --- /dev/null +++ b/tests/test_area.py @@ -0,0 +1,67 @@ +import unittest +import lob +# Setting the API key +lob.api_key = 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc' + +class AreaFunctions(unittest.TestCase): + def setUp(self): + lob.api_key = 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc' + self.route = lob.Route.list(zip_codes=94158) + + def test_create_area_with_zip(self): + area = lob.Area.create( + name = 'area_test_zip', + front = 'https://www.lob.com/areafront.pdf', + back = 'https://www.lob.com/areaback.pdf', + routes = ['94158','60031'], + target_type = 'all', + full_bleed = '1' + ) + self.assertTrue(isinstance(area, lob.Area)) + + + def test_create_area_with_route(self): + area = lob.Area.create( + name = 'area_test_route', + front = 'https://www.lob.com/areafront.pdf', + back = 'https://www.lob.com/areaback.pdf', + routes = self.route, + target_type = 'all', + full_bleed = '1' + ) + self.assertTrue(isinstance(area, lob.Area)) + + + def test_create_area_local_file(self): + area = lob.Area.create( + name = 'area_local_file', + front = open('tests/areafront.pdf', 'rb'), + back = open('tests/areaback.pdf', 'rb'), + routes = self.route, + target_type = 'all', + full_bleed = '1' + ) + self.assertTrue(isinstance(area, lob.Area)) + + def test_create_area_fail(self): + self.assertRaises(lob.error.InvalidRequestError, lob.Area.create) + + def test_list_areas(self): + areas = lob.Area.list() + self.assertTrue(isinstance(areas.data[0], lob.Area)) + self.assertEqual(areas.object, 'list') + + def test_list_areas_limit(self): + areas = lob.Area.list(count=2) + self.assertTrue(isinstance(areas.data[0], lob.Area)) + self.assertEqual(len(areas.data), 2) + + def test_list_area_fail(self): + self.assertRaises(lob.error.InvalidRequestError, lob.Area.list, count=1000) + + def test_retrieve_area(self): + area = lob.Area.retrieve(id=lob.Area.list().data[0].id) + self.assertTrue(isinstance(area, lob.Area)) + + def test_retrieve_area_fail(self): + self.assertRaises(lob.error.InvalidRequestError, lob.Area.retrieve, id='test') diff --git a/tests/test_job.py b/tests/test_job.py index b65bf6f..0b1dc9c 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -69,8 +69,8 @@ def test_create_job_inline(self): 'setting_id': '201' } ) - self.assertEqual(job.to_address.name, 'LOB1') - self.assertEqual(job.from_address.name, 'LOB2') + self.assertEqual(job.to_address.name, 'Lob1') + self.assertEqual(job.from_address.name, 'Lob2') self.assertEqual(job.objects[0].name, 'Object1') self.assertTrue(isinstance(job, lob.Job)) @@ -78,12 +78,18 @@ def test_create_job_multi_object(self): job = lob.Job.create( to_address = self.addr.id, from_address = self.addr.id, - objects = [self.obj.id, self.obj.id] + objects = [ + { + 'name': 'Test Job', + 'file': open('tests/pc.pdf', 'rb'), + 'setting_id': 201, + 'quantity': 2 + } + ] ) self.assertEqual(job.to_address.id, self.addr.id) self.assertEqual(job.from_address.id, self.addr.id) - self.assertEqual(job.objects[0].id, self.obj.id) - self.assertEqual(job.objects[1].id, self.obj.id) + self.assertEqual(job.objects[0].quantity, 2) self.assertTrue(isinstance(job, lob.Job)) def test_create_job_local_file(self): diff --git a/tests/test_route.py b/tests/test_route.py new file mode 100644 index 0000000..f2d06c3 --- /dev/null +++ b/tests/test_route.py @@ -0,0 +1,11 @@ +import unittest +import lob +# Setting the API key +lob.api_key = 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc' + +class RouteTest(unittest.TestCase): + def setUp(self): + lob.api_key = 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc' + + def test_route_find(self): + print lob.Route.list(zip_codes=[94158,60031])