From e8bd2c7e6ce64d8ad151951d9e2e9c39de7b1af1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Magalha=CC=83es?= Date: Fri, 3 Dec 2021 10:07:31 +0000 Subject: [PATCH] feat: improved list_orders_a solution --- src/shopify/base.py | 32 ++++++++++++++++++++++++++++++++ src/shopify/order.py | 30 ++---------------------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/shopify/base.py b/src/shopify/base.py index b6d79fa..53f17ce 100644 --- a/src/shopify/base.py +++ b/src/shopify/base.py @@ -252,3 +252,35 @@ def _build_url(self): if not self.store_url: raise appier.OperationalError(message = "No store URL provided") self.base_url = "https://%s/" % self.store_url + + def _fetch_many(self, url, method_count = None, limit = 50, all = False, **kwargs): + # creates the sequence that is going to hold the complete set of + # items to be retrieved from teh remote data source + items = [] + + # creates a variable to store the identifier of the last item that was + # retrieved, assumes this will allow proper pagination of the items + last_id = None + + # sets the initial value of the items remaining to be fetched as the + # limit value, this value will change as the loop continues + item_remaining = limit + + # if "all" flag is set to true then sets the the items remaining value + # to the value obtained method count method call + if all: item_remaining = method_count() + + # keeps fetching items until there isn't any more items to fetch + while item_remaining > 0: + contents = self.get( + url, + limit = limit, + since_id = last_id, + **kwargs + ) + items.extend(contents["items"]) + try: + last_id = items[-1]["id"] + except: + return [] + item_remaining -= limit diff --git a/src/shopify/order.py b/src/shopify/order.py index 3e66054..e46138f 100644 --- a/src/shopify/order.py +++ b/src/shopify/order.py @@ -44,12 +44,13 @@ def list_orders(self, *args, **kwargs): contents = self.get(url, **kwargs) return contents["orders"] - def list_orders_a(self, all = True, limit = 50, **kwargs): + def list_orders_a(self, limit = 50, all = True, **kwargs): url = self.base_url + "admin/orders.json" orders = self._fetch_many( url, method_count = self.get_orders_count, limit = limit, + all = all, **kwargs ) return orders @@ -127,30 +128,3 @@ def create_metafield_order(self, id, key, value, value_type = "string", namespac ) ) return contents["metafield"] - - def _fetch_many(self, url, method_count = None, limit = 50, **kwargs): - orders = [] - last_id = None - orders_count = limit - - # if "all" flag is set to true then set the "limit" to the maximum - # allowed value (250) and set "order_count" with the total number - # of existing orders - if all: - limit = 250 - orders_count = method_count() - - # keep fetching orders until there isn't any more orders to fetch - while orders_count > 0: - contents = self.get( - url, - limit = limit, - since_id = last_id, - **kwargs - ) - orders.extend(contents["orders"]) - try: - last_id = orders[-1]["id"] - except: - return [] - orders_count -= limit