python shopifyAPI hangs when reading orders

21 views Asked by At

I am reading orders from shopify using the Shopify Admin API Python Library (V 12.4.). I can connect, I can read orders ... all good, except that the API hangs sometimes. I am reading approx. 12k orders, but sometimes it hangs after 6k and does not continue. See (simplified) code attached.

Question 1: Is there a known issue, or anything I do (obviously) wrong?

Question 2: I am reading the full history of the orders on a daily basis - which is (at least) not optimal. I would rather like to load only all orders changed since 'last check'. I assume I can do so using the updated_at_min filter on orders. Storing this in 'changed orders per day' files would result in duplicated order across files over time. At the end I am only interested in the 'last version'/ current status of each order. I wonder if there is a smart solution for this - ideas welcome.

Thank you in advance!

Code sample:

import shopify
import time

SHOPIFY_ACCOUNTS = {
    's1' : {
        'SHOP': 's1', 'SHOPDOMAIN': 's1.myshopify.com',
        'APIVERSION': '2023-10',
        'TOKEN': '...token1...'
        },
    's2': {
        'SHOP': 's2', 'SHOPDOMAIN': 's2.myshopify.com',
        'APIVERSION': '2023-10',
        'TOKEN': '...token2...'
        }
}


def processMyOrders(orders):
    data = []
    for o in orders:
        # ... read some attributes like o.id
        for f in o.fulfillments:
            # ... read some attributes like f.updated_at
            for i in f.line_items:
                #...read some attributes like i.quantity
                for d in i.discount_allocations:
                    #read some attributes like d.amount
            data.append(_collected_data_)
    return data


def getMyOrders():    
    for shop in SHOPIFY_ACCOUNTS:
        #open session
        session = shopify.Session(SHOPIFY_ACCOUNTS[shop]['SHOPDOMAIN'], SHOPIFY_ACCOUNTS[shop]['APIVERSION'], SHOPIFY_ACCOUNTS[shop]['TOKEN'])
        shopify.ShopifyResource.activate_session(session)

        #get initial orders
        orders = shopify.Order.find(status='closed', limit=250)
        order_details = processMyOrders(orders)
        #... save order_details to file
        time.sleep(2)   #ensure quota limit

        #get following orders
        while orders.has_next_page():
            orders = orders.next_page()
            order_details = processMyOrders(orders)
            #... save/append order_details to file
            time.sleep(2)   #ensure quota limit

        #close session
        shopify.ShopifyResource.clear_session()
        session = None  #just to be sure
0

There are 0 answers