Accessing inventory and images

aidanhaidanh Member Posts: 3

I'm using Powershell to retrieve the inventory data (the Item endpoint) as well as images associated with the item (the Image endpoint).

I start by getting a new access and refresh token, then I can retrieve the inventory data.

I've successfully retrieved the inventory data, except for the fact that I am limited to 100 results for some reason. Is there a way to access the full results?

After pulling the inventory data into my own database, then I want to access the image data associated with the items.

Can I just use the same access token and loop through each item to get the corresponding image?

3 comments

  • VintageWineGuyVintageWineGuy Member Posts: 94 ✭

    Yes. I haven't done it in Powershell (I tend to use Python) but at some point you will have to iterate through everything 100 at a time.

    Call the API once to get your first chunk. You can get @attributes.count out of the response to set the high end of your loop, then iterate through. Set your new offset in the querystring for your next GET. Here is some pseudo code:

    headers = {'authorization' : 'Bearer {access_token}'}

    querystring = {'offset':0, 'limit':100}

    response = requests.get(api_url, params=querystring, headers=headers)

    all_data = response.json() #What the API gives you back

    attributes = all_data['@attributes'] #The piece of the response that has count, etc.

    all_resources = all_data[{Name of API you called, like Item}] #The piece of the response that has the actual things will be named Items, Customers, whatever

    total_amount = int(all_data['@attributes']['count'])

    current_offset = int(all_data['@attributes']['offset']) + int(all_data['@attributes']['limit'])

    current_limit = int(all_data['@attributes']['limit'])

    while total_amount > current_offset:

                    querystring = {'offset':current_offset, 'limit':current_limit}

                   response = requests.get(url, params=querystring, headers=headers)

                    all_data = response.json()

                    all_resources.extend(all_data[resource]) #Whatever PowerShell uses to concatenate; stick all the parts together

                    current_offset = current_offset + current_limit


    You will need to keep an eye on X-LS-API-Bucket-Level out of the response headers to make sure you don't overflow, but if you are only doing GETs they are pretty cheap and you can run quite a few without running in to issues. In my code I have a manage_rate function that keeps count and slows things down if needed.

  • aidanhaidanh Member Posts: 3

    Thanks for your help!


    What about this portion of my post?

    Can I just use the same access token and loop through each item to get the corresponding image?

  • aidanhaidanh Member Posts: 3

    Update: I successfully retrieved the images and realized where I was going wrong.

Sign In or Register to comment.