stripe: iterate over ALL coupons
Question:
I’m using stripe in my website. I’m trying to check if a user supplied input matches a coupon but I can’t figure out how to iterate over all coupons (it appears I always need to enter a limit which can range from 1 to 100): https://stripe.com/docs/api/python#list_coupons
Here’s what I’ve tried so far, which only returns 10 coupons which is the default:
coupons = stripe.Coupon.list(limit=None)
Answers:
Reading the documentation, it looks like you need to paginate through the results.
Basically, you request for a first page of N
coupons, and if there is more to fetch, you request for the next N
coupons, starting from the last one of your previous request.
Looking at the docs, I came up with this approach:
def get_all_coupons(page_size=100):
last_coupon = None
while True:
response = stripe.Coupon.list(limit=page_size, starting_after=last_coupon)
coupons = response['data']
if coupons:
for coupon in coupons:
yield coupon
last_coupon = coupons[-1]
if not response['has_more']:
break
get_all_coupons()
return a generator, that yields all the coupons, fetching 100 at a time.
Note: I have not tested this.
I’ve never used stripe but the docs say you can use the starting_after attribute to define your place in the coupons list. I don’t know how to read the coupon IDs from the list it returns but you will need that.
# Coupons 0 - 100
coupons_list_1 = stripe.Coupon.list(limit=100)
# Get last coupon in coupons_list_1 and get its ID
last_coupon_id = '$1OFF'
# Coupons 100 - 200
coupons_list_2 = stripe.Coupon.list(starting_after=last_coupon_id, limit=100)
# Check if user inputted coupon is in either list
userinput in coupons_list_1 + coupons_list_2
stripe now supports auto-pagination: https://stripe.com/docs/api/pagination/auto
customers = stripe.Customer.list(limit=3)
for customer in customers.auto_paging_iter():
# Do something with customer
I’m using stripe in my website. I’m trying to check if a user supplied input matches a coupon but I can’t figure out how to iterate over all coupons (it appears I always need to enter a limit which can range from 1 to 100): https://stripe.com/docs/api/python#list_coupons
Here’s what I’ve tried so far, which only returns 10 coupons which is the default:
coupons = stripe.Coupon.list(limit=None)
Reading the documentation, it looks like you need to paginate through the results.
Basically, you request for a first page of N
coupons, and if there is more to fetch, you request for the next N
coupons, starting from the last one of your previous request.
Looking at the docs, I came up with this approach:
def get_all_coupons(page_size=100):
last_coupon = None
while True:
response = stripe.Coupon.list(limit=page_size, starting_after=last_coupon)
coupons = response['data']
if coupons:
for coupon in coupons:
yield coupon
last_coupon = coupons[-1]
if not response['has_more']:
break
get_all_coupons()
return a generator, that yields all the coupons, fetching 100 at a time.
Note: I have not tested this.
I’ve never used stripe but the docs say you can use the starting_after attribute to define your place in the coupons list. I don’t know how to read the coupon IDs from the list it returns but you will need that.
# Coupons 0 - 100
coupons_list_1 = stripe.Coupon.list(limit=100)
# Get last coupon in coupons_list_1 and get its ID
last_coupon_id = '$1OFF'
# Coupons 100 - 200
coupons_list_2 = stripe.Coupon.list(starting_after=last_coupon_id, limit=100)
# Check if user inputted coupon is in either list
userinput in coupons_list_1 + coupons_list_2
stripe now supports auto-pagination: https://stripe.com/docs/api/pagination/auto
customers = stripe.Customer.list(limit=3)
for customer in customers.auto_paging_iter():
# Do something with customer