From 63c5edd60879bccbac8c7087e8e36521a3da44a5 Mon Sep 17 00:00:00 2001 From: Gareth du Plooy Date: Tue, 16 Jul 2019 14:31:08 -0500 Subject: [PATCH] Implements relative cursor pagination --- shopify/base.py | 2 +- shopify/mixins.py | 10 ++++++ shopify/pagination_link_headers.py | 20 +++++++++++ test/pagination_test.py | 53 ++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 shopify/pagination_link_headers.py create mode 100644 test/pagination_test.py diff --git a/shopify/base.py b/shopify/base.py index 6cfee0ea..510c5c44 100644 --- a/shopify/base.py +++ b/shopify/base.py @@ -166,7 +166,7 @@ def set_url(cls, value): @six.add_metaclass(ShopifyResourceMeta) -class ShopifyResource(ActiveResource, mixins.Countable): +class ShopifyResource(ActiveResource, mixins.Countable, mixins.Pagination): _format = formats.JSONFormat _threadlocal = threading.local() _headers = {'User-Agent': 'ShopifyPythonAPI/%s Python/%s' % (shopify.VERSION, sys.version.split(' ', 1)[0])} diff --git a/shopify/mixins.py b/shopify/mixins.py index c7806a0c..6c61a8b3 100644 --- a/shopify/mixins.py +++ b/shopify/mixins.py @@ -1,4 +1,7 @@ import shopify.resources +import shopify +import pdb +from shopify.pagination_link_headers import PaginationLinkHeaders class Countable(object): @@ -34,3 +37,10 @@ class Events(object): def events(self): return shopify.resources.Event.find(resource=self.__class__.plural, resource_id=self.id) + +class Pagination(object): + + def pagination_link_headers(self): + self.pagination_link_headers = PaginationLinkHeaders() + self._next_page_info = self.pagination_link_headers.link_headers + diff --git a/shopify/pagination_link_headers.py b/shopify/pagination_link_headers.py new file mode 100644 index 00000000..7cfd4acc --- /dev/null +++ b/shopify/pagination_link_headers.py @@ -0,0 +1,20 @@ +import pdb +import shopify +class PaginationLinkHeaders(object): + + @classmethod + def response(cls): + if not shopify.Shop.connection.response: + shopify.Shop.current() + return shopify.Shop.connection.response + + + def __init__(self): + self._link_headers = self.parse_link_header(self.response().headers['Link']) + self.next_link_header = + + def parse_link_header(self, link_header): + pdb.set_trace() + return 'aaa' + + diff --git a/test/pagination_test.py b/test/pagination_test.py new file mode 100644 index 00000000..bfba23dc --- /dev/null +++ b/test/pagination_test.py @@ -0,0 +1,53 @@ +import shopify +import pdb +from mock import patch +from test.test_helper import TestCase +from pyactiveresource.activeresource import ActiveResource +from pyactiveresource.util import xml_to_dict + + +class PaginationTest(TestCase): + """ + API Calls Limit Tests + + Conversion of test/limits_test.rb + """ + @classmethod + def setUpClass(self): + self.original_headers = None + + def setUp(self): + super(PaginationTest, self).setUp() + self.fake('shop') + shopify.Shop.current() + # TODO: Fake not support Headers + self.original_headers = shopify.Shop.connection.response.headers + + self.next_page_info = "eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6NDQwMDg5NDIzLCJsYXN0X3ZhbHVlIjoiNDQwMDg5NDIzIn0%3D" + self.previous_page_info = "eyJsYXN0X2lkIjoxMDg4MjgzMDksImxhc3RfdmFsdWUiOiIxMDg4MjgzMDkiLCJkaXJlY3Rpb24iOiJuZXh0In0%3D" + + # path_prefix = "%s/themes/%s" % (cls.site, theme_id) if theme_id else cls.site + + self.next_link_header = "; rel=\"next\"" % self.next_page_info + self.previous_link_header = "; rel=\"previous\"" % self.previous_page_info + + + def tearDown(self): + super(PaginationTest, self).tearDown() + shopify.Shop.connection.response.headers = self.original_headers + + def test_navigating_next_previous_orders(self): + link_header = "%s, %s" % (self.previous_page_info, self.next_page_info) + + self.fake('draft_orders', method='GET', code=200, body=self.load_fixture('draft_orders')) + draft_orders = shopify.DraftOrder.find() + with patch.dict( + shopify.Shop.connection.response.headers, + {'Link': link_header}, + clear=True): + + xx = draft_orders[0].pagination_link_headers() + pdb.set_trace() + self.assertEqual(1, len(draft_orders)) + self.assertEqual(517119332, draft_orders[0].id) +