Shopify API 对订单进行分页以获得准确的计数

问题描述 投票:0回答:2

在 Shopify ORDERS API 中,我使用

/admin/api/2021-01/orders/count.json

为了得到订单数,所以我想得到所有的订单。通过遵循 REST API 文档,我使用两个端点来执行此操作。

/admin/api/2021-01/orders.json?status=any

/admin/api/2021-01/orders.json?limit=250&status=any; rel=next

首先,我将使用第一个端点请求订单,在列表中我最多可以获取 50 个订单/商品。

然后使用计数器作为限制,假设我从

orders/count.json

的响应中收到了 550 个订单

我愿意:

accumulated = []
iter = 0
while True:
    if len(accumulated) > count:
        break
    if iter != 1:
        url = #user first url
    else:
        url = $use second url that has next

    items = #make a request here for that url and save each order item
    accumulated+=items #this saves each list to the accumulated list so we know that we hit the count

但由于某种原因,我只得到了计数的一小部分。比方说,在计数的 550 个中,我只得到了 350 个互不重复的。我想也许第二个网址只请求第二页而不继续到第三页。因此我正在做

first iteration = first page
second iteration = second page
third iteration = second page

所有这些都进入累积列表并停止循环,因为当累积超过 count 时循环将停止。

当我在shopify中请求ORDERS Endpoint时,我该如何做到这一点。我可以正确地从下一页开始吗?

我尝试按照shopify的教程发出分页请求,但我不清楚。关于如何使用它。这个

page_info
变量对我来说很难理解在哪里可以找到它以及如何使用它。

shopify shopify-api
2个回答
1
投票

嘿!在 Shopify REST api 中,每个 api 调用最多可以获得 250 个订单,如果有更多订单,您可以从标头中获得

LINK
,其中包含下一页请求的 URL,例如 Here you can see I have LINK variable in my response headers you just need to get this LINK and check for rel='next' flag

但是请记住,当您点击新 URL 并且仍然有更多订单要获取时,标头会发送带有两个 URL 的 LINK,1 表示上一个,1 表示下一个。

运行此代码片段以从标头获取链接

var flag = false;
var next_url = order.headers.link;
if(next_url){
  flag = true
  next_url = next_url.replace("<","");
  next_url = next_url.replace(">", "");
  var next_url_array = next_url.split('; ');
  var link_counter_start = next_url_array[0].indexOf("page_info=")+10;
  var link_counter_length = (next_url_array[0].length);
  var next_cursor="";
  var link_counter;
  for(link_counter=link_counter_start; link_counter<link_counter_length; link_counter++){
    next_cursor+=(next_url_array[0][link_counter])
  }
}

对于第一个 api 但如果您有两个以上页面,请使用以下代码将下一个链接与上一个和下一个标志分开

next_url = order.headers.link;
      var next_url_array,
       link_counter_start, link_counter_length,
        link_counter;
      if(next_url.includes(',')){
        next_url = next_url.split(',');
        next_url = next_url[1];
      }
  
  next_url = next_url.replace("<","");
  next_url = next_url.replace(">", "");
  next_url_array = next_url.split('; ');
  link_counter_start = next_url_array[0].indexOf("page_info=")+10;
  link_counter_length = (next_url_array[0].length);
  next_cursor="";
  for(link_counter=link_counter_start; link_counter<link_counter_length; link_counter++){
    next_cursor+=(next_url_array[0][link_counter])
  }
  if(next_url_array[1] != 'rel="next"'){
    flag = false;
  }

0
投票

这是使用

link
标题的已接受答案的较短版本。这应该至少比公认的答案更稳定、更高效。

它找到位于

rel="next"
注释之前的查询字符串。

它将查询参数分解为一个对象,因为这就是我使用的包所期望的。如果这对您没有用,只需使用正则表达式匹配的结果即可。

let testHeaderValue =
  '<https://X.myshopify.com/admin/api/2023-10/products.json?limit=250&page_info=X>; rel="next",<https://X.myshopify.com/admin/api/2023-10/products.json?limit=250&page_info=X>; rel="previous"'

const queryString = testHeaderValue.match(/([^?]*)>; rel="next"/)[1]
const queryArray = queryString.split("&")
const queryObject = queryArray.reduce((acc, x) => {
  const [key, value] = x.split("=")
  acc[key] = value
  return acc
}, {})

console.dir(queryObject)
/*
{
  limit: '250',
  page_info: 'X'
}
*/
© www.soinside.com 2019 - 2024. All rights reserved.