Skip to content

Commit 70b9947

Browse files
author
Shariq Torres
authored
Merge pull request #182 from lob/add-api-key-to-methods
added api_key param to requests
2 parents 99c3465 + e214442 commit 70b9947

File tree

4 files changed

+133
-15
lines changed

4 files changed

+133
-15
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ name = "pypi"
55

66
[packages]
77
requests = "*"
8+
requests-mock = "*"
89

910
[dev-packages]
1011
"flake8" = "*"

Pipfile.lock

Lines changed: 20 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lob/resource.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,16 @@ def __str__(self):
8787

8888
class APIResource(LobObject):
8989
@classmethod
90-
def retrieve(cls, id, timeout=TIMEOUT_DEFAULT, **params):
91-
requestor = api_requestor.APIRequestor()
90+
def retrieve(cls, id, timeout=TIMEOUT_DEFAULT, api_key=None, **params):
91+
requestor = api_requestor.APIRequestor(api_key)
9292
response = requestor.request('get', '%s/%s' % (cls.endpoint, id), params, timeout=timeout)
9393
return lob_format(response)
9494

9595

9696
# API Operations
9797
class ListableAPIResource(APIResource):
9898
@classmethod
99-
def list(cls, timeout=TIMEOUT_DEFAULT, **params):
99+
def list(cls, timeout=TIMEOUT_DEFAULT, api_key=None, **params):
100100
for key, value in params.copy().items():
101101
if isinstance(params[key], dict):
102102
for subKey in value:
@@ -105,31 +105,31 @@ def list(cls, timeout=TIMEOUT_DEFAULT, **params):
105105
elif isinstance(params[key], list):
106106
params[str(key) + '[]'] = params[key]
107107
del params[key]
108-
requestor = api_requestor.APIRequestor()
108+
requestor = api_requestor.APIRequestor(api_key)
109109
response = requestor.request('get', cls.endpoint, params, timeout=timeout)
110110
return lob_format(response)
111111

112112

113113
class DeleteableAPIResource(APIResource):
114114
@classmethod
115-
def delete(cls, id, timeout=TIMEOUT_DEFAULT):
116-
requestor = api_requestor.APIRequestor()
115+
def delete(cls, id, timeout=TIMEOUT_DEFAULT, api_key=None):
116+
requestor = api_requestor.APIRequestor(api_key)
117117
response = requestor.request('delete', '%s/%s' % (cls.endpoint, id), timeout=timeout)
118118
return lob_format(response)
119119

120120

121121
class CreateableAPIResource(APIResource):
122122
@classmethod
123-
def create(cls, timeout=TIMEOUT_DEFAULT, **params):
124-
requestor = api_requestor.APIRequestor()
123+
def create(cls, timeout=TIMEOUT_DEFAULT, api_key=None, **params):
124+
requestor = api_requestor.APIRequestor(api_key)
125125
response = requestor.request('post', cls.endpoint, params, timeout=timeout)
126126
return lob_format(response)
127127

128128

129129
class VerifiableAPIResource(APIResource):
130130
@classmethod
131-
def verify(cls, id, timeout=TIMEOUT_DEFAULT, **params):
132-
requestor = api_requestor.APIRequestor()
131+
def verify(cls, id, timeout=TIMEOUT_DEFAULT, api_key=None, **params):
132+
requestor = api_requestor.APIRequestor(api_key)
133133
response = requestor.request('post', '%s/%s/verify' % (cls.endpoint, id), params, timeout=timeout)
134134
return lob_format(response)
135135

tests/test_lob.py

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
import lob
3-
3+
import requests_mock
4+
import base64
45

56
class TestLob(unittest.TestCase):
67
def setUp(self):
@@ -13,5 +14,105 @@ def test_bad_auth(self):
1314
def test_set_version(self):
1415
self.assertEqual(lob.api_version, 'apiVersion')
1516

