R 如何知道何时可以使用令牌?

R 如何知道何时可以使用令牌?,r,api,curl,R,Api,Curl,一些API使用API调用而不是结果返回令牌,然后使用该令牌进行第二次API调用以获得查询结果。现在,我只是在函数中使用Sys.sleep()来猜测查询完成和令牌可用所需的时间。看来一定有更好的办法。也就是说,您是否可以每隔X秒(或几秒)迭代发送第二个API调用,直到成功 下面是一个使用TotalImpact API的简单示例(http://total-impact.org/api-docs). require(RCurl);要求(stringr);要求(RJSONIO) foo老实说,我希望它

一些API使用API调用而不是结果返回令牌,然后使用该令牌进行第二次API调用以获得查询结果。现在,我只是在函数中使用Sys.sleep()来猜测查询完成和令牌可用所需的时间。看来一定有更好的办法。也就是说,您是否可以每隔X秒(或几秒)迭代发送第二个API调用,直到成功

下面是一个使用TotalImpact API的简单示例(http://total-impact.org/api-docs).

require(RCurl);要求(stringr);要求(RJSONIO)

foo老实说,我希望它能马上起作用。如果您不使用
睡眠
,您是否遇到过任何问题?这个特定的api调用在没有睡眠的情况下可以工作,但这只是一个简单的示例。有了这个API和我用R包装的其他API,调用可能需要足够长的时间,您可能需要将sleep设置为15或20,例如@ttmaccer感谢您的测试,请参阅我最后的评论try或trycatch是否有用?是的,可能。调查
require(RCurl); require(stringr); require(RJSONIO)

foo <- function(url, sleep) {
  tt <- getURL(url)
  token <- str_extract(tt[[1]], "[a-z0-9]+")
  message("Pausing a bit for the query to finish...")
  Sys.sleep(time = sleep)
  fromJSON(
    getURL(
      paste("http://total-impact-core.herokuapp.com/item/", token, sep='')))
}
myurl <- "http://total-impact-core.herokuapp.com/tiid/doi/10.1371/journal.pcbi.1000361"
sleeptime <- 4
foo(myurl, sleeptime)

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] C/en_US.UTF-8/C/C/C/C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RJSONIO_0.98-1 stringr_0.6    RCurl_1.91-1   bitops_1.0-4.1 devtools_0.7  

loaded via a namespace (and not attached):
[1] digest_0.5.2 httr_0.1.1   memoise_0.1  plyr_1.7.1   tools_2.15.1