-
Notifications
You must be signed in to change notification settings - Fork 1
Add transactions resource #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,6 +96,7 @@ We currently expose the following resources to manage: | |
| * `Orders`_ | ||
| * `Customers`_ | ||
| * `Links`_ | ||
| * `Transactions`_ | ||
|
|
||
| Accounts | ||
| ~~~~~~~~ | ||
|
|
@@ -317,6 +318,34 @@ Get | |
| print(response) | ||
| # <class pybutton.Response id: customer-1234, ...> | ||
|
|
||
| Transactions | ||
| ~~~~~~~~~~~~ | ||
|
|
||
| All | ||
| ''' | ||
|
|
||
| You may pass the following optional arguments: | ||
|
|
||
| * ``cursor`` (string): An API cursor to fetch a specific set of results. | ||
| * ``start`` (ISO-8601 datetime string): Fetch transactions after this time. | ||
| * ``end`` (ISO-8601 datetime string): Fetch transactions before this time. | ||
| * ``time_field`` (string): Which time field ``start`` and ``end`` filter on. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. default?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
|
|
||
| .. code:: python | ||
|
|
||
| from pybutton import Client | ||
|
|
||
| client = Client('sk-XXX') | ||
|
|
||
| response = client.transactions( | ||
| start='2016-07-15T00:00:00.000Z', | ||
| end='2016-09-30T00:00:00.000Z', | ||
| time_field='modified_date', | ||
| ) | ||
|
|
||
| print(response) | ||
| # <class pybutton.Response [100 elements]> | ||
|
|
||
| Response | ||
| -------- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| TIME_FIELD_CREATED = 'created_date' | ||
| TIME_FIELD_MODIFIED = 'modified_date' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| from __future__ import absolute_import | ||
| from __future__ import division | ||
| from __future__ import print_function | ||
| from __future__ import unicode_literals | ||
|
|
||
| from pybutton.resources.resource import Resource | ||
|
|
||
|
|
||
| class Transactions(Resource): | ||
| """Manages interacting with Button Transactions via the Button API | ||
|
|
||
| See Resource for class docstring. | ||
|
|
||
| """ | ||
|
|
||
| def all(self, cursor=None, start=None, end=None, time_field=None): | ||
| """Get a list of transactions. | ||
| To paginate transactions, pass the result of response.next_cursor() as | ||
| the cursor argument. | ||
| Unlike Accounts.transactions, which retrieves transactions only for a | ||
| single account, Transactions.all retrieves all of an organization's | ||
| transactions. | ||
|
|
||
|
|
||
| Args: | ||
| cursor (str) optional: An opaque string that lets you view a | ||
| consistent list of transactions. | ||
| start (ISO-8601 datetime str) optional: Filter out transactions | ||
| created at or after this time. | ||
| end (ISO-8601 datetime str) optional: Filter out transactions | ||
| created before this time. | ||
| time_field (str) optional: Which time field ``start`` and ``end`` | ||
| filter on. Defaults to created_date. | ||
|
|
||
| Raises: | ||
| pybutton.ButtonClientError | ||
|
|
||
| Returns: | ||
| (pybutton.Response) The API response | ||
|
|
||
| """ | ||
|
|
||
| query = {} | ||
|
|
||
| if cursor: | ||
| query['cursor'] = cursor | ||
| if start: | ||
| query['start'] = start | ||
| if end: | ||
| query['end'] = end | ||
| if time_field: | ||
| query['time_field'] = time_field | ||
|
|
||
| return self.api_get('/v1/affiliation/transactions', query=query) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| from __future__ import absolute_import | ||
| from __future__ import division | ||
| from __future__ import print_function | ||
| from __future__ import unicode_literals | ||
|
|
||
| from unittest import TestCase | ||
| from mock import Mock | ||
| from mock import patch | ||
|
|
||
| from pybutton import constants | ||
| from pybutton.resources import Transactions | ||
|
|
||
| config = { | ||
| 'hostname': 'api.usebutton.com', | ||
| 'secure': True, | ||
| 'port': 443, | ||
| 'timeout': None, | ||
| } | ||
|
|
||
|
|
||
| class TransactionsTestCase(TestCase): | ||
|
|
||
| def test_all(self): | ||
| transactions = Transactions('sk-XXX', config) | ||
| transaction_response = [{'a': 1}, {'b': 2}] | ||
|
|
||
| api_get = Mock() | ||
| api_get.return_value = transaction_response | ||
|
|
||
| with patch.object(transactions, 'api_get', api_get): | ||
| response = transactions.all( | ||
| cursor='abc', | ||
| start='2016-09-15T00:00:00.000Z', | ||
| end='2016-09-30T00:00:00.000Z' | ||
| ) | ||
| self.assertEqual( | ||
| api_get.call_args[0][0], | ||
| '/v1/affiliation/transactions' | ||
| ) | ||
| self.assertEqual(response, transaction_response) | ||
| query = api_get.call_args[1]['query'] | ||
| self.assertEqual(query['cursor'], 'abc') | ||
| self.assertEqual(query['start'], '2016-09-15T00:00:00.000Z') | ||
| self.assertEqual(query['end'], '2016-09-30T00:00:00.000Z') | ||
|
|
||
| response = transactions.all( | ||
| cursor='abc', | ||
| start='2016-09-15T00:00:00.000Z', | ||
| end='2016-09-30T00:00:00.000Z', | ||
| time_field=constants.TIME_FIELD_MODIFIED | ||
| ) | ||
| self.assertEqual(response, transaction_response) | ||
| query = api_get.call_args[1]['query'] | ||
| self.assertEqual(query['time_field'], 'modified_date') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add description for how this differs from accounts#transactions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added, not sure if I used the right convention in the description, though.