Created
October 6, 2021 20:40
-
-
Save jamiemtdwyer/1ecc50b103190631557f155a4ede23e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_all_inventory_levels(): | |
get_inventory_levels(page_info: nil) # get first page of results | |
sleep 0.5 # naive approach to rate limiting | |
while is_next_page(): | |
next_page_info = get_next_page_info() | |
get_inventory_levels(page_info: next_page_info) | |
sleep 0.5 # naive approach to rate limiting | |
def get_inventory_levels(page_info = nil): | |
url = "https://{shop}.myshopify.com/admin/api/2021-07/inventory_levels.json?location_ids=..." | |
if page_info: | |
url += "&page_info=" + page_info # append page_info to query parameters | |
response = http.get(url) # perform HTTP request | |
process_response(response) # process response from Shopify API | |
def is_next_page(): | |
# return true / false based on presence of rel={next} indicator in "Link" response header | |
def get_next_page_info(): | |
# use regex to extract value of page_info query parameter from rel={next} indicator in "Link" response header | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment