Skip to content

Commit 76e5092

Browse files
committed
WIP - added /customer endpoint
1 parent 73cc25e commit 76e5092

File tree

7 files changed

+162
-2
lines changed

7 files changed

+162
-2
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: 40 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,44 @@ 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+
'advertising_id': '6E82078A-8146-4BA4-AC5B-76104861E61A',
258+
})
259+
260+
print(response)
261+
# <class pybutton.Response id: 'customer-1234', ...>
262+
263+
Get
264+
'''
265+
266+
.. code:: python
267+
268+
from pybutton import Client
269+
270+
client = Client('sk-XXX')
271+
272+
response = client.customers.get('customer-XXX')
273+
274+
print(response)
275+
# <class pybutton.Response id: customer-XXX, segments:[], ...>
276+
238277
Response
239278
--------
240279

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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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(customer._path('customer-1'), '/v1/customers/customer-1')
26+
27+
def test_get(self):
28+
customer = Customers('sk-XXX', config)
29+
customer_response = {'a': 1}
30+
31+
api_get = Mock()
32+
api_get.return_value = customer_response
33+
34+
with patch.object(customer, 'api_get', api_get):
35+
response = customer.get('customer-XXX')
36+
37+
self.assertEqual(response, customer_response)
38+
api_get.assert_called_with('/v1/customers/customer-XXX')
39+
40+
def test_create(self):
41+
customer = Customers('sk-XXX', config)
42+
customer_payload = {'b': 2}
43+
customer_response = {'a': 1}
44+
45+
api_post = Mock()
46+
api_post.return_value = customer_response
47+
48+
with patch.object(customer, 'api_post', api_post):
49+
response = customer.create(customer_payload)
50+
51+
self.assertEqual(response, customer_response)
52+
api_post.assert_called_with('/v1/customers', customer_payload)

pybutton/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = '2.3.0'
1+
VERSION = '2.4.0'

0 commit comments

Comments
 (0)