Skip to content

Commit ae66c84

Browse files
author
Will Myers
authored
Add merchants resource (#13)
* Add merchants resource * Update README.rst * Update README.rst * Update README.rst * Update docs
1 parent c166211 commit ae66c84

File tree

7 files changed

+174
-86
lines changed

7 files changed

+174
-86
lines changed

README.rst

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,75 @@ The supported options are as follows:
8787
Resources
8888
---------
8989

90-
We currently expose two resources to manage, ``Orders`` and ``Accounts``.
90+
We currently expose the following resources to manage:
91+
92+
* ``Accounts``
93+
* ``Merchants``
94+
* ``Orders``
95+
96+
Accounts
97+
~~~~~~~~
98+
99+
All
100+
'''
101+
102+
.. code:: python
103+
104+
from pybutton import Client
105+
106+
client = Client('sk-XXX')
107+
108+
response = client.accounts.all()
109+
110+
print(response)
111+
# <class pybutton.Response [2 elements]>
112+
113+
Transactions
114+
''''''''''''
115+
116+
Along with the required account ID, you may also
117+
pass the following optional arguments:
118+
119+
* ``cursor`` (string): An API cursor to fetch a specific set of results.
120+
* ``start`` (ISO-8601 datetime string): Fetch transactions after this time.
121+
* ``end`` (ISO-8601 datetime string): Fetch transactions before this time.
122+
123+
.. code:: python
124+
125+
from pybutton import Client
126+
127+
client = Client('sk-XXX')
128+
129+
response = client.accounts.transactions(
130+
'acc-123',
131+
start='2016-07-15T00:00:00.000Z',
132+
end='2016-09-30T00:00:00.000Z'
133+
)
134+
135+
print(response)
136+
# <class pybutton.Response [100 elements]>
137+
138+
Merchants
139+
~~~~~~~~~
140+
141+
All
142+
'''
143+
144+
You may pass the following optional arguments:
145+
146+
* ``status`` (string): Partnership status to filter by. One of ('approved', 'pending', or 'available')
147+
* ``currency`` (ISO-4217 string): Currency code to filter returned rates by
148+
149+
.. code:: python
150+
151+
from pybutton import Client
152+
153+
client = Client('sk-XXX')
154+
155+
response = client.merchants.all()
156+
157+
print(response)
158+
# <class pybutton.Response [23 elements]>
91159
92160
Orders
93161
~~~~~~
@@ -160,48 +228,6 @@ Delete
160228
print(response)
161229
# <class pybutton.Response >
162230
163-
Accounts
164-
~~~~~~~~
165-
166-
All
167-
'''
168-
169-
.. code:: python
170-
171-
from pybutton import Client
172-
173-
client = Client('sk-XXX')
174-
175-
response = client.accounts.all()
176-
177-
print(response)
178-
# <class pybutton.Response [2 elements]>
179-
180-
Transactions
181-
''''''''''''
182-
183-
Along with the required account ID, you may also
184-
pass the following optional arguments:
185-
186-
* ``cursor`` (string): An API cursor to fetch a specific set of results.
187-
* ``start`` (ISO-8601 datetime string): Fetch transactions after this time.
188-
* ``end`` (ISO-8601 datetime string): Fetch transactions before this time.
189-
190-
.. code:: python
191-
192-
from pybutton import Client
193-
194-
client = Client('sk-XXX')
195-
196-
response = client.accounts.transactions(
197-
'acc-123',
198-
start='2016-07-15T00:00:00.000Z',
199-
end='2016-09-30T00:00:00.000Z'
200-
)
201-
202-
print(response)
203-
# <class pybutton.Response [100 elements]>
204-
205231
Response
206232
--------
207233

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 .resources import Accounts
7+
from .resources import Merchants
78
from .resources import Orders
89
from .error import ButtonClientError
910

@@ -50,6 +51,7 @@ def __init__(self, api_key, config=None):
5051

5152
self.orders = Orders(api_key, config)
5253
self.accounts = Accounts(api_key, config)
54+
self.merchants = Merchants(api_key, config)
5355

5456

5557
def config_with_defaults(config):

pybutton/resources/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
from __future__ import print_function
44
from __future__ import unicode_literals
55

6-
from .orders import Orders
76
from .accounts import Accounts
7+
from .merchants import Merchants
8+
from .orders import Orders
89

9-
__all__ = [Orders, Accounts]
10+
__all__ = [
11+
Accounts,
12+
Merchants,
13+
Orders
14+
]

pybutton/resources/accounts.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,9 @@
77

88

99
class Accounts(Resource):
10-
'''Manages interacting with Button Accounts with the Button API
11-
12-
Args:
13-
api_key (string): Your organization's API key. Do find yours at
14-
https://app.usebutton.com/settings/organization.
15-
config (dict): Configuration options for the client. Options include:
16-
hostname: Defaults to api.usebutton.com.
17-
port: Defaults to 443 if config.secure, else defaults to 80.
18-
secure: Whether or not to use HTTPS. Defaults to True.
19-
timeout: The time in seconds for network requests to abort.
20-
Defaults to None.
21-
(N.B: Button's API is only exposed through HTTPS. This option is
22-
provided purely as a convenience for testing and development.)
23-
24-
Raises:
25-
pybutton.ButtonClientError
10+
'''Manages interacting with Button Accounts via the Button API
11+
12+
See Resource for class docstring.
2613
2714
'''
2815

pybutton/resources/merchants.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 .resource import Resource
7+
8+
9+
class Merchants(Resource):
10+
'''Manages interacting with Button Merchants via the Button API
11+
12+
See Resource for class docstring.
13+
14+
'''
15+
16+
def all(self, status=None, currency=None):
17+
'''Get a list of merchants and their configured rates
18+
19+
Args:
20+
status (str) optional: A status to filter by. One of ('approved',
21+
'pending', or 'available')
22+
currency (str) optional: An ISO-4217 currency code to filter rates
23+
by
24+
25+
Raises:
26+
pybutton.ButtonClientError
27+
28+
Returns:
29+
(pybutton.Response) The API response
30+
31+
'''
32+
33+
query = {}
34+
35+
if status:
36+
query['status'] = status
37+
38+
if currency:
39+
query['currency'] = currency
40+
41+
return self.api_get('/v1/merchants', query=query)

pybutton/resources/orders.py

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,9 @@
77

88

99
class Orders(Resource):
10-
'''Manages interacting with Button Orders with the Button API
11-
12-
Args:
13-
api_key (string): Your organization's API key. Do find yours at
14-
https://app.usebutton.com/settings/organization.
15-
config (dict): Configuration options for the client. Options include:
16-
hostname: Defaults to api.usebutton.com.
17-
port: Defaults to 443 if config.secure, else defaults to 80.
18-
secure: Whether or not to use HTTPS. Defaults to True.
19-
timeout: The time in seconds for network requests to abort.
20-
Defaults to None.
21-
(N.B: Button's API is only exposed through HTTPS. This option is
22-
provided purely as a convenience for testing and development.)
23-
24-
config (dict): Configuration options for the client. Options include:
25-
hostname: Defaults to api.usebutton.com.
26-
port: Defaults to 443 if config.secure, else defaults to 80.
27-
secure: Whether or not to use HTTPS. Defaults to True.
28-
timeout: The time in seconds for network requests to abort.
29-
Defaults to None.
30-
(N.B: Button's API is only exposed through HTTPS. This option is
31-
provided purely as a convenience for testing and development.)
32-
33-
Raises:
34-
pybutton.ButtonClientError
10+
'''Manages interacting with Button Orders via the Button API
11+
12+
See Resource for class docstring.
3513
3614
'''
3715

test/resources/merchants_test.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 Merchants
11+
12+
config = {
13+
'hostname': 'api.usebutton.com',
14+
'secure': True,
15+
'port': 443,
16+
'timeout': None,
17+
}
18+
19+
20+
class MerchantsTestCase(TestCase):
21+
22+
def test_all(self):
23+
merchants = Merchants('sk-XXX', config)
24+
merchants_response = [{'a': 1}, {'b': 2}]
25+
26+
api_get = Mock()
27+
api_get.return_value = merchants_response
28+
29+
with patch.object(merchants, 'api_get', api_get):
30+
response = merchants.all()
31+
32+
self.assertEqual(response, merchants_response)
33+
api_get.assert_called_with('/v1/merchants', query={})
34+
35+
def test_all_with_query(self):
36+
merchants = Merchants('sk-XXX', config)
37+
merchants_response = [{'a': 1}, {'b': 2}]
38+
39+
api_get = Mock()
40+
api_get.return_value = merchants_response
41+
42+
with patch.object(merchants, 'api_get', api_get):
43+
response = merchants.all(status='pending', currency='GBP')
44+
45+
self.assertEqual(response, merchants_response)
46+
api_get.assert_called_with(
47+
'/v1/merchants',
48+
query={'status': 'pending', 'currency': 'GBP'}
49+
)

0 commit comments

Comments
 (0)