我的目标是从Shopify导入客户的订单历史记录。 Shopify只允许我为每个请求导入250个订单,但我的客户有数千个。
这是(基本上)使用httr的当前工作解决方案非常慢
fetchedList <- list()
# Loop through pages of orders and collect them
for(pg in 1:10){
requestURL <- paste0("https://abc-store.myshopify.com/admin/orders.json?page=", p)
fetched <- httr::GET(
url = requestURL,
httr::add_headers(Accept = "application/json"),
httr::authenticate(user = "foo", password = "bar")
)
# Append the fetched response to fetchedList
fetchedList <- c(fetchedList, list(fetched))
}
# Process the results...
我想通过发出多个并发请求来加快速度。我怎样才能做到这一点?似乎curl和RCurl都支持这一点,但我对HTTP很新,无法使这两种解决方案都有效。
答案 0 :(得分:3)
您应该使用multi api来执行并发请求。请参阅?multi_run
的手册页或插图中的async requests部分。
还有一些软件包可以将多个api包装起来,以便更轻松。 crul package(如果您想真正了解async包,请注意crul不是拼写错误:)或更多。
答案 1 :(得分:2)
感谢@Jeroen将我指向crul包裹。当时,crul实际上没有设置此功能,但我与维护者交谈并实施了它。所以,从v 0.5.2.9100开始,我可以做到
dd <- Async$new(urls = c(
'https://abc-store.myshopify.com/admin/orders.json?page=1',
'https://abc-store.myshopify.com/admin/orders.json?page=2',
'https://abc-store.myshopify.com/admin/orders.json?page=3'
))
res <- dd$get(auth = auth(user = "foo", pwd = "bar"))
vapply(res, function(z) z$status_code, double(1))
vapply(res, function(z) z$success(), logical(1))
lapply(res, function(z) z$parse("UTF-8"))