Skip to content

Commit 9600f6a

Browse files
author
Tyler Nappy
authored
Merge pull request #25 from button/ty/DLC-4403
Ty/dlc 4403
2 parents 73cc25e + 95d52a1 commit 9600f6a

File tree

6 files changed

+163
-1
lines changed

6 files changed

+163
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,6 @@ ENV/
8787

8888
# Rope project settings
8989
.ropeproject
90+
91+
# DS_Store
92+
**/.DS_Store

README.rst

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ We currently expose the following resources to manage:
9292
* `Accounts`_
9393
* `Merchants`_
9494
* `Orders`_
95+
* `Customers`_
9596

9697
Accounts
9798
~~~~~~~~
@@ -173,7 +174,7 @@ Create
173174
from pybutton import Client
174175
175176
client = Client('sk-XXX')
176-
177+
177178
hashed_email = hashlib.sha256('user@example.com'.lower().strip()).hexdigest()
178179
179180
response = client.orders.create({
@@ -235,6 +236,43 @@ Delete
235236
print(response)
236237
# <class pybutton.Response >
237238
239+
Customers
240+
~~~~~~~~~
241+
242+
Create
243+
''''''
244+
245+
.. code:: python
246+
247+
import hashlib
248+
from pybutton import Client
249+
250+
client = Client('sk-XXX')
251+
252+
hashed_email = hashlib.sha256('user@example.com'.lower().strip()).hexdigest()
253+
254+
response = client.customers.create({
255+
'id': 'customer-1234',
256+
'email_sha256': hashed_email,
257+
})
258+
259+
print(response)
260+
# <class pybutton.Response id: customer-1234, ...>
261+
262+
Get
263+
'''
264+
265+
.. code:: python
266+
267+
from pybutton import Client
268+
269+
client = Client('sk-XXX')
270+
271+
response = client.customers.get('customer-1234')
272+
273+
print(response)
274+
# <class pybutton.Response id: customer-1234, ...>
275+
238276
Response
239277
--------
240278

pybutton/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import unicode_literals
55

66
from pybutton.resources import Accounts
7+
from pybutton.resources import Customers
78
from pybutton.resources import Merchants
89
from pybutton.resources import Orders
910
from pybutton.error import ButtonClientError
@@ -53,6 +54,7 @@ def __init__(self, api_key, config=None):
5354
self.orders = Orders(api_key, config)
5455
self.accounts = Accounts(api_key, config)
5556
self.merchants = Merchants(api_key, config)
57+
self.customers = Customers(api_key, config)
5658

5759

5860
def config_with_defaults(config):

pybutton/resources/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
from __future__ import unicode_literals
55

66
from pybutton.resources.accounts import Accounts
7+
from pybutton.resources.customers import Customers
78
from pybutton.resources.merchants import Merchants
89
from pybutton.resources.orders import Orders
910

1011
__all__ = [
1112
Accounts,
13+
Customers,
1214
Merchants,
1315
Orders
1416
]

pybutton/resources/customers.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
from __future__ import unicode_literals
5+
6+
from pybutton.resources.resource import Resource
7+
8+
9+
class Customers(Resource):
10+
'''Manages interacting with Button Customers via the Button API
11+
12+
See Resource for class docstring.
13+
14+
'''
15+
16+
def _path(self, customer_id=None):
17+
'''Format a url path
18+
19+
Args:
20+
customer_id (str) optional: A Button customer id ('customer-XXX')
21+
22+
Returns:
23+
(str): The formatted path
24+
25+
'''
26+
27+
if customer_id:
28+
return '/v1/customers/{0}'.format(customer_id)
29+
else:
30+
return '/v1/customers'
31+
32+
def get(self, customer_id):
33+
'''Get a customer
34+
35+
Args:
36+
customer_id (str) : A Button customer id ('customer-XXX')
37+
38+
Raises:
39+
pybutton.ButtonClientError
40+
41+
Returns:
42+
(pybutton.Response) The API response
43+
44+
'''
45+
46+
return self.api_get(self._path(customer_id))
47+
48+
def create(self, customer):
49+
'''Create an customer
50+
51+
Args:
52+
customer (dict): A dict representing the attributes of an customer
53+
54+
Raises:
55+
pybutton.ButtonClientError
56+
57+
Returns:
58+
(pybutton.Response) The API response
59+
60+
'''
61+
62+
return self.api_post(self._path(), customer)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
from __future__ import unicode_literals
5+
6+
from unittest import TestCase
7+
from mock import Mock
8+
from mock import patch
9+
10+
from pybutton.resources import Customers
11+
12+
config = {
13+
'hostname': 'api.usebutton.com',
14+
'secure': True,
15+
'port': 443,
16+
'timeout': None,
17+
}
18+
19+
20+
class CustomersTestCase(TestCase):
21+
22+
def test_path(self):
23+
customer = Customers('sk-XXX', config)
24+
self.assertEqual(customer._path(), '/v1/customers')
25+
self.assertEqual(
26+
customer._path('customer-1'),
27+
'/v1/customers/customer-1'
28+
)
29+
30+
def test_get(self):
31+
customer = Customers('sk-XXX', config)
32+
customer_response = {'a': 1}
33+
34+
api_get = Mock()
35+
api_get.return_value = customer_response
36+
37+
with patch.object(customer, 'api_get', api_get):
38+
response = customer.get('customer-XXX')
39+
40+
self.assertEqual(response, customer_response)
41+
api_get.assert_called_with('/v1/customers/customer-XXX')
42+
43+
def test_create(self):
44+
customer = Customers('sk-XXX', config)
45+
customer_payload = {'b': 2}
46+
customer_response = {'a': 1}
47+
48+
api_post = Mock()
49+
api_post.return_value = customer_response
50+
51+
with patch.object(customer, 'api_post', api_post):
52+
response = customer.create(customer_payload)
53+
54+
self.assertEqual(response, customer_response)
55+
api_post.assert_called_with('/v1/customers', customer_payload)

0 commit comments

Comments
 (0)