Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions examples/letter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import lob
lob.api_key = "test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc" # Replace this API key with your own.

# Creating an Address Object

example_address = lob.Address.create(
name = 'Julia Sanchez',
address_line1 = '104 Printing Boulevard',
address_city = 'Boston',
address_state = 'MA',
address_country = 'US',
address_zip = '12345'
)
print "\n"
print "Address Response"
print "\n"
print "======================================================="
print "\n"
print example_address
print "\n"
print "======================================================="
print "\n"

# Creating a Letter

example_letter = lob.Letter.create(
name = 'Test Letter',
to_address = {
'name': 'Antoinette Reynolds',
'address_line1': '1859 Kinney St',
'address_city': 'Agawam',
'address_zip': '01001',
'address_state': 'MA'
},
from_address = example_address,
file = '<html><p>Hello Julia</p></html>'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

)

print "Letter Response"
print "\n"
print "======================================================="
print "\n"
print example_letter
print "\n"
print "======================================================="
print "\n"
3 changes: 2 additions & 1 deletion lob/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#Resources
from lob.resource import (Address, Area, BankAccount, Check, Country, Job,
Object, Packaging, Postcard, Route, Service, Setting, State, Verification)
Letter, Object, Packaging, Postcard, Route, Service, Setting, State,
Verification)

from lob.version import VERSION
14 changes: 14 additions & 0 deletions lob/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def lob_format(resp):
'check': Check,
'country': Country,
'job': Job,
'letter' : Letter,
'object': Object,
'postcard': Postcard,
'state': State
Expand Down Expand Up @@ -170,6 +171,19 @@ def create(cls, **params):
params.pop('objects', None)
return super(Job, cls).create(**params)

class Letter(ListableAPIResource, CreateableAPIResource):
endpoint = '/letters'
@classmethod
def create(cls, **params):
if isinstance(params, dict):
if 'from_address' in params:
params['from'] = params['from_address']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason we call the vars "to_address" and "from_address" and not "to" and "from"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from is a reserved python keyword so it complains if you try to use it as a param name in the create()

source: https://docs.python.org/2.5/ref/keywords.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

booo reserved words.

params.pop('from_address')
if 'to_address' in params:
params['to'] = params['to_address']
params.pop('to_address')
return super(Letter, cls).create(**params)

class Object(ListableAPIResource, DeleteableAPIResource, CreateableAPIResource):
endpoint = '/objects'

Expand Down
34 changes: 34 additions & 0 deletions tests/test_letter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import unittest
import lob
# Setting the API key
lob.api_key = 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc'

class LetterFunctions(unittest.TestCase):
def setUp(self):
lob.api_key = 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc'
self.addr = lob.Address.list(count=1).data[0]

def test_list_letters(self):
letters = lob.Letter.list()
self.assertTrue(isinstance(letters.data[0], lob.Letter))
self.assertEqual(letters.object, 'list')

def test_retrieve_letter(self):
letter = lob.Letter.retrieve(id=lob.Letter.list().data[0].id)
self.assertTrue(isinstance(letter, lob.Letter))

def test_create_letter(self):
letter = lob.Letter.create(
from_address = {
'name': 'Antoinette Reynolds',
'address_line1': '1859 Kinney St',
'address_city': 'Agawam',
'address_zip': '01001',
'address_state': 'MA'
},
to_address = self.addr.id,
file = 'https://lob.com/goblue.pdf'
)
self.assertEqual(letter.to_address.id, self.addr.id)
self.assertTrue(isinstance(letter, lob.Letter))