Skip to content

Commit f2c2d91

Browse files
Add transactions resource (#35)
* Add transactions resource
1 parent 8d5bdfd commit f2c2d91

File tree

7 files changed

+143
-0
lines changed

7 files changed

+143
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Current Version
22
- Update flake8 quote linting
33
- Add official support for Python 3.7
44
- Drop official support for Python 2.6, 3.2, 3.3
5+
- Added `transactions` resource
56

67
2.7.0 June 21, 2018
78
- Added optional `time_field` argument to `client.accounts.transactions`

README.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ We currently expose the following resources to manage:
9696
* `Orders`_
9797
* `Customers`_
9898
* `Links`_
99+
* `Transactions`_
99100

100101
Accounts
101102
~~~~~~~~
@@ -317,6 +318,34 @@ Get
317318
print(response)
318319
# <class pybutton.Response id: customer-1234, ...>
319320
321+
Transactions
322+
~~~~~~~~~~~~
323+
324+
All
325+
'''
326+
327+
You may pass the following optional arguments:
328+
329+
* ``cursor`` (string): An API cursor to fetch a specific set of results.
330+
* ``start`` (ISO-8601 datetime string): Fetch transactions after this time.
331+
* ``end`` (ISO-8601 datetime string): Fetch transactions before this time.
332+
* ``time_field`` (string): Which time field ``start`` and ``end`` filter on.
333+
334+
.. code:: python
335+
336+
from pybutton import Client
337+
338+
client = Client('sk-XXX')
339+
340+
response = client.transactions(
341+
start='2016-07-15T00:00:00.000Z',
342+
end='2016-09-30T00:00:00.000Z',
343+
time_field='modified_date',
344+
)
345+
346+
print(response)
347+
# <class pybutton.Response [100 elements]>
348+
320349
Response
321350
--------
322351

pybutton/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pybutton.resources import Merchants
99
from pybutton.resources import Orders
1010
from pybutton.resources import Links
11+
from pybutton.resources import Transactions
1112
from pybutton.error import ButtonClientError
1213

1314

@@ -57,6 +58,7 @@ def __init__(self, api_key, config=None):
5758
self.merchants = Merchants(api_key, config)
5859
self.customers = Customers(api_key, config)
5960
self.links = Links(api_key, config)
61+
self.transactions = Transactions(api_key, config)
6062

6163

6264
def config_with_defaults(config):

pybutton/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
TIME_FIELD_CREATED = 'created_date'
2+
TIME_FIELD_MODIFIED = 'modified_date'

pybutton/resources/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
from pybutton.resources.links import Links # noqa: 401
99
from pybutton.resources.merchants import Merchants # noqa: 401
1010
from pybutton.resources.orders import Orders # noqa: 401
11+
from pybutton.resources.transactions import Transactions # noqa: 401

pybutton/resources/transactions.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 Transactions(Resource):
10+
"""Manages interacting with Button Transactions via the Button API
11+
12+
See Resource for class docstring.
13+
14+
"""
15+
16+
def all(self, cursor=None, start=None, end=None, time_field=None):
17+
"""Get a list of transactions.
18+
To paginate transactions, pass the result of response.next_cursor() as
19+
the cursor argument.
20+
Unlike Accounts.transactions, which retrieves transactions only for a
21+
single account, Transactions.all retrieves all of an organization's
22+
transactions.
23+
24+
25+
Args:
26+
cursor (str) optional: An opaque string that lets you view a
27+
consistent list of transactions.
28+
start (ISO-8601 datetime str) optional: Filter out transactions
29+
created at or after this time.
30+
end (ISO-8601 datetime str) optional: Filter out transactions
31+
created before this time.
32+
time_field (str) optional: Which time field ``start`` and ``end``
33+
filter on. Defaults to created_date.
34+
35+
Raises:
36+
pybutton.ButtonClientError
37+
38+
Returns:
39+
(pybutton.Response) The API response
40+
41+
"""
42+
43+
query = {}
44+
45+
if cursor:
46+
query['cursor'] = cursor
47+
if start:
48+
query['start'] = start
49+
if end:
50+
query['end'] = end
51+
if time_field:
52+
query['time_field'] = time_field
53+
54+
return self.api_get('/v1/affiliation/transactions', query=query)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 import constants
11+
from pybutton.resources import Transactions
12+
13+
config = {
14+
'hostname': 'api.usebutton.com',
15+
'secure': True,
16+
'port': 443,
17+
'timeout': None,
18+
}
19+
20+
21+
class TransactionsTestCase(TestCase):
22+
23+
def test_all(self):
24+
transactions = Transactions('sk-XXX', config)
25+
transaction_response = [{'a': 1}, {'b': 2}]
26+
27+
api_get = Mock()
28+
api_get.return_value = transaction_response
29+
30+
with patch.object(transactions, 'api_get', api_get):
31+
response = transactions.all(
32+
cursor='abc',
33+
start='2016-09-15T00:00:00.000Z',
34+
end='2016-09-30T00:00:00.000Z'
35+
)
36+
self.assertEqual(
37+
api_get.call_args[0][0],
38+
'/v1/affiliation/transactions'
39+
)
40+
self.assertEqual(response, transaction_response)
41+
query = api_get.call_args[1]['query']
42+
self.assertEqual(query['cursor'], 'abc')
43+
self.assertEqual(query['start'], '2016-09-15T00:00:00.000Z')
44+
self.assertEqual(query['end'], '2016-09-30T00:00:00.000Z')
45+
46+
response = transactions.all(
47+
cursor='abc',
48+
start='2016-09-15T00:00:00.000Z',
49+
end='2016-09-30T00:00:00.000Z',
50+
time_field=constants.TIME_FIELD_MODIFIED
51+
)
52+
self.assertEqual(response, transaction_response)
53+
query = api_get.call_args[1]['query']
54+
self.assertEqual(query['time_field'], 'modified_date')

0 commit comments

Comments
 (0)