From ab00995fdbf4859cea9af2b5d4c357b5d2e71a29 Mon Sep 17 00:00:00 2001 From: Tyler Nappy Date: Tue, 5 Dec 2017 18:22:41 -0500 Subject: [PATCH 1/5] added Links resource --- CHANGELOG.md | 3 ++ README.rst | 70 +++++++++++++++++++++++++++ pybutton/client.py | 2 + pybutton/resources/__init__.py | 4 +- pybutton/resources/links.py | 59 ++++++++++++++++++++++ pybutton/test/resources/links_test.py | 48 ++++++++++++++++++ pybutton/version.py | 2 +- 7 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 pybutton/resources/links.py create mode 100644 pybutton/test/resources/links_test.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 953448d..80d224c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Current Version - +2.5.0 December 5, 2017 + - Add links resource + 2.4.0 July 19, 2017 - Add customers resource diff --git a/README.rst b/README.rst index 84484f4..80827c5 100644 --- a/README.rst +++ b/README.rst @@ -93,6 +93,7 @@ We currently expose the following resources to manage: * `Merchants`_ * `Orders`_ * `Customers`_ +* `Links`_ Accounts ~~~~~~~~ @@ -225,6 +226,75 @@ Update Delete '''''' +.. code:: python + + from pybutton import Client + + client = Client('sk-XXX') + + response = client.orders.delete('btnorder-XXX') + + print(response) + # + +Links +~~~~~~ + +Create +'''''' + +.. code:: python + + from pybutton import Client + + client = Client('sk-XXX') + + response = client.links.create({ + 'url': 'https://www.jet.com', + "experience": { + 'btn_pub_ref': 'my-pub-ref', + 'btn_pub_user': 'user-id', + }, + }) + + print(response) + # + +Get Info +''' + +.. code:: python + + from pybutton import Client + + client = Client('sk-XXX') + + rresponse = client.links.get_info({ + "url": "https://www.jet.com", + }) + + print(response) + # + +Update +'''''' + +.. code:: python + + from pybutton import Client + + client = Client('sk-XXX') + + response = client.orders.update('btnorder-XXX', { + 'total': 60, + }) + + print(response) + # + +Delete +'''''' + .. code:: python from pybutton import Client diff --git a/pybutton/client.py b/pybutton/client.py index 813398a..476df32 100644 --- a/pybutton/client.py +++ b/pybutton/client.py @@ -7,6 +7,7 @@ from pybutton.resources import Customers from pybutton.resources import Merchants from pybutton.resources import Orders +from pybutton.resources import Links from pybutton.error import ButtonClientError @@ -55,6 +56,7 @@ def __init__(self, api_key, config=None): self.accounts = Accounts(api_key, config) self.merchants = Merchants(api_key, config) self.customers = Customers(api_key, config) + self.links = Links(api_key, config) def config_with_defaults(config): diff --git a/pybutton/resources/__init__.py b/pybutton/resources/__init__.py index db3b057..b7142fc 100644 --- a/pybutton/resources/__init__.py +++ b/pybutton/resources/__init__.py @@ -7,10 +7,12 @@ from pybutton.resources.customers import Customers from pybutton.resources.merchants import Merchants from pybutton.resources.orders import Orders +from pybutton.resources.links import Links __all__ = [ Accounts, Customers, Merchants, - Orders + Orders, + Links ] diff --git a/pybutton/resources/links.py b/pybutton/resources/links.py new file mode 100644 index 0000000..bd8a4ba --- /dev/null +++ b/pybutton/resources/links.py @@ -0,0 +1,59 @@ +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 Links(Resource): + '''Manages interacting with Button Links via the Button API + + See Resource for class docstring. + + ''' + + def _path(self, link=None): + '''Format a url path + + Args: + link (dict): A dict representing the attributes of a link + + Returns: + (str): The formatted path + + ''' + + return '/v1/links' + + def create(self, link): + '''Create a link + + Args: + link (dict): A dict representing the attributes of a link + + Raises: + pybutton.ButtonClientError + + Returns: + (pybutton.Response) The API response + + ''' + + return self.api_post(self._path(), link) + + def get_info(self, link): + '''Get info on a link + + Args: + link (dict): A dict representing the attributes of a link for info + + Raises: + pybutton.ButtonClientError + + Returns: + (pybutton.Response) The API response + + ''' + + return self.api_post(self._path() + '/info', link) diff --git a/pybutton/test/resources/links_test.py b/pybutton/test/resources/links_test.py new file mode 100644 index 0000000..312e376 --- /dev/null +++ b/pybutton/test/resources/links_test.py @@ -0,0 +1,48 @@ +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.resources import Links + +config = { + 'hostname': 'api.usebutton.com', + 'secure': True, + 'port': 443, + 'timeout': None, +} + + +class LinksTestCase(TestCase): + + def test_create(self): + link = Links('https://test.com', config) + link_payload = {'b': 2} + link_response = {'a': 1} + + api_post = Mock() + api_post.return_value = link_response + + with patch.object(link, 'api_post', api_post): + response = link.create(link_payload) + + self.assertEqual(response, link_response) + api_post.assert_called_with('/v1/links', link_payload) + + def test_get_info(self): + link = Links('https://test.com', config) + link_payload = {'b': 2} + link_response = {'a': 1} + + api_post = Mock() + api_post.return_value = link_response + + with patch.object(link, 'api_post', api_post): + response = link.create(link_payload) + + self.assertEqual(response, link_response) + api_post.assert_called_with('/v1/links/info', link_payload) diff --git a/pybutton/version.py b/pybutton/version.py index 6259394..8af1c58 100644 --- a/pybutton/version.py +++ b/pybutton/version.py @@ -1 +1 @@ -VERSION = '2.4.0' +VERSION = '2.5.0' From 9f30ad4b9dfdbc8bdb9acbc9d7031993cd61db71 Mon Sep 17 00:00:00 2001 From: Tyler Nappy Date: Tue, 5 Dec 2017 18:29:35 -0500 Subject: [PATCH 2/5] updated test for get info links --- pybutton/test/resources/links_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pybutton/test/resources/links_test.py b/pybutton/test/resources/links_test.py index 312e376..94ea155 100644 --- a/pybutton/test/resources/links_test.py +++ b/pybutton/test/resources/links_test.py @@ -45,4 +45,4 @@ def test_get_info(self): response = link.create(link_payload) self.assertEqual(response, link_response) - api_post.assert_called_with('/v1/links/info', link_payload) + api_post.assert_called_with('/v1/links', link_payload) From 0fdc2ef225e03e7dcae7434054a799c44d438922 Mon Sep 17 00:00:00 2001 From: Tyler Nappy Date: Tue, 5 Dec 2017 18:33:10 -0500 Subject: [PATCH 3/5] removed duplicate customer in README --- README.rst | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/README.rst b/README.rst index 80827c5..f2bfa11 100644 --- a/README.rst +++ b/README.rst @@ -276,36 +276,6 @@ Get Info print(response) # -Update -'''''' - -.. code:: python - - from pybutton import Client - - client = Client('sk-XXX') - - response = client.orders.update('btnorder-XXX', { - 'total': 60, - }) - - print(response) - # - -Delete -'''''' - -.. code:: python - - from pybutton import Client - - client = Client('sk-XXX') - - response = client.orders.delete('btnorder-XXX') - - print(response) - # - Customers ~~~~~~~~~ From 3cb219b5b3302d0616b256094cf38db0fc3a7915 Mon Sep 17 00:00:00 2001 From: Tyler Nappy Date: Wed, 6 Dec 2017 11:24:43 -0500 Subject: [PATCH 4/5] updated test for links --- pybutton/test/resources/links_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pybutton/test/resources/links_test.py b/pybutton/test/resources/links_test.py index 94ea155..d98f1f1 100644 --- a/pybutton/test/resources/links_test.py +++ b/pybutton/test/resources/links_test.py @@ -42,7 +42,7 @@ def test_get_info(self): api_post.return_value = link_response with patch.object(link, 'api_post', api_post): - response = link.create(link_payload) + response = link.get_info(link_payload) self.assertEqual(response, link_response) - api_post.assert_called_with('/v1/links', link_payload) + api_post.assert_called_with('/v1/links/info', link_payload) From 739cc8a744f364766b5bb1f0464dc8754955c096 Mon Sep 17 00:00:00 2001 From: Tyler Nappy Date: Thu, 7 Dec 2017 14:20:51 -0500 Subject: [PATCH 5/5] removed typo in README; alphabatezied imports --- README.rst | 2 +- pybutton/resources/__init__.py | 6 +++--- pybutton/resources/links.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index f2bfa11..4f34b71 100644 --- a/README.rst +++ b/README.rst @@ -269,7 +269,7 @@ Get Info client = Client('sk-XXX') - rresponse = client.links.get_info({ + response = client.links.get_info({ "url": "https://www.jet.com", }) diff --git a/pybutton/resources/__init__.py b/pybutton/resources/__init__.py index b7142fc..3d2b0e9 100644 --- a/pybutton/resources/__init__.py +++ b/pybutton/resources/__init__.py @@ -5,14 +5,14 @@ from pybutton.resources.accounts import Accounts from pybutton.resources.customers import Customers +from pybutton.resources.links import Links from pybutton.resources.merchants import Merchants from pybutton.resources.orders import Orders -from pybutton.resources.links import Links __all__ = [ Accounts, Customers, + Links, Merchants, - Orders, - Links + Orders ] diff --git a/pybutton/resources/links.py b/pybutton/resources/links.py index bd8a4ba..205addb 100644 --- a/pybutton/resources/links.py +++ b/pybutton/resources/links.py @@ -13,7 +13,7 @@ class Links(Resource): ''' - def _path(self, link=None): + def _path(self): '''Format a url path Args: