Skip to content

Commit

Permalink
feat: improved list_orders_a solution
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Dec 3, 2021
1 parent a2329f5 commit e8bd2c7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
32 changes: 32 additions & 0 deletions src/shopify/base.py
Expand Up @@ -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
30 changes: 2 additions & 28 deletions src/shopify/order.py
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit e8bd2c7

Please sign in to comment.