17+
def parse_headers(self, headers):
18+
auth = headers['Authorization'].split(" ")
19+
return base64.b64decode(auth[1])
20+
21+
def return_list(self, request, context):
22+
return {
23+
"data": [
24+
{
25+
"id": "adr_e68217bd744d65c8",
26+
"description": "Harry - Office",
27+
"name": "HARRY ZHANG",
28+
"company": "LOB",
29+
"phone": "5555555555",
30+
"email": "[email protected]",
31+
"address_line1": "210 KING ST STE 6100",
32+
"address_line2": None,
33+
"address_city": "SAN FRANCISCO",
34+
"address_state": "CA",
35+
"address_zip": "94107-1741",
36+
"address_country": "UNITED STATES",
37+
"metadata": {},
38+
"date_created": "2019-08-12T00:16:00.361Z",
39+
"date_modified": "2019-08-12T00:16:00.361Z",
40+
"object": "address"
41+
},
42+
{
43+
"id": "adr_asdi2y3riuasasoi",
44+
"description": "Harry - Office",
45+
"name": "Harry Zhang",
46+
"company": "Lob",
47+
"phone": "5555555555",
48+
"email": "[email protected]",
49+
"metadata": {},
50+
"address_line1": "370 WATER ST",
51+
"address_line2": "",
52+
"address_city": "SUMMERSIDE",
53+
"address_state": "PRINCE EDWARD ISLAND",
54+
"address_zip": "C1N 1C4",
55+
"address_country": "CANADA",
56+
"date_created": "2019-09-20T00:14:00.361Z",
57+
"date_modified": "2019-09-20T00:14:00.361Z",
58+
"object": "address"
59+
}
60+
],
61+
"object": "list",
62+
"next_url": "https://api.lob.com/v1/addresses?limit=2&after=eyJkYXRlT2Zmc2V0IjoiMjAxOS0wOC0wN1QyMTo1OTo0Ni43NjRaIiwiaWRPZmZzZXQiOiJhZHJfODMwYmYwZWFiZGFhYTQwOSJ9",
63+
"previous_url": None,
64+
"count": 2
65+
}
66+
67+
def return_status(self, request, context):
68+
return {
69+
"id": "adr_123456789",
70+
"deleted": True
71+
}
72+
73+
def return_single(self, request, context):
74+
return {
75+
"id": "adr_d3489cd64c791ab5",
76+
"description": "Harry - Office",
77+
"name": "HARRY ZHANG",
78+
"company": "LOB",
79+
"phone": "5555555555",
80+
"email": "[email protected]",
81+
"address_line1": "210 KING ST STE 6100",
82+
"address_city": "SAN FRANCISCO",
83+
"address_state": "CA",
84+
"address_zip": "94107",
85+
"address_country": "UNITED STATES",
86+
"metadata": {},
87+
"date_created": "2017-09-05T17:47:53.767Z",
88+
"date_modified": "2017-09-05T17:47:53.767Z",
89+
"object": "address"
90+
}
91+
92+
@requests_mock.Mocker()
93+
def test_different_api_keys_per_call(self, adapter):
94+
adapter.register_uri('GET', 'https://api.lob.com/v1/addresses', json=self.return_list)
95+
adapter.register_uri('POST', 'https://api.lob.com/v1/addresses', json=self.return_single)
96+
adapter.register_uri('GET', 'https://api.lob.com/v1/addresses/adr_12345', json=self.return_single)
97+
adapter.register_uri('DELETE', 'https://api.lob.com/v1/addresses/adr_12345', json=self.return_status)
98+
99+
lob.Address.list(api_key='key12345')
100+
self.assertEqual(bytes('key12345:', 'utf8'), self.parse_headers(adapter.last_request.headers))
101+
lob.Address.create(
102+
name='Lob',
103+
address_line1='185 Berry Street',
104+
address_line2='Suite 1510',
105+
address_city='San Francisco',
106+
address_zip='94017',
107+
address_state='CA',
108+
address_country='US',
109+
api_key='key98765'
110+
)
111+
self.assertEqual(bytes('key98765:', 'utf8'), self.parse_headers(adapter.last_request.headers))
112+
lob.Address.retrieve(id='adr_12345', api_key='key9999')
113+
self.assertEqual(bytes('key9999:', 'utf8'), self.parse_headers(adapter.last_request.headers))
114+
lob.Address.delete(id='adr_12345', api_key='key8080')
115+
self.assertEqual(bytes('key8080:', 'utf8'), self.parse_headers(adapter.last_request.headers))
116+
16117
def tearDown(self):
17118
del lob.api_version

0 commit comments

Comments
 (0)