将所有客户从 Stripe API 检索到一个列表中的最佳做法是什么 [英] What is the best practice to retrieve all customers from Stripe API into one list
问题描述
当调用 Stripe::Customer.all(:limit => 100)
时,每次调用限制为 100.我们的客户比这多得多,我想同时得到他们.我是否遗漏了什么,或者这只能通过编写一个简单的循环来检查 has_more
属性,然后进行新调用直到 has_more = false
?
When calling Stripe::Customer.all(:limit => 100)
there is a limit of 100 per call. We have a lot more customers than that, and I would like to get them all at once. Am I missing something, or is this only possible by writing a naive loop that checks on the has_more
attribute and then makes a new call until has_more = false
?
推荐答案
你说得对,你必须用游标写一个简单的循环 根据条带文档:
You are right, you must write a naive loop with a cursor per the stripe docs:
可选
用于分页的游标.starting_after
是一个对象 ID,用于定义您在列表中的位置.例如,如果您发出一个列表请求并收到 100 个以 obj_foo
结尾的对象,那么您的后续调用可以包含 starting_after=obj_foo
以获取列表的下一页.
A cursor for use in pagination. starting_after
is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo
, your subsequent call can include starting_after=obj_foo
in order to fetch the next page of the list.
这里有一个,以防有人需要快速复制粘贴.
Here's one in case anyone needs a quick copy-paste.
def self.all_stripe_customers
starting_after = nil
customers = []
loop do
results = Stripe::Customer.list(limit: 100, starting_after: starting_after)
break if results.data.length == 0
customers = customers + results.data
starting_after = results.data.last.id
end
return customers
end
这篇关于将所有客户从 Stripe API 检索到一个列表中的最佳做法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!